#!/bin/sh
# 	$Id: 8.preparefs.sh,v 1.10 2004/10/22 17:44:57 drizzt Exp $	

set -u

. ./config.sh

if [ \! -x /usr/local/bin/create_compressed_fs ]; then
    echo "Missing cloop utils. Please install sysutils/cloop-utils from ports collection"
    exit 1
fi

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|dist|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


# load g_md kernel module
  if [ ! kldstat -v | grep -q g_md ]; then
	if [ kldload geom_md ]; then
           echo "g_md module loaded."
        else
           echo "g_md module failed to load."
        fi
  fi


  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

  # We need to leave these files in place for the installer
  # if the user has selected this option since the installer
  # mounts up /usr and /var in /cdrom/ and proceeds with the
  # install from this location.
  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
    if [ "$BSDINSTALLER" != "YES" ]; then
	chflags -R noschg $i
	rm -rf $i
    fi 
  done
  sleep 2
    
  umount $MNT
  mdconfig -d -u $MDDEVICE    
  rmdir  $MNT

  gzip -f9 $FSIMAGE

  # We need to leave these files in place for the installer
  # if the user has selected this option since the installer
  # mounts up /usr and /var in /cdrom/ and proceeds with the
  # install from this location.
  for i in `find $FREESBIEBASEDIR -type d -depth 1`
    do
    BASE=`basename $i`
    echo $BASE | egrep -wv $NDDIRS
    if [ $? = 0 ]; then
	if [ "$BSDINSTALLER" != "YES" ]; then
	    chflags -R noschg $i
	    rm -rf $i
	fi
    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 [ ! -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" -ge "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

	mtree -Pcp $FREESBIEBASEDIR/usr | bzip2 -9 > FreeSBIE.usr.dist.bz2
mv FreeSBIE.usr.dist.bz2 $FREESBIEBASEDIR/dist/

        /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" -ge  "5" ]; then
	mtree -Pcp $FREESBIEBASEDIR/var | bzip2 -9 > FreeSBIE.var.dist.bz2
mv FreeSBIE.var.dist.bz2 $FREESBIEBASEDIR/dist/

	/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
