#!/bin/bash
#CD=/mnt/cd
CD=/data/srv/tftp/live
DESTCD=/data/srv/tftp/CD
DEST=/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`

mkdir -p $DESTCD
mkdir -p $DEST

mount $CD
status=0
if [ -f $CD/MythVantage/base/data.lzm ]
then
	status=0
else
	status=1
fi
if [ x$status = x0 ]
then
	echo "copying to disk"
	cp -rp $CD/*  $DESTCD
	cd $MODULES
	   for i in *.lzm 
	   do
		   echo $i
		   lzm2dir $i \/$DEST
	   done
        for i in sys proc dev tmp
        do
        	mkdir $DEST/$i
        done
        chmod 777 /$DEST/tmp
	mknod $DEST/dev/null c 1 3
        mknod $DEST/dev/null c 1 5
        mknod $DEST/dev/console c 5 1
        chmod +s $DEST/usr/bin/Xorg
        chmod +s $DEST/usr/bin/crontab
        chmod +s $DEST/usr/bin/sudo
        chmod +s $DEST/bin/mount
fi
if [ ! -f $DEST/etc/fstab.bak ]
then
	cp $DEST/etc/fstab $DEST/etc/fstab.bak
fi
tac $DEST/etc/fstab.bak|tail +4|tac  > $DEST/etc/fstab
echo "$myip:/data/srv/tftp/masterbuild / nfs" >> $DEST/etc/fstab
echo "/data/srv/tftp/masterbuild  *(rw,no_root_squash,anonuid=1000,anongid=1000)" >> /etc/exports


exportfs -arv

#copy in the modules
mkdir -p $DEST/mnt/live/mnt/
mv $DESTCD/*  $DEST/mnt/live/mnt/

