#!/bin/sh

if [ -z $1 ]; then
  echo "Usage: $0 username"
  exit 1
fi

USERNAME=$1

# Create the lxd group and add given user
if [ -z "`groups ${USERNAME} | grep lxd`" ]; then
  groupadd --force --system lxd
  usermod -G lxd -a $USERNAME
fi

# Map the given user to the container root user
uid=`id --user ${USERNAME}`
idmap="root:$uid:1"
if [ -z "`grep ${idmap} /etc/subuid`" ]; then
  echo ${idmap} | tee -a /etc/subuid /etc/subgid
fi


lxc profile device list default | grep "nic\|disk" > /dev/null
requires_init=$?
version=`lxd --version`
if [ ${version%%.*} -lt 2 -o ${version#*.} -lt 3 ]; then
  if [ "$requires_init" -ne "0" ]; then
    # Running lxd init without args to force the caller to pick the options
    lxd init
  fi
else
  if [ "$requires_init" -ne "0" ]; then
    # Running lxd init with auto flag to choose most of the right options
    lxd init --auto
  fi

  # networking
  ifconfig lxdbr0 > /dev/null
  bridge_exists=$?

  lxc network show lxdbr0 > /dev/null
  network_exists=$?

  # no managed network, no bridge means we need to create a managed network
  if [ "$bridge_exists" -ne "0" ]; then
    if [ "$network_exists" -ne "0" ]; then
      lxc network create lxdbr0

      network_name=`lxc profile device get default eth0 parent`
      if [ "$network_name" != "lxdbr0" ]; then
        lxc network attach-profile lxdbr0 default eth0
      fi
    else
      # a managed network and no bridge means the bridge should be recreated
      # when we restart the lxd service
      service lxd restart
    fi
  fi
fi

mkdir -p /home/$USERNAME/.config/lxc
chown -R $USERNAME:$USERNAME /home/$USERNAME/.config/lxc
