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

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

if [ ! -d "${DESTDIR}" ]; then
  mkdir -p "${DESTDIR}"

  if [ ! -d "${DESTDIR}" ]; then
    echo "${0}: unable to create destination directory ${DESTDIR}" >/dev/stderr
    exit 69
  fi
fi

cd /

zfs set mountpoint=${DESTDIR} ${ROOTPOOL}/ROOT/${RELEASEDATE}-r${RELEASEREV}

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

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

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

mkdir -p ${DESTDIR}/boot/opt

mkdir -p ${DESTDIR}/dev

mkdir -p ${DESTDIR}/media/a
mkdir -p ${DESTDIR}/media/cdrom
mkdir -p ${DESTDIR}/media/dvdrom
mkdir -p ${DESTDIR}/media/dosimage
mkdir -p ${DESTDIR}/media/dosstick
mkdir -p ${DESTDIR}/media/floppy
mkdir -p ${DESTDIR}/media/isoimage
mkdir -p ${DESTDIR}/media/ntfsstick
mkdir -p ${DESTDIR}/media/udfimage
mkdir -p ${DESTDIR}/media/ufsimage

mkdir -p ${DESTDIR}/proc

mkdir -p ${DESTDIR}/usr/compat/linux/dev
mkdir -p ${DESTDIR}/usr/compat/linux/dev/fd
mkdir -p ${DESTDIR}/usr/compat/linux/proc
mkdir -p ${DESTDIR}/usr/compat/linux/sys

mkdir -p ${DESTDIR}/var/log/bareos
touch ${DESTDIR}/var/log/bareos/bareos-audit.log
touch ${DESTDIR}/var/log/bareos/bareos.log

mkdir -p ${DESTDIR}/var/synth/builders/synth-live

chmod 1777 ${DESTDIR}/tmp
chmod 1777 ${DESTDIR}/var/mail # mail/alpine recommends this mode
chmod 1777 ${DESTDIR}/var/tmp

chown -R 302:302 ${DESTDIR}/var/cache/netdata
chown -R 997:997 ${DESTDIR}/var/db/bareos
chown -R 106:106 ${DESTDIR}/var/db/clamav
chown -R 800:800 ${DESTDIR}/var/db/clickhouse
chown -R 112:112 ${DESTDIR}/var/db/dcc
chown -R   2:5   ${DESTDIR}/var/db/entropy
chown -R 904:904 ${DESTDIR}/var/db/grafana7
chown -R 907:907 ${DESTDIR}/var/db/influxdb
chown -R 302:302 ${DESTDIR}/var/db/netdata
chown -R 288:288 ${DESTDIR}/var/db/ntopng
chown -R 478:478 ${DESTDIR}/var/db/prometheus
chown -R 125:0   ${DESTDIR}/var/db/postfix
chown -R 770:770 ${DESTDIR}/var/db/postgres
chown -R 535:535 ${DESTDIR}/var/db/redis
chown -R 997:997 ${DESTDIR}/var/log/bareos
chown -R 842:842 ${DESTDIR}/var/munin
chown -R   0:309 ${DESTDIR}/var/ossec
chown -R 997:997 ${DESTDIR}/var/spool/bareos
chown -R 200:200 ${DESTDIR}/var/spool/sympa
chown -R 100:100 ${DESTDIR}/var/squid

chmod -R 0750 ${DESTDIR}/var/cache/netdata
chmod -R 0700 ${DESTDIR}/var/db/bareos
chmod -R 0700 ${DESTDIR}/var/db/entropy
chmod -R 0750 ${DESTDIR}/var/db/netdata
chmod -R 0700 ${DESTDIR}/var/db/postfix
chmod -R 0700 ${DESTDIR}/var/db/postgres/data*
chmod    0750 ${DESTDIR}/var/db/redis
chmod    0750 ${DESTDIR}/var/log/bareos
chmod    0640 ${DESTDIR}/var/log/bareos/bareos*.log
chmod -R 0700 ${DESTDIR}/var/spool/bareos
chmod -R 0750 ${DESTDIR}/var/squid

ln -s usr/compat ${DESTDIR}/compat

mount -t devfs devfs ${DESTDIR}/dev

# EOF
