#!/bin/sh
# $Id: 7.customuser.sh,v 1.31 2004/10/22 17:44:57 drizzt Exp $

set -u

. ./config.sh
. ./.common.sh

echo "Installing customized files..."
cp $LOCALDIR/COPYING $FREESBIEBASEDIR/COPYRIGHT.FreeSBIE || print_error
[ -e $LOCALDIR/files/foobar/irssi_config ] && mkdir -p $FREESBIEBASEDIR/root/.irssi && cp -v $LOCALDIR/files/foobar/irssi_config $FREESBIEBASEDIR/root/.irssi/config
[ -e $LOCALDIR/files/foobar/mutt.tar.bz2 ] && tar jxvf $LOCALDIR/files/foobar/mutt.tar.bz2 -C $FREESBIEBASEDIR/root/
cp -Rv $LOCALDIR/files/foobar/ $FREESBIEBASEDIR/root/ || print_error
find $FREESBIEBASEDIR/root/ -type d -name CVS -exec rm -rf {} \; 2>/dev/null
rm -f $FREESBIEBASEDIR/root/irssi_config $FREESBIEBASEDIR/root/mutt.tar.bz2

pw usermod root -V $FREESBIEBASEDIR/etc -s /bin/tcsh || print_error

if [ -f $FREESBIEBASEDIR/usr/X11R6/bin/XFree86 ]; then
	echo "X installed, creating /etc/X11/..."
	mkdir -p $FREESBIEBASEDIR/etc/X11/ || print_error
	rm $FREESBIEBASEDIR/usr/X11R6/bin/X  || print_error
	ln -sf $FREESBIEBASEDIR/usr/X11R6/bin/XFree86 $FREESBIEBASEDIR/usr/X11R6/bin/X || print_error
fi

if [ "x$MINIMAL" = "xYES" ]; then
    rm -rf \
	$FREESBIEBASEDIR/usr/share/nls \
	$FREESBIEBASEDIR/usr/share/bsnmp \
	$FREESBIEBASEDIR/usr/share/dict \
	$FREESBIEBASEDIR/usr/share/doc \
	$FREESBIEBASEDIR/usr/share/examples \
	$FREESBIEBASEDIR/usr/share/games \
	$FREESBIEBASEDIR/usr/share/isdn \
	$FREESBIEBASEDIR/usr/share/locale \
	$FREESBIEBASEDIR/usr/share/me \
	$FREESBIEBASEDIR/usr/share/pcvt \
	$FREESBIEBASEDIR/usr/share/security \
	$FREESBIEBASEDIR/usr/share/sendmail \
	$FREESBIEBASEDIR/usr/share/bsnmp \
	$FREESBIEBASEDIR/usr/share/calendar/*/ \
	$FREESBIEBASEDIR/usr/include/*

fi

if [ "$BSDINSTALLER" = "YES" ]; then
	if [ "$RELEASE" -ge "5" ]; then
		PKGSUFFIX=".tbz"
		PKGDIR="FBSD5"
	else
		PKGSUFFIX=".tgz"
		PKGDIR="FBSD4"
	fi
	SITEURL="http://www.bsdinstaller.com/packages"
	PACKAGES="libaura~libaura-latest$PKGSUFFIX \
	libdfui~libdfui-latest$PKGSUFFIX \
	libinstaller~libinstaller-latest$PKGSUFFIX \
	dfuibe_installer~dfuibe_installer-latest$PKGSUFFIX \
	dfuife_cgi~dfuife_cgi-latest$PKGSUFFIX \
	thttpd~thttpd-notimeout-latest$PKGSUFFIX \
	cpdup~cpdup-latest$PKGSUFFIX \
	dfuife_curses~dfuife_curses-latest$PKGSUFFIX"

	if [ -e $FREESBIEBASEDIR/chrootscript ]; then
	    rm $FREESBIEBASEDIR/chrootscript
	fi
	for PKG in ${PACKAGES}; do
		pkg_name=`echo $PKG | cut -d~ -f 1`
		pkg_url=`echo $PKG | cut -d~ -f 2`
		echo "Checking for $pkg_name ..."
		does_exist=`chroot $FREESBIEBASEDIR pkg_info | grep $pkg_name`
		if [ -n "$does_exist" ]; then
			echo "Package $pkg_name already exists.  Skipping."
		else
			# Fetch package from BSDInstaller.com and install
			echo "Downloading package $SITEURL/$PKGDIR/$pkg_url ..."
			cd $FREESBIEBASEDIR && fetch "$SITEURL/$PKGDIR/$pkg_url"
			echo pkg_add /$pkg_url >> $FREESBIEBASEDIR/chrootscript
			echo rm /$pkg_url >> $FREESBIEBASEDIR/chrootscript
			echo ldconfig -m /usr/local/lib >> $FREESBIEBASEDIR/chrootscript
		fi
	done
	echo "Chrooting into $FREESBIEBASEDIR and running /chrootscript ..."
	if [ -f "$FREESBIEBASEDIR/chrootscript" ]; then
	    chroot $FREESBIEBASEDIR sh /chrootscript
	    rm $FREESBIEBASEDIR/chrootscript
	fi
	cp $LOCALDIR/files/scripts/after_installation_routines.sh $FREESBIEBASEDIR/usr/local/bin/
	chmod a+x $FREESBIEBASEDIR/usr/local/bin/after_installation_routines.sh
fi
