#!/bin/bash
set -e

export MALLOC_CHECK_=0 # workaround for LP: #520465
export PATH
export LC_ALL=C
export DEBIAN_FRONTEND=noninteractive
export http_proxy=""

if [ -f "/debootstrap/debootstrap" ];then
    echo "I: Fixing root fs permission"
    chown -R 0:0 /

    echo "I: Running second stage"
    
fi

if [ "" ];then
    echo "I: Starting basic services in VM"

    mount -t proc proc /proc
    mount -t sysfs sys /sys
    mkdir -p /dev/pts
    mount -t devpts devpts /dev/pts

    udevd --daemon &
    hostname -F /etc/hostname
fi

dpkg-divert --add --local --divert /usr/sbin/invoke-rc.d.rootstock --rename /usr/sbin/invoke-rc.d
cp /bin/true /usr/sbin/invoke-rc.d



echo "deb http://ports.ubuntu.com/ubuntu-ports precise main universe multiverse" >/etc/apt/sources.list


if [ "" ];then
    ifconfig lo up
    ifconfig eth0 up
    dhclient eth0
fi

locale-gen --no-purge en_GB.UTF-8 || true
locale-gen --no-purge en_GB.UTF-8 || true
echo LANG=en_GB.UTF-8 >>/etc/default/locale

sed -i -e 's/^XKBMODEL=.*$/XKBMODEL=""/' /etc/default/console-setup || true
sed -i -e 's/^XKBLAYOUT=.*$/XKBLAYOUT=""/' /etc/default/console-setup || true
sed -i -e 's/^XKBVARIANT=.*$/XKBVARIANT=""/' /etc/default/console-setup || true
sed -i -e 's/^XKBOPTIONS=.*$/XKBOPTIONS=""/' /etc/default/console-setup || true

echo Europe/London > /etc/timezone
cp -f /usr/share/zoneinfo/Europe/London /etc/localtime || true

groupadd fuse || true

apt-get update

apt-get clean

PKGCOUNT=$(LANG=C apt-get -s install tightvncserver  |grep "newly installed"|cut -d ',' -f2|tr -d ' [a-z]')
echo "packagecount=${PKGCOUNT:-1}"
[ -z "tightvncserver " ] || apt-get -y  install tightvncserver 

groupadd admin || true
echo '%admin  ALL=(ALL) ALL' >>/etc/sudoers

if [ "ubuntu" ] && [ "ubuntu" ];then
    if ! ERROR=$(useradd -G "admin,adm,dialout,cdrom,floppy,audio,dip,video" -s /bin/bash -m -p qeDTPsnM/ZMUo -c "Ubuntu System User" ubuntu 2>&1);then
        echo "E: Failed to add user 'ubuntu'"
        echo "E: $ERROR"
        echo "E: Besides possible user name conflicts, while adding the user"              "a new group is created with the same name as the user, so"              "avoid using existing group names"
        exit 1
    fi
else
    echo "I: Enabling firstboot configuration (as no user/password"          "definition was found at the argument list)"
    PACKAGE="oem-config"
    if [ -n "$(dpkg -l xserver-xorg 2>/dev/null|grep ^ii)" ];then
        if [ $(which gdm) ];then
            PACKAGE="oem-config-gtk"
        elif [ $(which kdm) ];then
            PACKAGE="oem-config-kde"
        fi
    fi
    apt-get install -y --no-install-recommends $PACKAGE
    touch /var/lib/oem-config/run
    rm /usr/lib/ubiquity/plugins/ubi-tasks.py*
fi




apt-get clean

passwd -l root || true

rm -f /usr/sbin/invoke-rc.d
dpkg-divert --remove --rename /usr/sbin/invoke-rc.d

if [ -x /rootstock-user-script ]; then
    echo "I: Running user custom script"
    /rootstock-user-script
    rm -f /rootstock-user-script
fi

if [ "" ];then
    mount -n -o remount,ro -t ext3 /dev/sda / || true
    rm /bin/installer && reboot -fp
fi

