#!/bin/sh

source ../Common/util.inc

#set -e

echo -n "Password for root: "
read PASS

# File with what packages you want to install
if [ "${1}" != "" ]; then
	PKGLIST="${1}"
	IMG_ADD="-${1}"
else
	echo "Usage: mkroot package-list-file"
	exit 1
fi

# Put here the path to SLACKWARE directory, the one that contain slackware dir
SLACK=/data/SLACK

# Size in megabytes that you want to be FREE on filesystem.
# Please, re-read above line!
SIZE=50

# Name of the file that you want fs to be put into
IMG_NAME="Slack${IMG_ADD}.img"
OUTFILE="../${IMG_NAME}"

# type of the filesystem
FSTYPE=reiserfs
FSCOMMAND="mkreiserfs -f -f"


# NO NEED TO MODIFY ANYTHING BELOW THIS LINE


echo "Build filesystem in ${OUTFILE} from ${SLACK}..."
mkdir -p TMP

while read pkg; do
	# Find package
	FOUND="`find ${SLACK}/slackware ADD /data/SLACK.pkg ../.common -name \"${pkg}-[0-9]*.tgz\"`"
	if [ "${FOUND}" = "" ]; then
		echo "Package ${pkg} not found! Skip it!"
	else
		echo "Install ${FOUND}..."
		installpkg -root TMP "${FOUND}" 1>>/dev/null
	fi
done < ${PKGLIST}

chroot TMP ldconfig

mkdir -p TMP/selinux
cp ADD/fstab TMP/etc
cp ADD/syslog.conf TMP/etc
mkdir -p TMP/lib/modules/
cp -a ../.common/MODULES/* TMP/lib/modules/
if [ -r "../.common/MODULES/System.map" ]; then
	cp -a ../.common/MODULES/System.map TMP/boot/
fi
cp ../.common/rc.dinouml TMP/etc/rc.d/rc.local
chmod a+x TMP/etc/rc.d/rc.local
#echo "sc1:12345:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> TMP/etc/inittab
echo "ttyS0" >> TMP/etc/securetty
echo "DinoUML" > TMP/etc/HOSTNAME
sed -e 's/-r now/-h now/' TMP/etc/inittab > TMP/etc/inittab.new
mv TMP/etc/inittab.new TMP/etc/inittab 

i=0
for dev in a b c d e f g h; do
	echo "Make ubd${dev} (mknod /dev/ubd${dev} b 98 ${i})..."
	rm -f TMP/dev/ubd${dev}
	mknod TMP/dev/ubd${dev} b 98 ${i}
	i=$[${i}+16]
done

# rights
chmod a+x TMP/etc/rc.d/rc.ip_forward
if [ -r "TMP/etc/rc.d/rc.nfsd" ]; then
	chmod a-x TMP/etc/rc.d/rc.nfsd
fi

echo "Add user catab..."
chroot TMP /usr/sbin/useradd -g users -d /home/catab \
	-m -s /bin/sh catab

if [ "${PASS}" = "" ]; then
	chroot TMP passwd catab
else
	echo "Set catab pass..."
	chroot TMP bash -c "(echo \"catab:${PASS}\" | chpasswd)"

	echo "Set root pass..."
	chroot TMP bash -c "(echo \"root:${PASS}\" | chpasswd)"
fi

echo "Create ssh keys..."
chroot TMP /usr/bin/ssh-keygen -t rsa1 -f /etc/ssh/ssh_host_key -N ''
chroot TMP /usr/bin/ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
chroot TMP /usr/bin/ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''

echo "Put everything in ${OUTFILE}..."
DU=`du -ms TMP | cut -f1`
echo "DU=${DU}"
TOTAL=$[${SIZE}+${DU}]
echo "TOTAL=${TOTAL}"

echo "Make fsfile..."
rm -f "${OUTFILE}"
dd if=/dev/zero of="${OUTFILE}" bs=1M seek=${TOTAL} count=0

echo "Make fs..."
${FSCOMMAND} "${OUTFILE}"

echo "Mount file..."
mkdir -p TMP.loop
mount "${OUTFILE}" TMP.loop -t${FSTYPE} -oloop

echo "Copy files..."
cp -a TMP/* TMP.loop

echo "Umount file..."
umount TMP.loop
rmdir TMP.loop

rm -rf TMP


make_default "${IMG_NAME}"
