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

# Copyright © 2013, 2020, 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.

# Where the .txz distribution files are located on the boot media.
DISTDIR=/usr/freebsd-dist

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 "${ROOTPOOL}" ]; then
  echo "${0}: you must set the environment variable ROOTPOOL to the name of your root pool" >/dev/stderr
  echo "${0}: e.g. export ROOTPOOL=zroot" >/dev/stderr
  exit 69
fi

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

if [ -z "${RELEASEDATE}" ]; then
  echo "${0}: you must set the environment variable RELEASEDATE to the date of the release you are installing" >/dev/stderr
  echo "${0}: e.g. export RELEASEDATE=20130602" >/dev/stderr
  exit 69
fi

if [ -z "${RELEASEREV}" ]; then
  echo "${0}: you must set the environment variable RELEASEREV to the Subversion revision number of the release you are installing" >/dev/stderr
  echo "${0}: e.g. export RELEASEREV=251259" >/dev/stderr
  exit 69
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 ${DESTDIR}

if [ -n "${BOOTPOOL}" ]; then
  cd ${DESTDIR}/bootpool
  tar xpvvf ${DISTDIR}/base.txz ./boot
  tar xpvvf ${DISTDIR}/kernel.txz ./boot

  cd ${DESTDIR}
  tar xpvvf ${DISTDIR}/base.txz --exclude ./boot
  tar xpvvf ${DISTDIR}/kernel.txz --exclude ./boot

  ln -s bootpool/boot boot
else
  tar xpvvf ${DISTDIR}/base.txz
  tar xpvvf ${DISTDIR}/kernel.txz
fi

if [ -f ${DISTDIR}/kernel-dbg.txz ]; then
  tar xpvvf ${DISTDIR}/kernel-dbg.txz
fi

tar xpvvf ${DISTDIR}/doc.txz
tar xpvvf ${DISTDIR}/games.txz
tar xpvvf ${DISTDIR}/lib32.txz

if [ -n "${BOOTPOOL}" ]; then
  rootfs="${ROOTPOOL}/ROOT/${RELEASEDATE}-r${RELEASEREV}"

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

# EOF
