#! /bin/sh

# (c) Thomas Lange, 2001-2002, lange@debian.org

# install addition packags that are located in /fai/files/packages/
for pack in "$addpackages"; do
    if [ -f $FAI/files/packages/$pack ]; then
	dpkg -i --root=$target $FAI/files/packages/$pack
    else
	echo "Package $pack not found in $FAI/files/packages/"
    fi
done

echo $HOSTNAME     > $target/etc/hostname
echo $time_zone    > $target/etc/timezone
ln -fs /usr/share/zoneinfo/${time_zone} $target/etc/localtime

cat > $target/etc/network/interfaces <<-EOF
# generated by FAI
auto lo
iface lo inet loopback
EOF
fcopy /etc/hosts
echo -e "$IPADDR\t$HOSTNAME.$DOMAIN $HOSTNAME" >>$target/etc/hosts

# make /tmp writeable for all, but only the user can remove his/her files
chmod 1777 $target/tmp
chown root:root $target/tmp

# create keyboard layput table
dumpkeys | gzip -9f >$target/etc/console/boottime.kmap.gz

# create NIS/NONIS config
fcopy /etc/nsswitch.conf /etc/host.conf
ifclass NONIS && rm -f $target/etc/defaultdomain
if ifclass NIS; then
    echo $YPDOMAIN > $target/etc/defaultdomain
    rm -f $target/etc/yp.conf
    for s in $YPSRVR; do
	echo "ypserver $s" >> $target/etc/yp.conf
    done
fi

if [ -n "$hdparm" ]; then
    cat > $target/etc/init.d/hdparm <<-EOF
	#! /bin/sh
	# added by FAI
	# tune my harddisks
	$hdparm
EOF
    chmod a+rx $target/etc/init.d/hdparm
    ln -s ../init.d/hdparm $target/etc/rcS.d/S61hdparm
fi

# make scratch directory writeable for all;
ifclass SCRATCH && chmod 1777 $target/scratch
if ifclass FILES_SCRATCH; then
  chmod 1777 $target/files/scratch
  # enable kernel automounter
  fcopy /etc/auto.master /etc/auto.scratch /etc/apt/sources/list
fi

# copy default dotfiles for root account
fcopy /root/.bash_profile /root/.bashrc /root/.cshrc /root/.login /root/.alias /root/.emacs

# suppress emacs errors
touch $target/etc/mailname

ifclass USR_LOCAL_COPY && cp -a /usr/local $target/usr

# make floppy read and writeable for all users
chmod a+rw $target/dev/fd*
# cdrom's are read only for all
chmod a+r /dev/sr* 

# if $kernelimage if not a debfile, then it's the kernel version
# create dummy link, so package kernel-image-* makes new correct link
ln -s /boot/vmlinuz-nolink $target/vmlinuz

if [ -f $files/packages/$kernelimage ]; then
   yes 'n' | dpkg --root=$target -i $files/packages/$kernelimage
else
   # default kernel package
   yes 'n' | $ROOTCMD apt-get install $kernelimage
fi

ifclass X11 && fcopy /etc/X11/XF86Config /etc/X11/Xserver

# put CD-ROM into fstab
# if cdromdevice if defined in *.var, use that value
# if you have both a SCSI and an IDE CD-ROM -> Uuuuups, this will fail
[ "$cdromdevice" ] || cdromdevice=`grep -H cdrom /proc/ide/hd*/media | head -1 | awk -F/ '{print $4}'`
[ "$cdromdevice" ] || cdromdevice=`grep -q CD-ROM /proc/scsi/scsi && echo "sr0"`
echo "/dev/$cdromdevice  /cdrom   auto    ro,user,nosuid,noauto 0 0" >>$target/etc/fstab

# copy bootsector programm to local disk
[ -d /usr/local/sbin ] && cp -p /sbin/bootsector $target/usr/local/sbin
