#!@l_prefix@/lib/openpkg/bash
##
##  uuid -- OpenPKG UUID Update Utility
##  Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>
##
##  This software is property of the OpenPKG GmbH, DE MUC HRB 160208.
##  All rights reserved. Licenses which grant limited permission to use,
##  copy, modify and distribute this software are available from the
##  OpenPKG GmbH.
##
##  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  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.
##

#   configuration
prefix="@l_prefix@"
musr="@l_musr@"
mgrp="@l_mgrp@"

#   minimum command line parsing
opt_v=no
while [ 1 ]; do
    case "$1" in
        -v | --verbose ) opt_v=yes; shift ;;
        * ) break ;;
    esac
done

#   determine temporary directory
tmpdir="${TMPDIR-/tmp}"

#   helper function for checking the signature
valid_signature () {
    script="%{lua: print(rpm.signature(\"$1\", nil,"
    script="$script \"$prefix/etc/openpkg/openpkg.com.pgp\","
    script="$script \"7D121A8FC05DC18A4329E9EF67042EC961B7AE34\")) }"
    result=`$prefix/bin/openpkg rpm --eval "$script" 2>/dev/null || true`
    if [ ".$result" == .true ]; then
        return 0
    else
        return 1
    fi
}

#   display usage help
do_help () {
    echo "openpkg license help .......................... show usage help (this text)"
    echo "openpkg license install <id> <file>|<url>|- ... install a new license"
    echo "openpkg license update <id> <file>|<url>|- .... update an installed license"
    echo "openpkg license uninstall <id> ................ uninstall an installed license"
    echo "openpkg license activate <id> ................. activate an installed license"
    echo "openpkg license view <id> ..................... view an installed license"
    echo "openpkg license list .......................... list all installed licenses"
    echo "openpkg license active ........................ list the currently activated license"
    echo "openpkg license sanity ........................ sanity check installed license"
}

#   install license
do_install () {
    if [ $# -ne 2 ]; then
        echo "openpkg:license:USAGE: openpkg license install <id> <file>|-" 1>&2
        exit 1
    fi
    id="$1"
    src="$2"
    case "$src" in
        http:* | https:* | ftp:* )
            tmp="$tmpdir/openpkg.license.txt"
            ( rm -f $tmp >/dev/null 2>&1 || true
              set -o noclobber
              $prefix/lib/openpkg/curl -s -q -f -L $src >$tmp
            ) || exit $?
            src="$tmp"
            ;;
        "-" )
            src="$tmpdir/openpkg.license.txt"
            ( rm -f $src >/dev/null 2>&1 || true
              set -o noclobber
              cat >$src
            ) || exit $?
            ;;
    esac
    if [ -f $prefix/etc/openpkg/license.d/$id ]; then
        echo "openpkg:license:ERROR: license with id \"$id\" already exists -- uninstall first" 1>&2
        exit 1
    fi
    if ! valid_signature $src; then
        echo "openpkg:license:ERROR: invalid signature on license file" 1>&2
        exit 1
    fi
    if [ ! -w $prefix/etc/openpkg/license.d ]; then
        echo "openpkg:license:ERROR: unable to store signature -- permission problems?" 1>&2
        exit 1
    fi
    cat $src >$prefix/etc/openpkg/license.d/$id || exit $?
    chown $musr:$mgrp $prefix/etc/openpkg/license.d/$id >/dev/null 2>&1 || true
}

#   update license
do_update () {
    if [ $# -ne 2 ]; then
        echo "openpkg:license:USAGE: openpkg license update <id> <file>|-" 1>&2
        exit 1
    fi
    id="$1"
    src="$2"
    case "$src" in
        http:* | https:* | ftp:* )
            tmp="$tmpdir/openpkg.license.txt"
            ( rm -f $tmp >/dev/null 2>&1 || true
              set -o noclobber
              $prefix/lib/openpkg/curl -s -q -f -L $src >$tmp
            ) || exit $?
            src="$tmp"
            ;;
        "-" )
            src="$tmpdir/openpkg.license.txt"
            ( rm -f $src >/dev/null 2>&1 || true
              set -o noclobber
              cat >$src
            ) || exit $?
            ;;
    esac
    if [ ! -f $prefix/etc/openpkg/license.d/$id ]; then
        echo "openpkg:license:ERROR: license with id \"$id\" does not exist -- install first to update" 1>&2
        exit 1
    fi
    if ! valid_signature $src; then
        echo "openpkg:license:ERROR: invalid signature on license file" 1>&2
        exit 1
    fi
    if [ ! -w $prefix/etc/openpkg/license.d ]; then
        echo "openpkg:license:ERROR: unable to store signature -- permission problems?" 1>&2
        exit 1
    fi
    cat $src >$prefix/etc/openpkg/license.d/$id || exit $?
    chown $musr:$mgrp $prefix/etc/openpkg/license.d/$id >/dev/null 2>&1 || true
}

#   uninstall license
do_uninstall () {
    if [ $# -ne 1 ]; then
        echo "openpkg:license:USAGE: openpkg license uninstall <id>" 1>&2
        exit 1
    fi
    id="$1"
    if [ ! -f $prefix/etc/openpkg/license.d/$id ]; then
        echo "openpkg:license:ERROR: no license under id \"$id\" installed" 1>&2
        exit 1
    fi
    id_active="`cat $prefix/etc/openpkg/license`"
    if [ ".$id" = ".$id_active" ]; then
        echo "openpkg:license:ERROR: license under id \"$id\" still activated -- activate a different one first" 1>&2
        exit 1
    fi
    rm -f $prefix/etc/openpkg/license.d/$id
    if [ $? -ne 0 ]; then
        echo "openpkg:license:ERROR: failed to uninstall license" 1>&2
        exit 1
    fi
}

#   activate license
do_activate () {
    if [ $# -ne 1 ]; then
        echo "openpkg:license:USAGE: openpkg license activate <id>" 1>&2
        exit 1
    fi
    id="$1"
    if [ ! -f $prefix/etc/openpkg/license.d/$id ]; then
        echo "openpkg:license:ERROR: no license under id \"$id\" installed" 1>&2
        exit 1
    fi
    id_active="`cat $prefix/etc/openpkg/license`"
    if [ ".$id" = ".$id_active" ]; then
        echo "openpkg:license:ERROR: license id \"$id\" is already activated" 1>&2
        exit 1
    fi
    echo "$id" >$prefix/etc/openpkg/license
    if [ $? -ne 0 ]; then
        echo "openpkg:license:ERROR: failed to activate license under id \"$id\"" 1>&2
        exit 1
    fi
}

#   view license
do_view () {
    if [ $# -ne 1 ]; then
        echo "openpkg:license:USAGE: openpkg license view <id>" 1>&2
        exit 1
    fi
    id="$1"
    if [ ! -f $prefix/etc/openpkg/license.d/$id ]; then
        echo "openpkg:license:ERROR: no license under id \"$id\" installed" 1>&2
        exit 1
    fi
    viewer=""
    for name in less more cat; do
        for dir in `echo $PATH | sed -e 's;:; ;g'`; do
            if [ -x $dir/$name ]; then
                viewer="$dir/$name"
                break
            fi
        done
        if [ ".$viewer" != . ]; then
            break
        fi
    done
    eval $viewer $prefix/etc/openpkg/license.d/$id
}

#   list license
do_list () {
    if [ $# -ne 0 ]; then
        echo "openpkg:license:USAGE: openpkg license list" 1>&2
        exit 1
    fi
    id_active="`cat $prefix/etc/openpkg/license`"
    for file in `cd / && find $prefix/etc/openpkg/license.d -type f -print | sort`; do
        id=`echo $file | sed -e "s;^$prefix/etc/openpkg/license.d/;;"`
        status="-"
        if [ ".$id" = ".$id_active" ]; then
            status="+"
        fi
        echo . | awk '{ printf("%s %s\n", status, id); }' id="$id" status="$status"
    done
}

#   list currently activate license
do_active () {
    if [ $# -ne 0 ]; then
        echo "openpkg:license:USAGE: openpkg license active" 1>&2
        exit 1
    fi
    cat $prefix/etc/openpkg/license
}

#   sanity-check license
do_sanity () {
    if [ $# -ne 0 ]; then
        echo "openpkg:license:USAGE: openpkg license sanity" 1>&2
        exit 1
    fi
    id="`cat $prefix/etc/openpkg/license`"
    if [ ! -f $prefix/etc/openpkg/license.d/$id ]; then
        echo "openpkg:license:ERROR: active license with id \"$id\" does not exist" 1>&2
        exit 1
    fi
    if ! valid_signature $prefix/etc/openpkg/license.d/$id; then
        echo "openpkg:license:ERROR: invalid signature on active license" 1>&2
        exit 1
    fi
}

#   Command Line Dispatching
cmd="$1"
shift
case "$cmd" in
    help      ) do_help      ${1+"$@"} ;;
    install   ) do_install   ${1+"$@"} ;;
    update    ) do_update    ${1+"$@"} ;;
    uninstall ) do_uninstall ${1+"$@"} ;;
    activate  ) do_activate  ${1+"$@"} ;;
    view      ) do_view      ${1+"$@"} ;;
    list      ) do_list      ${1+"$@"} ;;
    active    ) do_active    ${1+"$@"} ;;
    sanity    ) do_sanity    ${1+"$@"} ;;
    ""        ) echo "openpkg:license:ERROR: no command given (use \"help\" for usage)" 1>&2; exit 1 ;;
    *         ) echo "openpkg:license:ERROR: invalid command \"$cmd\" (use \"help\" for usage)" 1>&2; exit 1 ;;
esac

