# Settings !!!*** PLEASE MODIFY THESE ***!!!
export MIRROR="http://anorien.warwick.ac.uk/mirrors/slamd64/slamd64-12.1/slackware/"
#export SLAPTGET="http://ftp.scarlet.be/pub/linuxpackages/Slackware-10.2/jasorg/slapt-get/slapt-get-0.9.10e-i386-1.tgz"
export SLAPTGET_PKG="slapt-get-0.9.12b-x86_64-1.tgz"
export SLAPTGET="http://software.jaos.org/slackpacks/current-x86_64/${SLAPTGET_PKG}"
export TMPDIR=`pwd`"/tmp2"
export ROOT_FS="./Slamd64-12.1-root_fs"
export PKGHOME=`pwd`"/pkgs.64/"
export CHROOT="chroot"
export TOOLS=`pwd`"/tools64"
export WARN="-warn"
export WARN=""

export FILE_LIST="./file_list.64"


function umount_fs() {
	umount $TMPDIR/tmp
	umount $TMPDIR
	rmdir $TMPDIR
}

function fetch_pkg() {
	series=$1
	pkg=$2
	echo "fetch_pkg $series $pkg = ${match}"
	match=`grep ${series}/${pkg} ${FILE_LIST} | sort | head -n 1`
	if [ -z "${match}" ]; then
		echo "$1/$2 not found"
		umount_fs
		exit 1
	fi
	local_pkg="${PKGHOME}/${match}"
	if [ ! -e $local_pkg ]; then
		mkdir -p "${PKGHOME}/$series" >& /dev/null
		remote_pkg="${MIRROR}/${match}"
		echo "wget -O $local_pkg $remote_pkg"
		wget -O $local_pkg $remote_pkg
		if [ ! -e $local_pkg ]; then
			echo "$local_pkg missing!"
			umount_fs
			exit 1
		fi
	fi
}


function setup_tools() {
	#if [ ! -e "${TOOLS}" ]; then
		# Preparing some tools
		mkdir $TOOLS >& /dev/null
		for apkg in glibc-solibs pkgtools tar aaa_base acl attr bin bash coreutils grep gzip; do
			echo fetch_pkg a $apkg
			fetch_pkg a $apkg
			tar -C $TOOLS -zxf $local_pkg
		done
		for lpkg in glibc libidn lzo ncurses aalib libtermcap; do
			fetch_pkg l $lpkg
			tar -C $TOOLS -zxf $local_pkg
		done
		ln -sf bash2.new $TOOLS/bin/bash
		ln -sf bash $TOOLS/bin/sh
		mv $TOOLS/lib64/incoming/* $TOOLS/lib64/
		rmdir $TOOLS/lib64/incoming
		rm -fr $TOOLS/usr/lib/locale
		mv $TOOLS/bin/tar $TOOLS/bin/tar.new
		ln -sf tar-1.13 $TOOLS/bin/tar
	#fi
}
function fixups() {
	#ln -sf bash2 $TMPDIR/bin/bash
	for replace in `find $TMPDIR -name "*\.new" | grep "\.new\$" | sed 's+\.new$++g'`; do
		echo "replacing $replace"
		rm -f $replace
		mv $replace.new $replace
	done
	ln -sf halt $TMPDIR/sbin/poweroff
	cp inittab $TMPDIR/etc/
	ldconfig -r $TMPDIR
}

function install_local_pkg {
	cp $local_pkg $TMPDIR/tmp
	trimmed_pkg=`basename $local_pkg`
	echo $CHROOT $TMPDIR /sbin/installpkg /tmp/$trimmed_pkg
	$CHROOT $TMPDIR /sbin/installpkg /tmp/$trimmed_pkg
	rm $TMPDIR/tmp/$trimmed_pkg
}
function install_pkg {
	fetch_pkg $1 $2
	install_local_pkg
}

function mkfs {
	# Making the filesystem
	umount $TMPDIR
	dd if=/dev/zero of=$ROOT_FS bs=1048576 count=256
	mkfs.ext3 $ROOT_FS
	mkdir $TMPDIR >& /dev/null
	mount -o loop $ROOT_FS $TMPDIR
}
function copy_tools {
	mkdir $TMPDIR/tmp
	mount -t tmpfs none $TMPDIR/tmp
	cp -apr $TOOLS/* $TMPDIR/
	echo "/lib" > $TMPDIR/etc/ld.so.conf
	echo "/usr/lib" >> $TMPDIR/etc/ld.so.conf
	ldconfig -r $TMPDIR
}


function install_a {
	touch $TMPDIR/var/run/utmp
	touch $TMPDIR/var/log/wtmp
	# we need this one early
	fetch_pkg a etc
        tar -C $TMPDIR -zxf $local_pkg

	# Installing the packages from the 'a' series (base)
	for package in etc devs aaa_base aaa_elflibs bash bin bzip2 coreutils cpio cxxlibs dcron e2fsprogs findutils gawk gettext getty-ps glibc-solibs glibc-zoneinfo grep gzip infozip jfsutils kbd less logrotate openssl-solibs module-init-tools pkgtools procps reiserfsprogs sed shadow slocate sysklogd tar tcsh udev util-linux utempter sysvinit-scripts util-linux-ng; do
		install_pkg a ${package}
	done

	#sysvinit fails because we do not have /dev/initctl...
	fetch_pkg a sysvinit
	tar -C $TMPDIR -zxf $local_pkg
}

function install_l {
	# Installing the packages from the 'l' series (libraries)
	for package in glibc libtermcap ncurses zlib libidn; do
		install_pkg l ${package}
	done
}

function install_n {
	# Installing the packages from the 'n' series (networking)
	for package in libgpg-error  openldap-client cyrus-sasl gpgme libassuan gnupg dhcpcd links openssh ppp tcp_wrappers wget traceroute curl; do
		install_pkg n ${package}
	done
	#this one does not seem to work - but no error message and works when done by hand...
	#fetch_pkg n tcpip-0.17-i486-39
	#tar -C $TMPDIR -zxf $local_pkg
}

function install_slaptget {
	# Installing the 'slapt-get' package management tool:
	wget -c -nc $SLAPTGET
	pkg=$SLAPTGET_PKG
	local_pkg=`pwd`/$pkg
	install_local_pkg
	# Make new new config from our mirror defined above
	cp $TMPDIR/etc/slapt-get/slapt-getrc $TMPDIR/etc/slapt-get/slapt-getrc.save
	grep -v "slackware.com" $TMPDIR/etc/slapt-get/slapt-getrc.save > $TMPDIR/etc/slapt-get/slapt-getrc
	echo "SOURCE=$MIRROR/" >> $TMPDIR/etc/slapt-get/slapt-getrc
	# Update system:
	mount -t proc none $TMPDIR/proc
	$CHROOT $TMPDIR slapt-get --add-keys
	$CHROOT $TMPDIR slapt-get --update
	$CHROOT $TMPDIR slapt-get --upgrade
	umount $TMPDIR/proc
	# restore default slapt-get:
        mv $TMPDIR/etc/slapt-get/slapt-getrc.save $TMPDIR/etc/slapt-get/slapt-getrc
}

function configure {
	# ldconfig the newly installed libraries
	ldconfig -r $TMPDIR

	# These must be present
	cp inittab fstab $TMPDIR/etc/
	# create the ubd devices
	for ((i=0; i<8; i++))
	do
		let=$(echo $i|tr '0-7' 'a-z')
		mknod $TMPDIR/dev/ubd$let b 98 $[16 * $i]
		for ((p=1; p<=16; p++)) do
			mknod $TMPDIR/dev/ubd$let$p b 98 $[16 * $i + $p]
		done
	done

	# copy current resolv
	cp /etc/resolv.conf $TMPDIR/etc

	# tweak the inittab to only use tty0 and add it to securetty
	#cp $TMPDIR/etc/inittab $TMPDIR/etc/inittab.save
	#grep -v "c[0-9]:" $TMPDIR/etc/inittab.save > $TMPDIR/etc/inittab
	#echo "# Consoles:" >> $TMPDIR/etc/inittab
	#echo "c1:1235:respawn:/sbin/agetty 38400 tty0 linux" >> $TMPDIR/etc/inittab
	#echo "s0:1235:respawn:/sbin/agetty 38400 ttyS0 linux" >> $TMPDIR/etc/inittab
	cp inittab $TMPDIR/

	echo "# Consoles:" >> $TMPDIR/etc/securetty
	echo "tty0" >> $TMPDIR/etc/securetty
	echo "ttyS0" >> $TMPDIR/etc/securetty

	# make the first network interface start with dhcp
	#cp $TMPDIR/etc/rc.d/rc.inet1.conf $TMPDIR/etc/rc.d/rc.inet1.conf.save
	#sed 's+USE_DHCP\[0\]=""+USE_DHCP\[0\]="yes"+' < $TMPDIR/etc/rc.d/rc.inet1.conf.save > $TMPDIR/etc/rc.d/rc.inet1.conf
	cp rc.inet1.conf $TMPDIR/etc/rc.d/rc.inet1.conf
}

mkfs
setup_tools
copy_tools
install_a
install_l
install_n
fixups
configure
install_slaptget
dd if=/dev/zero of=$TMPDIR/blank >& /dev/null
sync
rm $TMPDIR/blank

umount_fs
echo "Done!"
