#!/bin/sh
#
# HOWTO/script for creating ZFS filesystems using the same inputr
# file format as zfsboot. ${POOL} defaults to 'home', set the POOLr
# environment variable as appropriate. This script is usefull if you
# need to create more filesystems on the root zpool after installation
# with zfsboot.  Or if you create additional zpool(s) mounted on the
# root zpool.
#
#	Yarema <yds@CoolRat.org>

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

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

# The recommended <pool> name is simply the letter 'z' which makes output
# form mount(8), zfs(8M) and zpool(8M) easy to read for us humans.
: ${POOL:='home'}
DESTDIR="/${POOL}"

# file describing the ZFS datasets to create
: ${DATASETS:="$0.fs"}

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
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
}

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
  while read filesystem options; do
    case "${filesystem}" in
    /*)
      command="zfs create"
      for option in ${options};do
	command="${command} -o ${option}"
      done
      eval `echo "${command} ${POOL}${filesystem}" | sed -E 's/-<span class="createlink"><a href="http://yds.coolrat.org/ikiwiki.cgi?page=%3A__60__%3A&from=zfsboot%2Fzfscreate.sh&do=create" rel="nofollow">?</a>:<:</span>o<span class="createlink"><a href="http://yds.coolrat.org/ikiwiki.cgi?page=%3A__62__%3A&from=zfsboot%2Fzfscreate.sh&do=create" rel="nofollow">?</a>:>:</span><span class="createlink"><a href="http://yds.coolrat.org/ikiwiki.cgi?page=%3Aspace%3A&from=zfsboot%2Fzfscreate.sh&do=create" rel="nofollow">?</a>:space:</span>+-<span class="createlink"><a href="http://yds.coolrat.org/ikiwiki.cgi?page=%3A__60__%3A&from=zfsboot%2Fzfscreate.sh&do=create" rel="nofollow">?</a>:<:</span>V<span class="createlink"><a href="http://yds.coolrat.org/ikiwiki.cgi?page=%3A__62__%3A&from=zfsboot%2Fzfscreate.sh&do=create" rel="nofollow">?</a>:>:</span><span class="createlink"><a href="http://yds.coolrat.org/ikiwiki.cgi?page=%3Aspace%3A&from=zfsboot%2Fzfscreate.sh&do=create" rel="nofollow">?</a>:space:</span>+-<span class="createlink"><a href="http://yds.coolrat.org/ikiwiki.cgi?page=%3A__60__%3A&from=zfsboot%2Fzfscreate.sh&do=create" rel="nofollow">?</a>:<:</span>o<span class="createlink"><a href="http://yds.coolrat.org/ikiwiki.cgi?page=%3A__62__%3A&from=zfsboot%2Fzfscreate.sh&do=create" rel="nofollow">?</a>:>:</span>/-V/g'`
      zfs get mountpoint,compression,exec,setuid ${POOL}${filesystem}
      # Only set mountpoint for top level filesysytem
      # Let the child filesystem(s) inherit the moutpoint
      if echo "${filesystem}" | egrep '^/[^/]+$' > /dev/null 2>&1; then
	# Exclude volumes since they do not have the moutpoint property
	if echo "${command}" | egrep -v '<span class="createlink"><a href="http://yds.coolrat.org/ikiwiki.cgi?page=%3Aspace%3A&from=zfsboot%2Fzfscreate.sh&do=create" rel="nofollow">?</a>:space:</span>+-V<span class="createlink"><a href="http://yds.coolrat.org/ikiwiki.cgi?page=%3Aspace%3A&from=zfsboot%2Fzfscreate.sh&do=create" rel="nofollow">?</a>:space:</span>+' > /dev/null 2>&1; then
	  mountpoints="${mountpoints} ${filesystem}"
	fi
      fi
      ;;
    *)
      continue
      ;;
    esac
  done < ${DATASETS}

#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#===#
for mp in ${mountpoints}; do
  zfs set mountpoint=${mp} ${POOL}${mp}
done
zfs list

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