#!/bin/sh

TARGETDISK=
RPOOL=

SWAPSIZE="1G"
SRCLOADER=1
SETS="manpages games"

### TODO: many things to check if user is trying to do anything funny or when a process failed

PATH=/dist/bin:/dist/sbin:/dist/usr/bin:/dist/usr/sbin:${PATH}
export PATH

echo "FreeBSD Installation to ZFS root helper script"
echo "Please don't attach any non-empty disks to prevent unwanted result"
echo "Or use at your own risk"
echo 

if [ "${SRCLOADER}" = 0 ] && [ ! -e "/tmp/loader" ]; then
	echo "Please put the ZFS-capable loader in /tmp/loader"
	exit 1
fi

while [ -z "${TARGETDISK}" ] || [ ! -e "/dev/${TARGETDISK}" ]; do
	printf "Please specify target disk: "
	read TARGETDISK
	[ -e "/dev/${TARGETDISK}" ] || echo "Invalid disk name or disk doesn't exist"
done
##TODO: disk checker

echo "Purging disk..."

dd if=/dev/zero of="/dev/${TARGETDISK}" bs=512k count=10

echo "Done"

if [ -z "${RPOOL}" ]; then
	printf "Please specify name for ZFS root pool (default: rpool): "
	read RPOOL
fi
[ -n "${RPOOL}" ] || RPOOL="rpool"
##TODO: sanitizer rpool name. avoid: whatever folder names in /


echo "Creating GPT partitions..."

gpart create -s GPT "${TARGETDISK}"
gpart add -s 128 -t freebsd-boot "${TARGETDISK}"
gpart add -t freebsd-zfs "${TARGETDISK}"
gpart bootcode -b /dist/boot/pmbr -p /dist/boot/gptzfsboot -i 1 "${TARGETDISK}"

echo "Done"

echo "Loading ZFS module..."

kldload /dist/boot/kernel/opensolaris.ko
kldload /dist/boot/kernel/zfs.ko

echo "Done"

echo "Creating default pools"
zpool create "${RPOOL}" "/dev/${TARGETDISK}p2"
zfs create "${RPOOL}/usr"
zfs create "${RPOOL}/usr/home"
zfs create "${RPOOL}/var"
zfs create -V "${SWAPSIZE}" "${RPOOL}/swap"
zfs set org.freebsd:swap=on "${RPOOL}/swap"
zfs set checksum=off "${RPOOL}/swap"
echo "Done"

echo "Installing sets..."
DESTDIR="/${RPOOL}"
export DESTDIR
cd "/dist/"[0-9].[0-9]-*
for i in base ${SETS}; do
	echo "Installing ${i} set..."
	(cd "${i}"; sh install.sh)
	echo "Done"
done
echo "Installing kernel..."
(cd kernels; sh install.sh GENERIC)
echo "Done"
if [ -n "${SRCLOADER}" ]; then
	echo "Installing src set..."
	(cd src; sh install.sh all)
	echo "Done"
else
	echo "Not installing loader from source, skipping src set."
fi
echo "Set installation finished"

echo "Voodoo magic..."
rmdir "/${RPOOL}/boot/kernel"
mv "/${RPOOL}/boot/GENERIC" "/${RPOOL}/boot/kernel"
cat <<EOF>>"/${RPOOL}/boot/loader.conf"
zfs_load="YES"
#vfs.root.mountfrom="zfs:${RPOOL}"
tmpfs_load="YES"
EOF
echo "tmpfs /tmp tmpfs rw 0 0" >> "/${RPOOL}/etc/fstab"
echo "${RPOOL} / zfs rw,noatime 0 0" >> "/${RPOOL}/etc/fstab"
chroot "/${RPOOL}" /usr/bin/passwd
echo 'zfs_enable="YES"' >> "/${RPOOL}/etc/rc.conf"
echo "Incantation completed"

echo 'LOADER_ZFS_SUPPORT="YES"' >> "/${RPOOL}/etc/src.conf"

cat <<"EOF">>"/${RPOOL}/makeloader"
#!/bin/sh
unset DESTDIR
cd /usr/src/sys/boot
make obj
make depend
make
cd $(uname -p)/loader
make install
cd /
EOF
if [ -n "${SRCLOADER}" ]; then
	mount -t devfs devfs "/${RPOOL}/dev"
	chroot "/${RPOOL}" /bin/sh /makeloader
	umount "/${RPOOL}/dev"
else
	cat /tmp/loader > "/${RPOOL}/boot/loader"
fi

echo "Installing ZFS pool information..."
mkdir /boot/zfs
zpool export "${RPOOL}" && zpool import "${RPOOL}"
cp "/boot/zfs/zpool.cache" "/${RPOOL}/boot/zfs/zpool.cache"
echo "Done"

echo "Setting root pool mountpoint..."
for i in usr/home usr var; do
	zfs umount "${RPOOL}/${i}" && zfs set mountpoint="/${i}" "${RPOOL}/${i}"
done
zfs umount "${RPOOL}" && set mountpoint=legacy "${RPOOL}"
zpool set bootfs="${RPOOL}" "${RPOOL}"
echo "Done"
