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

# Copyright © 2013, 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 "${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 "${DATAPOOL}" ]; then
  echo "${0}: you must set the environment variable DATAPOOL to the name of your data pool" >/dev/stderr
  echo "${0}: e.g. export DATAPOOL=zdata" >/dev/stderr
  echo "${0}: your data pool can be the same as your root pool" >/dev/stderr
  echo "${0}: e.g. export DATAPOOL=\${ROOTPOOL}" >/dev/stderr
  exit 69
fi

if [ "${DATAPOOL}" != "${ROOTPOOL}" ]; then
  if ! zpool list "${DATAPOOL}" >/dev/null 2>/dev/null; then
    echo "${0}: data pool ${DATAPOOL} does not exist" >/dev/stderr
    echo "${0}: try: zpool create -o cachefile=/tmp/zpool.cache -O mountpoint=legacy ${DATAPOOL} ..." >/dev/stderr
    exit 69
  fi
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

cd /

umount ${DESTDIR}/dev

zfs set readonly=on ${ROOTPOOL}/var/empty

zfs unmount -a

zfs inherit mountpoint ${ROOTPOOL}/ROOT/${RELEASEDATE}-r${RELEASEREV}

if [ -n "${BOOTPOOL}" ]; then
  zfs set -u mountpoint=/bootpool ${BOOTPOOL}/BOOT/${RELEASEDATE}-r${RELEASEREV}
fi

zfs set -u mountpoint=/home  ${DATAPOOL}/home
zfs set -u mountpoint=/media ${ROOTPOOL}/media
zfs set -u mountpoint=/nfs   ${ROOTPOOL}/nfs
zfs set -u mountpoint=/tmp   ${ROOTPOOL}/tmp
zfs set -u mountpoint=/usr   ${ROOTPOOL}/usr
zfs set -u mountpoint=/var   ${ROOTPOOL}/var

if [ "${DATAPOOL}" != "${ROOTPOOL}" ]; then
  zfs set -u mountpoint=/usr ${DATAPOOL}/usr
  zfs set -u mountpoint=/var ${DATAPOOL}/var
fi

zfs unmount -a

# EOF
