#!/bin/sh
#
# HOWTO/script to create a USB mounted bootable "root" install for
# bootstrapping a root-on-ZFS and the like.
#
# -- Yarema <yds@CoolRat.org>

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
program=`basename $0`

if [ -f "$0.rc" ];then
  . "$0.rc"
fi

: ${RELEASE:="8.0-RELEASE"}
: ${DESTDIR:="/media"}

if [ $# -lt 2 ]; then
  echo "Usage:	${program} <dist> <vdev>"
  echo "	where <dist> is the path to the distribution"
  echo "	files, usually an .iso mounted on /dist"
  echo "	and <vdev> is usually da0"
  exit 1
fi

dist="$1"
vdev="$2"

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
ask() {
  local question default answer
  question=$1
  default=$2
  read -p "${question} [${default}]? " answer
  if [ -z "${answer}" ]; then
    answer=${default}
  fi
  echo ${answer}
}

yesno() {
  local question default answer
  question=$1
  default=$2
  while :; do
    answer=$(ask "${question}" "${default}")
    case "${answer}" in
    [Yy]*)	return 0;;
    [Nn]*)	return 1;;
    esac
    echo "Please answer yes or no."
  done
}

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
if yesno "Create one slice covering the entire ${vdev}" n; then
  dd if=/dev/zero of=/dev/${vdev} bs=512 count=79
  fdisk -BIv /dev/${vdev}
else
  exit 1
fi

if yesno "Initialize the BSD label" y; then
  bsdlabel -wB /dev/${vdev}s1
fi

bsdlabel /dev/${vdev}s1
while yesno "(Re)Edit the BSD label" n; do
  bsdlabel -e /dev/${vdev}s1
  bsdlabel /dev/${vdev}s1
done

newfs -U /dev/${vdev}s1a
[ -d ${DESTDIR} ] || mkdir -p ${DESTDIR}
mount /dev/${vdev}s1a ${DESTDIR}

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
: ${dists:="base games manpages info dict"}
for i in ${dists}; do
  echo "  Extracting the ${i} distribution into ${DESTDIR}..."
  cat ${dist}/${RELEASE}/${i}/${i}.??		| tar --unlink -xpzf - -C ${DESTDIR}
done

echo "  Extracting the GENERIC kernel into ${DESTDIR}/boot/"
cat ${dist}/${RELEASE}/kernels/generic.??	| tar --unlink -xpzf - -C ${DESTDIR}/boot && ln -f ${DESTDIR}/boot/GENERIC/* ${DESTDIR}/boot/kernel/

echo "  Copying ${dist}/${RELEASE}/ to ${DESTDIR}..."
tar cf - ${dist}/${RELEASE}/*	| tar --unlink -xpf - -C ${DESTDIR}

# Mounting /var on tmpfs will work for both tmp directories
# by creating a softlink from /tmp to /var/tmp
rm -rf ${DESTDIR}/tmp
ln -sf var/tmp ${DESTDIR}/tmp

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
if [ -f $0.local ];then
  . $0.local
fi

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
echo "nullfs_load=\"YES\"		# Null filesystem" >> ${DESTDIR}/boot/loader.conf.local
echo "zfs_load=\"YES\"			# ZFS" >> ${DESTDIR}/boot/loader.conf.local
echo "geom_eli_load=\"YES\"		# Disk encryption driver (see geli(8))" >> ${DESTDIR}/boot/loader.conf.local
echo "geom_mirror_load=\"YES\"		# RAID1 disk driver (see gmirror(8))" >> ${DESTDIR}/boot/loader.conf.local
${EDITOR:-"vi"} ${DESTDIR}/boot/loader.conf.local

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
echo "# Device		Mountpoint	FStype	Options			Dump	Pass#" > ${DESTDIR}/etc/fstab
echo "/dev/${vdev}s1a		/		ufs	rw,noatime		0	0" >> ${DESTDIR}/etc/fstab
echo "tmpfs			/var		tmpfs	rw,nosuid		0	0" >> ${DESTDIR}/etc/fstab
echo "proc			/proc		procfs	rw			0	0" >> ${DESTDIR}/etc/fstab
echo "# Device		Mountpoint	FStype	Options			Dump	Pass#" >> ${DESTDIR}/etc/fstab
echo "/dev/acd0		/cdrom		cd9660	ro,nodev,noauto,nosuid	0	0" >> ${DESTDIR}/etc/fstab
echo "/dev/fd0		/media/fd	ufs	rw,nodev,noauto,nosuid	0	0" >> ${DESTDIR}/etc/fstab
echo "/dev/fd0		/media/floppy	msdosfs	rw,nodev,noauto,nosuid	0	0" >> ${DESTDIR}/etc/fstab
${EDITOR:-"vi"} ${DESTDIR}/etc/fstab

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
umount /dev/${vdev}s1a

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
# EOF