# Settings !!!*** PLEASE MODIFY THESE ***!!!
export MIRROR="http://anorien.warwick.ac.uk/mirrors/slamd64/slamd64-11.0/"
#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.11g-x86_64-1.tgz"
export SLAPTGET="http://software.jaos.org/slackpacks/11.0-x86_64/$SLAPTGET_PKG"
export TMPDIR=`pwd`"/tmp2"
export ROOT_FS="./Slamd64-11-root_fs"
export PKGHOME=`pwd`"/pkgs/"
export CHROOT="chroot"
export TOOLS=`pwd`"/tools64"
export WARN="-warn"
export WARN=""



function fetch_pkg() {
	echo fetch_pkg $1 $2
	series=$1
	pkg="$2.tgz"
	local_pkg="${PKGHOME}/$series/$pkg"
	if [ ! -e $local_pkg ]; then
		mkdir -p "${PKGHOME}/$series" >& /dev/null
		remote_pkg="${MIRROR}/slackware/$series/$pkg"
		echo "wget -O $local_pkg $remote_pkg"
		wget -O $local_pkg $remote_pkg
		if [ ! -e $local_pkg ]; then
			echo "$local_pkg missing!"
			exit 1
		fi
	fi
}


function setup_tools() {
	if [ ! -e "${TOOLS}" ]; then
		# Preparing some tools
		mkdir $TOOLS >& /dev/null
		for apkg in pkgtools-11.0.0-x86_64-2 tar-1.15.1-x86_64-2 aaa_base-11.0.0-noarch-1 bin-11.0-x86_64-2 bash-3.1.017-x86_64-1 coreutils-5.97-x86_64-3 grep-2.5-x86_64-3 gzip-1.3.5-x86_64-1; do
			echo fetch_pkg a $apkg
			fetch_pkg a $apkg
			tar -C $TOOLS -zxf $local_pkg
		done
		for lpkg in glibc-2.3.6-x86_64-8 libtermcap-1.2.3-x86_64-1; 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
		rmdir $TOOLS/lib $TOOLS/usr/lib
		ln -sf lib64 $TOOLS/lib
		ln -sf lib64 $TOOLS/usr/lib
		mv $TOOLS/lib/tls/incoming/* $TOOLS/lib/
		rmdir $TOOLS/lib/tls/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
	ldconfig -r $TMPDIR
}

function install_local_pkg {
	cp $local_pkg $TMPDIR/tmp
	echo $CHROOT $TMPDIR /sbin/installpkg /tmp/$pkg
	$CHROOT $TMPDIR /sbin/installpkg /tmp/$pkg
	rm $TMPDIR/tmp/$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 "/lib64" > $TMPDIR/etc/ld.so.conf
	echo "/usr/lib64" >> $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-11.0-noarch-2
        tar -C $TMPDIR -zxf $local_pkg

	# Installing the packages from the 'a' series (base)
	for package in etc-11.0-noarch-2 devs-2.3.1-noarch-24 aaa_base-11.0.0-noarch-1 aaa_elflibs-11.0.0-x86_64-7 bash-3.1.017-x86_64-1 bin-11.0-x86_64-2 bzip2-1.0.3-x86_64-3 coreutils-5.97-x86_64-3 cpio-2.5-i386-1 cxxlibs-6.0.3-x86_64-1 dcron-2.3.3-x86_64-2 e2fsprogs-1.38-x86_64-2 findutils-4.2.28-x86_64-1 gawk-3.1.5-x86_64-3 gettext-0.15-x86_64-1 glibc-solibs-2.3.6-x86_64-8 glibc-zoneinfo-2.3.6-noarch-1 grep-2.5-x86_64-3 gzip-1.3.5-x86_64-1 infozip-5.52-x86_64-1 kbd-1.12-x86_64-2 less-394-x86_64-1 logrotate-3.6.8-x86_64-1 openssl-solibs-0.9.8d-x86_64-1 module-init-tools-3.2.2-x86_64-2 pkgtools-11.0.0-x86_64-2 procps-3.2.7-x86_64-1 reiserfsprogs-3.6.19-x86_64-1 sed-4.1.5-x86_64-1 shadow-4.0.6-x86_64-3 slocate-3.1-x86_64-1 sysklogd-1.4.1-x86_64-1 tar-1.15.1-x86_64-2 tcsh-6.14.00-x86_64-2 udev-097-x86_64-6 util-linux-2.12r-x86_64-1 utempter-1.1.3-x86_64-1; do
		install_pkg a ${package}
	done

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

function install_l {
	# Installing the packages from the 'l' series (libraries)
	for package in glibc-2.3.6-x86_64-8 libtermcap-1.2.3-x86_64-1 ncurses-5.4-x86_64-1 zlib-1.2.3-x86_64-1 libidn-0.6.3-x86_64-1; do
		install_pkg l ${package}
	done
}

function install_n {
	# Installing the packages from the 'n' series (networking)
	for package in dhcpcd-2.0.4-x86_64-1 links-2.1pre23-x86_64-1 openssh-4.4p1-x86_64-1 ppp-2.4.4-x86_64-1 tcpip-0.17-x86_64-9 wget-1.10.2-x86_64-1 traceroute-1.4a12-x86_64-1 curl-7.15.5-x86_64-1; 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-x86_64-9
	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/slackware/" >> $TMPDIR/etc/slapt-get/slapt-getrc
	# Update system:
	$CHROOT $TMPDIR slapt-get --update
	$CHROOT $TMPDIR slapt-get --upgrade
	# 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

	# copy the fstab (root=/dev/ubda)
	cp 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 "# We launch just one console for UML:" >> $TMPDIR/etc/inittab
	echo "c1:1235:respawn:/sbin/agetty 38400 tty0 linux" >> $TMPDIR/etc/inittab

	echo "# UML modification: use tty0" >> $TMPDIR/etc/securetty
	echo "tty0" >> $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
}

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 $TMPDIR/tmp
umount $TMPDIR
rmdir $TMPDIR
echo "Done!"
