#!/bin/bash
#CD=/mnt/cd
. /etc/profile
. /etc/systemconfig

TFTP=/data/srv/tftp
CD=/data/srv/tftp/live
DESTCD=/data/srv/tftp/CD
BASE=/data/srv/tftp/masterbuild

MODULES=$DESTCD/MythVantage/base/
myip=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'|head -n1`
SRCFILES=$BASE/mnt/live/mnt/MythVantage/base/

function log () {
echo $1
}

function check_xvfb () {
export    DISPLAY=127.0.0.1:25
 Xvfb -ac :25  &

}


function create_base_image() {
echo $SRCFILES
if [ -f $SRCFILES/data.lzm ]
then
    mkdir $HOST_IMG_DIR
    cd $SRCFILES
    echo "Creating image.." > $BASE/tmp/.install_state
    LIST=`ls *.lzm`
    for i in  $LIST
    do
       echo  $i
       echo  $i > $BASE/tmp/.install_percent
       lzm2dir $i \/$HOST_IMG_DIR
    done

    for i in sys proc dev tmp
    do
         mkdir /$HOST_IMG_DIR/$i
    done

    chmod 777 /$HOST_IMG_DIR/tmp
    mknod /$HOST_IMG_DIR/dev/null c 1 3
    mknod /$HOST_IMG_DIR/dev/null c 1 5
    mknod /$HOST_IMG_DIR/dev/console c 5 1
    chmod +s /$HOST_IMG_DIR/usr/bin/Xorg
    chmod +s /$HOST_IMG_DIR/usr/bin/crontab
    chmod +s /$HOST_IMG_DIR/usr/bin/sudo
    chmod +s /$HOST_IMG_DIR/bin/mount
    #copy fstab from masterbuild
    cp -f $BASE/etc/fstab /$HOST_IMG_DIR/etc/fstab
    echo "Done: installing data" > $BASE/tmp/.install_state
    return 0
else
    echo "Error: no modules" > $BASE/tmp/.install_state
    log "couldn't find modules to install"
    return 1
fi
}

function configure_image () {
    echo "Configuring system" > $BASE/tmp/.install_state
    cp -f $BASE/etc/systemconfig  $HOST_IMG_DIR/etc/systemconfig
    chown root:mythtv $HOST_IMG_DIR/etc/systemconfig
    chmod 775 "$HOST_IMG_DIR" /etc/systemconfig
    cp /etc/mtab $HOST_IMG_DIR/etc/mtab
     mount --bind /dev $HOST_IMG_DIR/dev

    chroot "$HOST_IMG_DIR" $MV_ROOT/bin/systemconfig.sh misc,hostype,network,this_is_install
#copy syssettings template to here.
    SE=$BASE$TEMPLATES/settings/syssettings
    mkdir -p  $HOST_IMG_DIR/$TEMPLATES/settings/syssettings
    cp -rp $SE/*  ${HOST_IMG_DIR}/$TEMPLATES/settings/syssettings
    chown root:mythtv $HOST_IMG_DIR/etc/systemconfig
    chown -R mythtv:mythtv $HOST_IMG_DIR/$TEMPLATES/settings/syssettings
    chmod -R 775  $HOST_IMG_DIR/$TEMPLATES/settings/syssettings
#update remotedb with this host settings
    OLDDISPLAY=$DISPLAY
    check_xvfb
    chroot $HOST_IMG_DIR  $MV_ROOT/bin/MythVantage  -t  restore,default_1
    chroot $HOST_IMG_DIR  $MV_ROOT/bin/restore_default_settings.sh -c restore -t syssettings
    if [ !  $? = 0 ]
    then
            touch $mountpoint/tmp/.dbsysfailed
            chmod 777 $mountpoint/tmp/.dbsysfailed
    else
        #Run second time
        chroot "$HOST_IMG_DIR" $MV_ROOT/bin/systemconfig.sh misc,hostype,network,this_is_install
        chroot "$HOST_IMG_DIR" $MV_ROOT/bin/restore_default_settings.sh -c  ACCESSCONTROL
    fi
chroot "$HOST_IMG_DIR" $MV_ROOT/bin/systemconfig.sh advanced,this_is_install
cp -f $BASE/etc/X11/xorg.conf   $HOST_IMG_DIR/etc/X11/xorg.conf
echo "Done" > $BASE/tmp/.install_state
DISPLAY=$OLDDISPLAY

#cp /tmp/mythvantage_install.log  $HOST_IMG_DIR/var/log/
}

function create_tftp_boot () {

    CFGFILE=$TFTP/pxelinux.cfg/01-$2
    echo "DEFAULT mythfe" > $CFGFILE
    echo "LABEL mythfe" >> $CFGFILE
    echo "kernel masterbuild/boot/vmlinuz26STB" >> $CFGFILE
    echo "append vga=normal root=/dev/nfs rw netboot  nfsroot=$myip:/data/srv/tftp/$1 ip=dhcp init=/sbin/runit" >> $CFGFILE
    echo "TIMEOUT 2" >>  $CFGFILE
}

function create_nfs_export () {
echo "/data/srv/tftp/$1    *(rw,no_root_squash,anonuid=1000,anongid=1000) " >> /etc/exports
exportfs -arv
}

#-------------------------------------------------------------------------------MAIN PROGRAM ---------------------------------------------------------------------
if [ x$1 = x  -o   x$2 = x ]
then
    echo "usage:  hostname macaddress"
	exit 1
fi

image_name=$1
mac_address=`echo $2| tr "[:upper:]" "[:lower:]"`

echo "using image_name of $image_name"
echo "using mac_address of $mac_address"

HOST_IMG_DIR=$TFTP/$1

create_base_image $image_name
configure_image
umount $HOST_IMG_DIR/dev
create_tftp_boot $image_name $mac_address
create_nfs_export $image_name




#create_dhcp_entry $mac_address