#!/bin/bash

exiterr () {
	errmsg="$1"
	ec=1
	[ $# -ge 2 ] && ec=$2
	echo "$errmsg" >&2
	exit $ec
}

[ $# -eq 1 ] || exiterr "Add meg a hostnevet!"

host=$1

confdir=/etc/vst
lh=$(uname -n)
volname="${lh}_${host}"
volname=${volname:0:12}

[ -d $confdir ] || exiterr 'Nincs konfigkonyvtar! '"($confdir)"

[ -r $confdir/vstemplate.conf ] || exiterr 'Nincs alapkonfig! '"($confdir/vstemplate.conf)"
. $confdir/vstemplate.conf

[ -r $confdir/templates/$host ] || exiterr 'Nem talalom a konfigot! '"($confdir/templates/$host)"
. $confdir/templates/$host

$MKFS $mkfs_opts -L $volname $disk || exiterr 'Nem sikerult mkfs!'

mkdir $VSERVER_ROOT/$host
[ -d $VSERVER_ROOT/$host ] || exiterr 'Nem sikerult mkdir!'

echo "LABEL=\"${volname}\" $VSERVER_ROOT/$host xfs defaults 0 2" >>/etc/fstab
mount $VSERVER_ROOT/$host

/usr/lib/lxc/templates/lxc-debian -p $VSERVER_ROOT/$host 

cat <<END >>$VSERVER_ROOT/$host/config
# vpsgen generated config section
lxc.utsname=$host
END

for ni in ${net//,/ } ; do
	echo "lxc.network.${ni%%:*}=${ni#*:}" >>$VSERVER_ROOT/$host/config
done

cat <<END >$VSERVER_ROOT/$host/rootfs/etc/network/interfaces
# Network configuration file, generated by vpsgen script

auto lo
iface lo inet loopback
END
for ni in ${ifes//,/ } ; do
	case "${ni%%:*}" in
		sdev)
			echo -e "\niface ${ni#*:} inet static" >>$VSERVER_ROOT/$host/rootfs/etc/network/interfaces
		;;
		autosdev)
			echo -e "\nauto ${ni#*:}" >>$VSERVER_ROOT/$host/rootfs/etc/network/interfaces
			echo "iface ${ni#*:} inet static" >>$VSERVER_ROOT/$host/rootfs/etc/network/interfaces
		;;
		*)
			echo -e "\t${ni%%:*} ${ni#*:}" >>$VSERVER_ROOT/$host/rootfs/etc/network/interfaces
		;;
	esac
done

echo "$INSTALL_MIRROR" >$VSERVER_ROOT/$host/rootfs/etc/apt/sources.list
echo "$INSTALL_SECMIRROR" >>$VSERVER_ROOT/$host/rootfs/etc/apt/sources.list

cat <<END >$VSERVER_ROOT/$host/rootfs/root/initscript.sh
#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin
ifup -a
apt-get update
#debconf debconf/frontend select Noninteractive
cat <<DC | debconf-set-selections
sysv-rc sysv-rc/convert-legacy boolean true
dash dash/sh boolean true
DC
rm /etc/timezone
apt-get -y dist-upgrade
echo "$TZ" >/etc/timezone
rm /etc/localtime
dpkg-reconfigure -fnoninteractive tzdata
apt-get install ed
addgroup --system wheel
echo -e '/pam_wheel.so\ns/^# //\nw' | ed /etc/pam.d/su
echo -e '/trust\ns/^# //\nw' | ed /etc/pam.d/su
echo -e '/root\ns/:[^:]*:/::\nw' | ed /etc/shadow
apt-get -y install apache2 libapache2-mod-php5 php5
END

for i in ${users//,/ } ; do
	gecos=${i#*:}
	gecos="${gecos//_/ }"
	login=${i%%:*}
	cp $confdir/keys/$login.pkey $VSERVER_ROOT/$host/rootfs/root/$login.pkey
	cat <<END >>$VSERVER_ROOT/$host/rootfs/root/initscript.sh
adduser --disabled-password --gecos "$gecos" $login
install -d -m 700 -o $login -g $login /home/$login/.ssh
install -m 600 -o $login -g $login /root/$login.pkey /home/$login/.ssh/authorized_keys
rm /root/$login.pkey
END
done

for i in ${wheel//,/ } ; do
	echo "adduser $i wheel" >>$VSERVER_ROOT/$host/rootfs/root/initscript.sh
done

install -d -m 700 -o root -g root $VSERVER_ROOT/$host/rootfs/root/.ssh
install -m 600 -o root -g root $confdir/keys/$VSBACKUPKEY.pkey $VSERVER_ROOT/$host/rootfs/root/.ssh/authorized_keys

echo "apt-get clean" >>$VSERVER_ROOT/$host/rootfs/root/initscript.sh

chmod +x $VSERVER_ROOT/$host/rootfs/root/initscript.sh
lxc-start -n $host /root/initscript.sh
rm $VSERVER_ROOT/$host/rootfs/root/initscript.sh
ln -s $VSERVER_ROOT/$host/config /etc/lxc/$host.conf
echo "CONTAINERS=\"\$CONTAINERS $host\"" >>/etc/default/lxc
