#! /bin/bash 
## -----------------------------------------------------------------------
##   
##   Copyright 2000-2002 H. Peter Anvin - All Rights Reserved
##
##   This program is free software; you can redistribute it and/or modify
##   it under the terms of the GNU General Public License as published by
##   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
##   USA; either version 2 of the License, or (at your option) any later
##   version; incorporated herein by reference.
##
## -----------------------------------------------------------------------
##
##   Revision 0.9 By Jeremy McDaniel for Plan-B  05/27/03 04:30:29 EST
##   eliminated several sections as most the build is done hand to keep 
##   the environment which is configured separate from the script.  For 
##   the full script by Anvin see make.sh, as pmake was clipped to make 
##   it easier to read.
## 
## -----------------------------------------------------------------------

set -xe

BUILDDIRS='irtree isolinux'
READMES='gpl_license.txt'

umask 022

mkdir -p work/isotree work/nozftree work/nozftree/boot work/nozftree/isolinux
mkdir -p work/roottree work/buildtree

# Make initrd
./makeinit.sh

# Set up boot files
cd work/nozftree/isolinux
  cp -a ../../../usr/lib/syslinux/isolinux.bin .
  cp ../../initrd.gz .
  cp -a ../../../isolinux/. .
cd ../../..

# This is a list of items in the root which will get symlinked to
# the usr directory
symlink_in_root='bin sbin lib'
# Generate root tree.
cp -a root/. work/roottree/. 
# Remove things which are already symlinked
(
  cd work/roottree
  for l in $symlink_in_root; do
    if [ -L "$l" -a -e ../usr/root/"$l" ]; then
      rm -f "$l"
      cp -a ../usr/root/"$l" "$l"
    fi
  done
)
# Create root tarball
( cd work/roottree && tar cfp - . | gzip -9 > ../nozftree/root.tar.gz )

# Create buildinfo file -- edited for Plan-B
echo -en "Version: Custom \nBuild Date: `date`" >  work/nozftree/build.nfo

# Install README files
cp $READMES work/nozftree

mkzftree -p2 usr work/usr.zf

mkzftree -p2 work/isotree work/isotree.zf

mkisofs -z -v -v -o planb-custom.iso -b isolinux/isolinux.bin -c isolinux/isoboot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -l -J -R -hide-rr-moved -A Custom -P "Custom" -V "PlanB-Custom" work/nozftree work/isotree.zf work/usr.zf > mkisofs.log 2>&1

# Clean up
if [ $? = 0 ]; then
    rm mkisofs.log 
    rm -Rfd work 
fi

# EOF
