#!/bin/sh
# 	$Id: 8.mkiso.sh,v 1.38 2004/07/09 20:16:03 drizzt Exp $	

set -u

. ./config.sh

cd $LOCALDIR

create_mfsroot()
{
  roundup() echo $((($1+$2-1)-($1+$2-1)%$2))

  MNT=`mktemp -d /tmp/mnt.X`
  COPYDIR="$FREESBIEBASEDIR/rescue $FREESBIEBASEDIR/etc $FREESBIEBASEDIR/root $FREESBIEBASEDIR/proc"
  NDDIRS="cloop|scripts|boot|usr|var|bin|sbin|lib|libexec"
  FSIMAGE=$FREESBIEBASEDIR/boot/mfsroot

  sk=$(du -sk $COPYDIR | cut -f1 | xargs | sed 's/ /+/g' | bc)
  FSSIZE=$(roundup $(($sk*12/10)) 1024)

  dd of=$FSIMAGE if=/dev/zero count=$FSSIZE bs=1k 2>/dev/null

  MDDEVICE=`mdconfig -a -t vnode -f $FSIMAGE`
    
  sleep 2 #Waiting for kernel recognition
        
  if [ ! -c /dev/${MDDEVICE} ] ; then
    echo "No /dev/$MDDEVICE" 1>&2
    exit 1
  fi

  bsdlabel -w $MDDEVICE
  newfs /dev/${MDDEVICE}c
  tunefs -m0 /dev/${MDDEVICE}c
    
  mount /dev/${MDDEVICE}c $MNT

  mtree -deU -f $LOCALDIR/files/mfsroot.run.dist -p $MNT
    
  ln -s /rescue $MNT/bin
  ln -s /rescue $MNT/sbin
    
  for i in $COPYDIR
  do
    if [ ! -d $i ] ; then
      exit 1
    fi
    BASE=$MNT/`basename $i`
    mkdir $BASE
    tar -cpf - -C $i . | tar -xpf - -C $BASE
    chflags -R noschg $i
    rm -rf $i
  done
   
  sleep 2
    
  umount $MNT
  mdconfig -d -u $MDDEVICE    
  rmdir  $MNT

  gzip -f9 $FSIMAGE

  for i in `find $FREESBIEBASEDIR -type d -depth 1`
  do
    BASE=`basename $i`
    echo $BASE | egrep -wv $NDDIRS
    if [ $? = 0 ]; then
      chflags -R noschg $i
      rm -rf $i
    fi
  done
}

check_dyn_linked()
{
    TO_CHECK_BIN_DIR="/usr/local/bin/ /usr/local/sbin/ /usr/X11R6/bin/ /usr/X11R6/sbin/" 
    BIN_DIR=""
    ERROR=""
    DIR=""
    CHCMD="chroot $FREESBIEBASEDIR"
    CHROOTLDD=`$CHCMD which ldd`

    for i in $TO_CHECK_BIN_DIR
    do
    	if [ -d $i ]
    	then
    		BIN_DIR="$BIN_DIR $i"
        fi
    done 

    for j in $BIN_DIR
    do
        echo "Parsing ${j}"
	for i in `$CHCMD find ${j} -type f`
	do
	  FILE_TYPE=`$CHCMD file $i`
	  IS_DYN=`echo ${FILE_TYPE} | grep dynamically`
	  if [ "${IS_DYN}" != "" ]; then 
	      RETVAL=`$CHCMD $CHROOTLDD $i | grep "not found"`
	      if [ "${RETVAL}" != "" ]; then 
		  LIB=`echo ${RETVAL} | cut -d\= -f 1`
		  echo "$i has an unresolved dependency ( ${LIB})"
		  ERROR="Y"
	      fi
	  fi
	done
    done
    [ "${ERROR}" = "Y" ] && echo "Aborting." && exit 1
}
                                                                      
if [ $RELEASE = 5 ]; then
    cd geom_ugz && make && cp geom_ugz.ko $FREESBIEBASEDIR/boot/kernel/ && make clean && cd ..
fi

if [ ! -f $FREESBIEBASEDIR/cloop/usr.cloop ]; then
    usr_must_die()
    {
        echo 
        echo 
        echo "The ${FREESBIEBASEDIR}/usr has been compressed, so"
        echo "it's necessary to move it elsewhere or delete it!"
        echo 
        echo -n "Do you want to Move it or Delete it [M/D]? "
        read dummy
        if [ "${dummy}" != "M" -a "${dummy}" != "D" ]; then
            echo "Aborted"
            exit
        fi
        if [ "${dummy}" = "M" ]; then
            echo -n "Insert a PATH where ${FREESBIEBASEDIR}/usr will be moved to: "
            read path
            if [ ! -d "$path" ]; then
                echo "The path inserted is not a directory. Sorry!"
                exit
            else
                mv $FREESBIEBASEDIR/usr $path
                if [ "$?" != "0" ]; then
                    echo "Errors while moving $FREESBIEBASEDIR/usr. Sorry!"
                    exit
                fi
		mkdir -m 0755 $FREESBIEBASEDIR/usr
            fi
        else
            echo -n "WARNING: ${FREESBIEBASEDIR}/usr is being deleted. Are you sure [Y/N]? "
            read last
            if [ "${last}" != "Y" ]; then
                echo "Pay more attention, the next time :)"
                exit
            else
                rm -rf $FREESBIEBASEDIR/usr
		mkdir -m 0755 $FREESBIEBASEDIR/usr
            fi
        fi
    }
    echo "Creating MFSROOT"
    create_mfsroot

    echo "------------------------------------------------------------"
    echo "Now I'm going to check all dynamically linked ELF binaries,"
    echo "To prevent \"/libexec/ld-elf.so.1: Shared object "
    echo "\"libXXX.so.XX\" not found error."
    echo "------------------------------------------------------------"
    check_dyn_linked

    if [ $RELEASE = 5 ]; then

#        if [ -d $FREESBIEBASEDIR/usr/local/OpenOffice.org1.1.0 -a ! -e $FREESBIEBASEDIR/cloop/openoffice.cloop ]; then
#            mkisofs -lrJL $FREESBIEBASEDIR/usr/local/OpenOffice.org1.1.0 | create_compressed_fs - 65536 > $FREESBIEBASEDIR/cloop/openoffice.cloop
#            rm -rf $FREESBIEBASEDIR/usr/local/OpenOffice.org1.1.0/*
#            #oo_must_die
#        fi

#        mkisofs -lrJL $FREESBIEBASEDIR/usr/X11R6 | create_compressed_fs - 65536 > $FREESBIEBASEDIR/cloop/x11r6.cloop

        /usr/local/bin/mkisofs -lrJL $FREESBIEBASEDIR/usr | /usr/local/bin/create_compressed_fs - 65536 > $FREESBIEBASEDIR/cloop/usr.cloop

        if [ "$?" != "0" ]; then
            echo "Something went wrong."
            exit 1;
        fi

        chflags -R noschg $FREESBIEBASEDIR/usr/
	#usr_must_die
        rm -rf $FREESBIEBASEDIR/usr
	mkdir -m 0755 $FREESBIEBASEDIR/usr
    fi
fi

if [ ! -f $FREESBIEBASEDIR/cloop/var.cloop ]; then
    if [ $RELEASE = 5 ]; then
	/usr/local/bin/mkisofs -lrJL $FREESBIEBASEDIR/var | /usr/local/bin/create_compressed_fs - 65536 > $FREESBIEBASEDIR/cloop/var.cloop
    
	if [ "$?" != "0" ]; then
	    echo "Something went wrong."
	    exit 1;
	fi
	
	chflags -R noschg $FREESBIEBASEDIR/var/
	rm -rf $FREESBIEBASEDIR/var
	mkdir -m 0755 $FREESBIEBASEDIR/var
    fi
fi

echo "Removing unwanted files..."
cd $FREESBIEBASEDIR
rm -f etc/X11/XF86Config
rm -f etc/resolv.conf

rm -f root/.choose
rm -f root/.first_time
rm -f root/.xfirst_time
rm -f root/.kbdmap
rm -f root/.message_done
rm -f root/.xfree86

echo "Preparing ISO..."
cp $LOCALDIR/files/boot.catalog $FREESBIEBASEDIR/boot

/usr/local/bin/mkisofs -b boot/cdboot -no-emul-boot -c boot/boot.catalog -R -J -V FreeSBIE -o $PATHISO .

echo "------------------------------------------------------------"
echo "Your FreeSBIE iso is ready to fly and is located in"
echo "$PATHISO."
echo "If you have got an IDE/ATAPI cd burner you"
echo "can use burncd(1) in order to burn it."
echo "If you want to modify your iso later (e.g.: add/remove"
echo "packages), remember to run A.restore.sh (directly or"
echo "from the dialog) before."
echo "------------------------------------------------------------"
