#!/bin/sh

# See whether given command is readonly
# and therefore doesn't need root credentials
__readonly_cmd () {
    case "$1" in
        defaults|df|get|list|show|snaplist) echo "0"
            ;;
        *) echo "1"
            ;;
    esac
}

# Process command line options-------------------------
__parse_cmd () {
    while [ $# -gt 0 ] ; do
        case "$1" in
            activate)   __activate "$2"
                        exit
                ;;
            cap)        __rctl_limits "$2"
                        exit
                ;;
            clean)      __clean "$2" "$3"
                        exit
                ;;
            clone)      __export_props "$@"
                        __clone_jail "$2"
                        exit
                ;;
            console)    export _force=0
                        if [ "$2" = "-f" ] ; then
                            export _force=1
                            shift
                        fi
                        __console "$2"
                        exit
                ;;
            create)     local _tag _hostname _ip4if _ip4addr _ip4subnet _ip4ip
                        __export_props "$@"
                        _hostname="${host_hostname}"
                        _tag="${tag}"
                        _ip4if="$(echo ${ip4_addr} | awk 'BEGIN { FS = "|" } \
                            ; { print $1 }')"
                        _ip4addr="$(echo ${ip4_addr} | \
                            awk 'BEGIN { FS = "|" } \
                            ; { gsub("\/[0-9]+","") ; print $2 }')"
                        _ip4subnet="$(echo ${ip4_addr} | \
                            awk 'BEGIN { FS = "/" } \
                            ; { print $2 }')"

                        for _i in $(seq ${count}) ; do
                            unset uuid host_hostuuid host_hostname
                            uuid="$(uuidgen)"
                            host_hostuuid="${uuid}"

                            if [ "${count}" -eq 1 ] ; then
                                tag="${_tag}"
                                if [ -n "${iocset_host_hostname}" ] ; then
                                    host_hostname="${_hostname}"
                                else
                                    host_hostname="${uuid}"
                                fi
                            elif [ "${count}" -gt 1 ] ; then
                                tag="${_tag}_${_i}"
                                if [ -n "${iocset_host_hostname}" ] ; then
                                    host_hostname="${_hostname}_${_i}"
                                else
                                    host_hostname="${uuid}"
                                fi

                                if [ "${ip4_addr}"  != "none" ] ; then
                                    if [ "${_i}" = 1 ] ; then
                                        _ip4ip="$(echo ${_ip4addr} | \
                                            cut -d "." -f 4)"
                                    else
                                        _ip4ip="$(expr ${_ip4ip} + 1)"
                                        ip4_addr="${_ip4if}|$(echo ${_ip4addr} | \
                                            cut -d "." -f 1-3).${_ip4ip}/${_ip4subnet}"
                                    fi
                                fi
                            fi

                            __create_jail "$@"
                        done
                        exit
                ;;
            deactivate) __deactivate "$2"
                        exit
                ;;
            defaults)   __print_defaults
                        exit
                ;;
            destroy)    if [ "$2" = "-f" ] ; then
                            export _force=1
                            shift 2
                            for _jdel in $@ ; do
                                __destroy_jail "${_jdel}"
                                unset jail_datasets uuid jail_zfs_dataset _fs \
                                      _jail_type
                                export jail_datasets="$(zfs list -d3 -rH -o \
                                       name ${pool}/iocell | egrep -v \
                                       "jails$|releases$|templates$|download|${pool}/iocell$|/root$")"
                            done
                        else
                            export _force=0
                            shift
                            for _jdel in $@ ; do
                                __destroy_jail "${_jdel}"
                                unset jail_datasets uuid jail_zfs_dataset _fs \
                                      _jail_type
                                export jail_datasets="$(zfs list -d3 -rH -o \
                                       name ${pool}/iocell | egrep -v \
                                       "jails$|releases$|templates$|download|${pool}/iocell$|/root$")"
                            done
                        fi

                        exit
                ;;
            df)         __print_disk
                        exit
                ;;
            exec)       shift
                        __exec "$@"
                        exit
                ;;
            export)     __export "$2"
                        exit
                ;;
            fetch)      __export_props "$@"
                        local _gitcheck=$(echo $* | grep -v "ftp" | \
                                        grep -q "/" ; echo $?)

                        # Check if the user is specifying a git plugin and also
                        # check to see if they want a thick plugin
                        if [ "${_gitcheck}" = "0" ] ; then
                            shift
                            if [ "$1" = "-t" ] ; then
                                shift
                                __fetch_plugin_thick "$@"
                                exit 0
                            else
                                __fetch_plugin "$@"
                                exit 0
                            fi
                        fi

                        __fetch_release "$@"
                        exit
                ;;
            get)        __get_jail_prop "$2" "$3"
                        exit
                ;;
            help)       __help
                        exit
                ;;
            init-host)  __init_host "$2" "$3"
                        exit
                ;;
            inuse)      __rctl_inuse "$2"
                        exit
                ;;
            import)     shift
                        __import "$@"
                        exit
                ;;
            limits)     __rctl_list "$2"
                        exit
                ;;
            list)       __list_jails "$2" "$3"
                        exit
                ;;
            promote)    __promote "$2"
                        exit
                ;;
            pull)       __pull "$2"
                        exit
                ;;
            push)       __push "$2"
                        exit
                ;;
            rcboot)     __rc_jails boot
                        exit
                ;;
            rcshutdown) __rc_jails shutdown
                        exit
                ;;
            reboot|restart)    shift
                               if [ "$1" = "-s" ] ; then
                                    shift
                                    for _j in $@ ; do
                                        __restart_jail "${_j}"
                                    done
                                    exit
                               else
                                    for _j in $@ ; do
                                        __reboot_jail "${_j}"
                                    done
                                    exit
                                fi
                ;;
            reset)      __reset_jail_props "$2"
                        exit
                ;;
            rollback)   __rollback "$2"
                        exit
                ;;
            runtime)    __runtime "$2"
                        exit
                ;;
            send)       shift
                        __send "$@"
                        exit
                ;;
            set)        __export_props "$@"
                        shift
                        # Big thanks to john@ix for his huge help with this
                        local _nprops _props _njails _jails _var _pair _i \
                              _jail _fulluuid _j _prop _pname _pval _dataset

                        _nprops=0
                        _njails=0

                        for _jset in "$@" ; do
                            _var="$(echo ${_jset} | cut -f1 -d'=' -s)"

                            if [ -n "${_var}" ] ; then
                                _pair="_prop_${_nprops}='${_jset}'"
                                eval "${_pair}"
                                _nprops=$((_nprops + 1))
                                continue
                            fi

                            eval "_jail_${_njails}=${_jset}"
                            _njails=$((_njails + 1))
                        done

                        if [ "${_njails}" = "0" ] ; then
                            __die "please specify a jail!"
                        fi

                        _i=0
                        while [ "${_i}" -lt "${_njails}" ] ; do
                            _var=\$$(printf "_jail_${_i}")
                            _jail=$(eval "echo ${_var}")
                            _dataset=$(__find_jail ${_jail}) || exit $?
                            _fulluuid="$(__check_name ${_jail} ${_dataset})"
                            _j=0

                            while [ "${_j}" -lt "${_nprops}" ] ; do
                                _var=\$$(printf "_prop_${_j}")
                                _prop=$(eval "echo ${_var}")
                                _pname=$(echo "${_prop}" | cut -f1 -d'=' -s)
                                _pval=$(echo "${_prop}" | cut -f2 -d'=' -s)

                                __set_jail_prop "${_pname}=${_pval}" \
                                    "${_fulluuid}"
                                _j=$((_j + 1))
                            done

                            _i=$((_i + 1))
                        done
                        exit
                ;;
            snaplist)   __snaplist "$2"
                        exit
                ;;
            snapmount)  __mount_snapshot "$2" "$3"
                        exit
                ;;
            snapremove) __snapremove "$2"
                        exit
                ;;
            snapshot|snap)   if [ "$2" = "-f" ] ; then
                            export _force=1
                            shift
                        fi

                        __snapshot "$2" "$3"
                        exit
                ;;
            snapumount) __umount_snapshot "$2"
                        exit
                ;;
            start)      if [ "$2" = "-f" ] ; then
                            export _force=1
                            shift 2
                        else
                            export _force=0
                            shift
                        fi

                        for _j in $@ ; do
                            __start_jail "${_j}"
                        done
                        exit
                ;;
            stop)       shift
                        for _j in $@ ; do
                            __stop_jail "${_j}"
                        done
                        exit
                ;;
            uncap)      __rctl_uncap "$2"
                        exit
                ;;
            update)     __update "$2" "$3"
                        exit
                ;;
            upgrade)    __export_props "$@"
                        shift
                        __upgrade "$@"
                        exit
                ;;
            pkg)        __pkg_wrapper "$@"
                        exit
                ;;
            *)          __usage
                        exit
                ;;
        esac
        shift
    done
}
