#!/bin/sh
#-------------------------------------------------------------------------+
# Copyright (c) 2014-2015, iocage
# Copyright (c) 2016-2017, Bartek Rutkowski, iocell
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing 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 AUTHOR ``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 AUTHOR 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.

unset LC_ALL
unset LANG

PATH=${PATH}:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin

if [ "${1}" = "--version" ] || [ "${1}" = "version" ] ; then
    echo "iocell 2.1.2 (2017-06-17)"
    exit 0
fi

# Check if the system has ZFS available. That is required for iocell to work properly.
# Exit if it doesn't.
if [ "$(zpool list)" = 'no pools available' ] ; then
    echo "  ERROR: ZFS is required for iocell to work" \
        "and I can't find any pools available." >&2
    exit 1
fi

if [ "$(uname -K)" -lt "903000" ] ; then
    echo "  ERROR: Unsupported RELEASE found." \
        " Please upgrade to 9.3-RELEASE or later." >&2
    exit 1
fi

# Check if libs are available
if [ -e "./lib/ioc-common" ] ; then
    LIB="./lib"
elif [ -e "/usr/local/lib/iocell" ] ; then
    LIB="/usr/local/lib/iocell"
else
    echo "ERROR: missing libraries" >&2
    exit 1
fi

# Source the libs needed to use the script, include shellcheck directives
# shellcheck source=lib/ioc-cmd
. "${LIB}/ioc-cmd"
# shellcheck source=lib/ioc-globals
. "${LIB}/ioc-globals"
# shellcheck source=lib/ioc-info
. "${LIB}/ioc-info"
# shellcheck source=lib/ioc-help
. "${LIB}/ioc-help"
# shellcheck source=lib/ioc-common
. "${LIB}/ioc-common"
# shellcheck source=lib/ioc-zfs
. "${LIB}/ioc-zfs"
# shellcheck source=lib/ioc-send
. "${LIB}/ioc-send"
# shellcheck source=lib/ioc-snapshot
. "${LIB}/ioc-snapshot"
# shellcheck source=lib/ioc-upgrade
. "${LIB}/ioc-upgrade"
# shellcheck source=lib/ioc-image
. "${LIB}/ioc-image"
# shellcheck source=lib/ioc-common
. "${LIB}/ioc-common"
# shellcheck source=lib/ioc-rc
. "${LIB}/ioc-rc"
# shellcheck source=lib/ioc-network
. "${LIB}/ioc-network"
# shellcheck source=lib/ioc-rctl
. "${LIB}/ioc-rctl"
# shellcheck source=lib/ioc-configure
. "${LIB}/ioc-configure"
# shellcheck source=lib/ioc-hacks
. "${LIB}/ioc-hacks"
# shellcheck source=lib/ioc-git
. "${LIB}/ioc-git"

if [ -z "$1" ] ; then
    __usage
    exit 0
fi

if [ "$1" = "-v" ] ; then
    set -x
    shift
fi

if [ "$1" = "help" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ] ; then
    __help
    exit 0
fi

if [ "$(__readonly_cmd "$1")" != "0" ] && [ "$(id -un)" != "root" ] ; then
    echo "* The $1 command needs root credentials!" >&2
    exit 1
fi

if [ ! -e "/usr/local/lib/libucl.so" ] || [ ! -e "/usr/local/bin/uclcmd" ] ; then
    echo "* Please install all dependencies!"
    exit 1
fi

# Work around zpool activation chicken-egg problem
# (when activating a pool don't look for an existing one)
# Also look for an altroot and if it exists change lib/ioc-globals to that value
if [ "$1" != "activate" ] ; then
    _pool=$(__get_default_prop pool)
    export pool="${_pool}"

    if [ "${pool}" = "null" ] || [ "${pool}" = "none" ]; then
        _found="0"
        for _pool in $(zpool list -H -o name) ; do
            _mpool=$(zpool get -H comment "${_pool}" | awk '{print $3}')

            if [ "${_mpool}" = "iocell" ] ; then
                _found="1"
                __activate "${_pool}"
                break
            fi
        done
      if [ "${_found}" = "0" ] ; then
          __die "you need to activate a pool!"
      fi
    fi

    __check_filesystems
fi

__parse_cmd "$@"
