#!/bin/bash
#
# LXC Scripts by Roberto Puzzanghera
# More info here https://www.sagredo.eu/lxc-scripts-280/wrapper-scripts-for-lxc-unprivileged-containers-258.html
#

DOMAIN=mydomain.tld # default domain
LXC_PATH=/lxc       # where to store LXC containers

########################################################################################

# It finds the owner of the container
owner() {
  echo $(ls -ld $LXC_PATH/$1 | awk 'NR==1 {print $3}')
}

# It returns 1 if a given container exists, 0 if not
container_exists() {
  i=0
  for CONTAIN in $(lxc-ls); do
    if [ "$CONTAIN" == "$1" ]; then
      ((++i));
      break;
    fi
  done
  return $i
}

# It returns 1 if a container is running, 2 if it is frozen, 0 otherwise
is_it_running() {
  STATUS=0

  # if OWNER must be passed as $2
  if [ -z "$2"  ]; then
    echo "Expecting the owner of the container $1 as an argument of is_it_running(). Exiting..."
    exit 1
  fi

  # is it running/frozen?
  if [ "$(sudo -u $2 lxc-info -s -n $1 2> /dev/null | grep RUNNING$)" ]; then
    STATUS=1
  elif [ "$(sudo -u $2 lxc-info -s -n $1 2> /dev/null | grep FROZEN$)" ]; then
    STATUS=2
  fi
  echo $STATUS
}

# It stops a container
stop_container() {
  OWNER=$(owner $1)
  RUNNING=$(is_it_running $1 $OWNER)
  if [ $RUNNING -ne 0 ]; then
    echo "Stopping container $1..."
    lxd $1
    if [ $? -ne 0 ]; then
      echo "Failed in stopping container $1. Exiting..."
      exit 1
    else
      echo "stopped."
    fi
  fi
}

# Lists unprivileged containers' owners
list_owners() {
  # Build the owners array with a loop on all unprivileged containers
  users=()

  for CONTAIN in $(lxc-ls); do
    # is it unprivileged?
    if grep -q 'lxc.idmap' $LXC_PATH/$CONTAIN/config; then
      # find owner of the container
      USER=$(owner $CONTAIN)

      # Add new element at the end of the array
      if [[ ! " ${users[*]} " =~ " $USER " ]]; then
        users=(${users[@]} $USER)
        # whatever you want to do when array contains value
      fi
    fi
  done

  # users is the array of containers' owners. Sort the array:
  IFS=$'\n' users=($(sort <<<"${users[*]}"))
  unset IFS

  # print the content of the array
  printf '%s\n' "${users[@]}"
}
