#! /bin/bash
# vim: fdm=marker et

. shell-args

# function show_help {{{
show_help()
{
    cat <<EOF

mkve-template - create a skeleton template for mkve-bundle

Usage: $PROG [options] <template>

Options:
  --hypervisor=HYPERVISOR   hypervisor (type of virtualization: kvm, openvz);
  --packages=PACKAGES       list of packages separated by whitespaces and/or commas;
  --fpackages=FILES         list of files containing lists of packages;
  --append                  <template> already supports another hypervisor;
  --force                   remove <template> if it already exists;
  -q, --quiet               try to be more quiet;
  -v, --verbose             try to be more verbose;
  -V, --version             print program version and exit;
  -h, --help                show help text and exit.

See mkve-template(1) for more verbose help.
EOF

    exit 0
}
# }}}
# function check_variables {{{

# function check_template {{{

# This function checks that our's argument is a valid writable directory
check_template()
{
    [ -n "$template" ] ||
        fatal "template is not specified"

    if [ -z "$append" ]; then
        if [ -d "$template" ]; then
            if [ -n "$force" ]; then
                rm -rf "$template" ||
                    fatal "can't remove '$template' directory"
            else
                fatal "$template directory already exists"
            fi
        fi

        mkdir -p "$template" ||
        fatal "can't create directory $template"
    else
        [ -d "$template" ] ||
            fatal "--append is specified, but there is no $template directory"
    fi

    verbose "using template '$template'"
}
# }}}
# function check_hypervisor {{{

# This function checks that it's argument is a valid hypervisor
check_hypervisor()
{
    [ -n "$hypervisor" ] ||
        fatal "hypervisor is not specified"

    if [ -n "$append" ]; then
        egrep -q "^$hypervisor$" "$template/hypervisors" &&
            fatal "already supported hypervisor: $hypervisor"
    fi

    verbose "using hypervisor '$hypervisor'"
}
# }}}

check_variables()
{
    check_template
    check_hypervisor
}
# }}}
# function create_hypervisors {{{
create_hypervisors()
{
    echo "$hypervisor" >>"$template/hypervisors" ||
        fatal "can't write to $template/hypervisors"
}
# }}}
# function create_version {{{
create_version()
{
    mkdir -p "$template/versions"
    echo "template_version=0.01" >"$template/versions/$hypervisor" ||
        fatal "can't write to $template/versions/$hypervisor"
}
# }}}
# function create_packages_list {{{
create_packages_list()
{
    mkdir -p "$template/packages-lists"
    true >"$template/packages-lists/$hypervisor"

    for file in $fpackages; do
        packages="$packages $(cat "$file" | tr '\n' ' ')" ||
            fatal "can't read file $file"
    done

    for pack in $(echo $packages | tr ' ' '\n' | sort -u); do
        echo "$pack" >>"$template/packages-lists/$hypervisor" ||
            fatal "can't write to $template/packages-lists/$hypervisor"
    done
}
# }}}

append=
force=
hypervisor=
packages=
fpackages=

TEMP=$(getopt -o ${getopt_common_opts} -n "$PROG" --long hypervisor:,packages:,fpackages:,append,force,${getopt_common_longopts} -- "$@")
eval set -- "${TEMP}"
while true; do
    case "$1" in
        --hypervisor)
            shift; hypervisor="$1"
            ;;
        --packages)
            shift; packages="${1//,/ }"
            ;;
        --fpackages)
            shift; fpackages="${1//,/ }"
            ;;
        --force)
            force='yes'
            ;;
        --append)
            append='yes'
            ;;
        --) shift; break
            ;;
        *)
            parse_common_option "$1"
            ;;
    esac
    shift
done

template="$1"

check_variables
create_hypervisors
create_version
create_packages_list
