#!/bin/sh
# -*- coding: latin-1 -*-

# Copyright © 2013, 2018, Trond Endrestøl <Trond.Endrestol@ximalas.info>

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if [ -n "${BOOTPOOL}" ]; then
  if ! zpool list "${BOOTPOOL}" >/dev/null 2>/dev/null; then
    echo "${0}: boot pool ${BOOTPOOL} does not exist" >/dev/stderr
    echo "${0}: try: zpool create -o cachefile=/tmp/zpool.cache -O mountpoint=legacy ${BOOTPOOL} ..." >/dev/stderr
    exit 69
  fi
fi

if [ -z "${DESTDIR}" ]; then
  echo "${0}: you must set the environment variable DESTDIR to the destination directory" >/dev/stderr
  echo "${0}: e.g. export DESTDIR=/tmp/zroot" >/dev/stderr
  exit 69
fi

if [ ! -d "${DESTDIR}" ]; then
  echo "${0}: destination directory ${DESTDIR} does not exist" >/dev/stderr
  echo "${0}: maybe mkdir -p ${DESTDIR} needs to be run" >/dev/stderr
  exit 69
fi

if ! zfs get mountpoint "${DESTDIR}" >/dev/null 2>/dev/null; then
  echo "${0}: destination directory ${DESTDIR} is not a ZFS filesystem" >/dev/stderr
  exit 69
fi

cd /dist/8.*/base

echo "${0}: Please answer y to the following question!"
./install.sh
# Answer y

cd ../games && ./install.sh

cd ../info && ./install.sh

cd ../lib32 && ./install.sh

cd ../manpages && ./install.sh

cd ../kernels && ./install.sh generic

cd ${DESTDIR}/boot
rmdir kernel
mv GENERIC kernel

if [ -n "${BOOTPOOL}" ]; then
  cd ${DESTDIR}
  tar cf - boot | (cd bootpool; tar xvvf -)
  chflags -R 0 boot
  rm -Rf boot
  ln -s bootpool/boot boot

  if [ -n "${ROOTPOOL}" ]; then
    rootfs=`zpool get -Ho value bootfs ${ROOTPOOL} 2>/dev/null`
  fi

  # Blank value for ${rootfs} is acceptable.
  echo "${0}: do not forget to add the proper vfs.root.mountfrom=\"${rootfs}\" line in boot/loader.conf" >/dev/stderr
  echo "${0}: to avoid the kernel mounting the small boot pool as its root filesystem" >/dev/stderr
fi

# EOF
