#!/bin/sh -x
#
# cdtool - The behind-the-scenes magic glue that happens before a CD is made
#
# Michael Jennings <mej@eterm.org>
# 21 July 2000
#
# $Id: bootable-cdtool,v 1.9 2002/06/24 15:51:55 mej Exp $
#

umask 022

PATH=/bin:/usr/bin:/sbin:/usr/sbin

trap 'exit -1' 1 2 15

if [ "x$1" = "x-i" ]; then
    UPD=1
    shift
else
    UPD=0
fi

if [ "x$MEZZANINE_BUILDDIR" != "x" ]; then
    BUILDDIR=$MEZZANINE_BUILDDIR
else
    BUILDDIR=$PWD/build.mezz
fi

#V_VER=`cut -d' ' -f2 /etc/vermillion`
#RH_VER=`cut -d' ' -f5 /etc/redhat-release`
V_VER=7.3.1
RH_VER=7.3

PKGLIST=$HOME/distro_stuff/base-${RH_VER}.list
POST_FILES=$HOME/cvs/jc-cd/post
IMAGE=/os/jc-cd
NET=vermillion-${V_VER}-net
IMGTOOL=imgtool
MEDIATOOL=mediatool

if [ $UPD -eq 1 ]; then
    $IMGTOOL -i $IMAGE -fl $PKGLIST -D $BUILDDIR/$NET/RedHat/RPMS:$HOME:$HOME/pkgs

cd $IMAGE
find bin usr etc lib boot sbin \( -type d -o -type f \) -perm -0002 -xdev -exec chmod o-w {} \;

KERNEL_VER=`ls -1 boot/vmlinuz-*netboot | sed 's!boot/vmlinuz-!!'`

# Set up isolinux
test -d boot && mv boot isolinux && ln -s isolinux boot
cp -a /usr/lib/syslinux/isolinux.bin $IMAGE/boot/

# Move root's home directory to /home
test -d root && mv root home/ && ln -s home/root root

# Relocate dynamic files from /boot to /var/sys
mkdir -p -m 755 var/sys
touch var/sys/kernel.h && rm -f boot/kernel.h && ln -s /var/sys/kernel.h boot/kernel.h
rm -f boot/System.map
ln -sf System.map-${KERNEL_VER} boot/System.map

# Remove the modules.dep files and symlink them to /var/sys/modules.dep
rm -f lib/modules/*/modules.dep
for i in lib/modules/*netboot ; do
    chroot $IMAGE /sbin/depmod -a -F /boot/System.map-${KERNEL_VER} ${KERNEL_VER}
done
mv -f lib/modules/${KERNEL_VER}/modules.dep var/sys/
for i in lib/modules/* ; do
    test -d $i && ln -sf /var/sys/modules.dep $i/modules.dep
done

# Fix symlinks
rm -f etc/X11/X && ln -sf /usr/X11R6/bin/XFree86 etc/X11/X
rm -f etc/X11/xkb && ln -sf /usr/X11R6/lib/X11/xkb etc/X11/xkb
rm -f etc/X11/xdm/authdir && ln -sf /var/lib/xdm etc/X11/xdm/authdir
rm -f etc/sysconfig/network-scripts/ifdown && ln -sf /sbin/ifdown etc/sysconfig/network-scripts/ifdown
rm -f etc/sysconfig/network-scripts/ifup && ln -sf /sbin/ifup etc/sysconfig/network-scripts/ifup

# Turn services on/off as needed.
#for i in smb ; do
#    chroot $IMAGE /sbin/chkconfig --level 345 $i on
#done
for i in atd crond nfs nfslock smb xinetd ; do
    chroot $IMAGE /sbin/chkconfig --del $i
done

# Configure networking
chroot $IMAGE /usr/sbin/netconfig --bootproto=dhcp -d eth0 --hostname=localhost --domain=localdomain

# Install and symlink the plugins
test -x usr/lib/ICAClient/npica.so && install -m 755 usr/lib/ICAClient/npica.so usr/lib/netscape/plugins/
for i in usr/lib/netscape/plugins/* ; do
    rm -f usr/lib/mozilla/plugins/`basename $i` && cp -a $i usr/lib/mozilla/plugins/`basename $i`
done

# Create font directory files
for i in usr/X11R6/lib/X11/fonts/{misc,Type1,Speedo,75dpi,100dpi} usr/share/fonts/*/T* \
         usr/share/AbiSuite/fonts usr/X11R6/lib/X11/fonts/latin2/{Type1,100dpi} \
         usr/share/fonts/ISO8859-[79]/{misc,Type1,75dpi,100dpi} usr/share/fonts/KOI8-R/100dpi ; do
    test -d $i && chroot $IMAGE /usr/X11R6/bin/mkfontdir /$i
done

# Install modified files from CVS tree
(cd $POST_FILES ; tar --exclude .cvsignore --exclude CVS --exclude ChangeLog -cf - .) | (cd $IMAGE ; tar -xf -)
mv -f usr/lib/security/*.so lib/security/
mv -f usr/lib/libnss_* lib/
chroot $IMAGE /sbin/ldconfig
test -e lib/libnss_winbind.so.2 || ln -s libnss_winbind.so lib/libnss_winbind.so.2

mkdir -p var/cache/ICA/zlcache
chmod 1777 var/cache/ICA var/cache/ICA/zlcache
rm -rf usr/lib/ICAClient/cache
ln -s /var/cache/ICA usr/lib/ICAClient/cache

# All login-based authentication should be the same
(cd etc/pam.d
for i in kde xdm gdm ; do
    rm -f $i
    ln login $i
done
)

else
    cd $IMAGE
fi

# Fix ownerships
chroot $IMAGE /usr/bin/find . \( -nouser -o -nogroup \) -exec /bin/chown root:root {} \;
chroot $IMAGE /bin/chown -R lp:lp var/spool/lpd
chown root:root var/spool/lpd

# Make tarballs for writeable filesystems
for i in var etc home ; do
    if [ -L $i ]; then
        rm -f $i
        mv -f mnt/$i .
    fi
    tar -zcf boot/${i}.tgz $i
    mv -f $i mnt/
    ln -s mnt/$i $i
done

# Just the one tmp dir...
rm -rf tmp
ln -s /var/tmp tmp

# Create ISO file, using /isolinux as the boot directory
cd $IMAGE/..
$MEDIATOOL -i jc-cd.iso -D $IMAGE -I isolinux
