#!/bin/sh

CONTAINER=$1
INSTROOT=$2
VERSION=$3
ARCH=$4
DATADIR=$5
OVERLAY=$6
PACKAGE=$7

CONTAINER_PATH=/var/lib/lxc/$CONTAINER
CACHEDIR="/var/cache/$PACKAGE"
OVERLAYFS="$CONTAINER_PATH/overlay"

LXCARCH="x86_64"
if [ "$ARCH" != "x86_64" ]; then
	LXCARCH="x86"
fi


sed -i -e 's|^if .*readonlyroot.*cmdline.*$|if ( /bin/false ); then|' $INSTROOT/etc/init.d/halt

if [ -e $INSTROOT/etc/rc.sysinit ]; then
	sed -i -e '/hwclock/d' $INSTROOT/etc/rc.sysinit
fi
if [ -e $INSTROOT/etc/init.d/halt ]; then
	sed -i -e '/hwclock/d' $INSTROOT/etc/init.d/halt

	cat <<EOF > $INSTROOT/etc/init.d/lxc-pre-halt
#!/bin/sh

sed -i -e 's|^if .*readonlyroot.*cmdline.*$|if ( /bin/false ); then|' /etc/init.d/halt

if [ -e /etc/rc.sysinit ]; then
sed -i -e '/hwclock/d' /etc/rc.sysinit
fi
if [ -e /etc/init.d/halt ]; then
sed -i -e '/hwclock/d' /etc/init.d/halt
fi

EOF
	chmod 755 $INSTROOT/etc/init.d/lxc-pre-halt
	(
		cd $INSTROOT/etc/rc.d/rc0.d
		ln -s ../init.d/lxc-pre-halt S00lxc-pre-halt
		cd $INSTROOT/etc/rc.d/rc6.d
		ln -s ../init.d/lxc-pre-halt S00lxc-pre-reboot
	)
fi

if [ -f /etc/sysconfig/clock ]; then
	cp -f /etc/sysconfig/clock $INSTROOT/etc/sysconfig/
fi

chroot $INSTROOT chkconfig network on
chroot $INSTROOT chkconfig rsyslog off
cp -f "$DATADIR/defs/$VERSION/$ARCH/network" $INSTROOT/etc/sysconfig/
cp -f "$DATADIR/defs/$VERSION/$ARCH/ifcfg-eth0" $INSTROOT/etc/sysconfig/network-scripts/
cp -f "$DATADIR/defs/$VERSION/$ARCH/fstab" $INSTROOT/etc/fstab

chroot $INSTROOT useradd -u 1000 -g 10 -d /home/$PACKAGE -m -s /bin/bash $PACKAGE

mkdir -p $CONTAINER_PATH/rootfs

mv -f $CONTAINER_PATH/config $CONTAINER_PATH/config.orig
cat <<EOF > $CONTAINER_PATH/config
lxc.utsname = $CONTAINER
lxc.arch = $LXCARCH
lxc.rootfs = $CONTAINER_PATH/rootfs
lxc.mount.auto = cgroup:mixed sys:mixed
lxc.tty = 1
lxc.pts = 1024
lxc.hook.autodev = $CONTAINER_PATH/autodev
EOF

cat <<EOF > $CONTAINER_PATH/autodev
#!/bin/sh

cd \${LXC_ROOTFS_MOUNT}/

mkdir ./dev/shm

EOF
chmod 755 $CONTAINER_PATH/autodev

if [ $OVERLAY -ne 0 ]; then
	cat <<EOF > $CONTAINER_PATH/pre-start
#!/bin/sh

mount -t overlay -o lowerdir=$CONTAINER_PATH/rootfs.lower,upperdir=$OVERLAYFS/upper,workdir=$OVERLAYFS/work overlay $CONTAINER_PATH/rootfs
mount -o bind,_netdev $CACHEDIR/$VERSION/$ARCH $CONTAINER_PATH/rootfs/var/cache/apt

if [ -d $CONTAINER_PATH/tmp ]; then
	mkdir -p $CONTAINER_PATH/rootfs/tmp/jobs
	mount -o bind,_netdev $CONTAINER_PATH/tmp $CONTAINER_PATH/rootfs/tmp/jobs
fi

EOF
	chmod +x $CONTAINER_PATH/pre-start

	cat <<EOF > $CONTAINER_PATH/post-stop
#!/bin/sh

if [ -d $CONTAINER_PATH/tmp ]; then
	umount $CONTAINER_PATH/rootfs/tmp/jobs
fi

umount $CONTAINER_PATH/rootfs/var/cache/apt
umount $CONTAINER_PATH/rootfs

EOF
	chmod +x $CONTAINER_PATH/post-stop

else

	cat <<EOF > $CONTAINER_PATH/pre-start
#!/bin/sh

mount -o bind,_netdev $CACHEDIR/$VERSION/$ARCH $CONTAINER_PATH/rootfs/var/cache/apt

if [ -d $CONTAINER_PATH/tmp ]; then
	mkdir -p $CONTAINER_PATH/rootfs/tmp/jobs
	mount -o bind,_netdev $CONTAINER_PATH/tmp $CONTAINER_PATH/rootfs/tmp/jobs
fi

EOF
	chmod +x $CONTAINER_PATH/pre-start

	cat <<EOF > $CONTAINER_PATH/post-stop
#!/bin/sh

if [ -d $CONTAINER_PATH/tmp ]; then
	umount $CONTAINER_PATH/rootfs/tmp/jobs
fi

umount $CONTAINER_PATH/rootfs/var/cache/apt

EOF
	chmod +x $CONTAINER_PATH/post-stop
fi

cat <<EOF >> $CONTAINER_PATH/config
lxc.hook.pre-start = $CONTAINER_PATH/pre-start
lxc.hook.post-stop = $CONTAINER_PATH/post-stop
EOF

cat $CONTAINER_PATH/config.orig >> $CONTAINER_PATH/config
rm -f $CONTAINER_PATH/config.orig
