#!/bin/bash
# 
# Copyright (c) 2015-2016, Gregory M. Kurtzer. All rights reserved.
# 
# "Singularity" Copyright (c) 2016, The Regents of the University of California,
# through Lawrence Berkeley National Laboratory (subject to receipt of any
# required approvals from the U.S. Dept. of Energy).  All rights reserved.
# 
# If you have questions about your rights to use or distribute this software,
# please contact Berkeley Lab's Innovation & Partnerships Office at
# IPO@lbl.gov.
# 
# NOTICE.  This Software was developed under funding from the U.S. Department of
# Energy and the U.S. Government consequently retains certain rights. As such,
# the U.S. Government has been granted for itself and others acting on its
# behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software
# to reproduce, distribute copies to the public, prepare derivative works, and
# perform publicly and display publicly, and to permit other to do so. 
# 
# 


prefix="/usr"
exec_prefix="/usr"
libexecdir="/usr/lib"
sysconfdir="/etc"

SINGULARITY_version="2.0"
SINGULARITY_libexecdir="$libexecdir"
SINGULARITY_sysconfdir="$sysconfdir"

RETVAL=0
MESSAGELEVEL=1
DEBUG=""

export SINGULARITY_sysconfdir SINGULARITY_libexecdir RETVAL MESSAGELEVEL DEBUG

# the Modules package always calls 
unset module

if [ -f "$SINGULARITY_libexecdir/singularity/functions" ]; then
    . "$SINGULARITY_libexecdir/singularity/functions"
else
    echo "Error loading functions: $SINGULARITY_libexecdir/singularity/functions"
    exit 1
fi

usage() {
    echo "USAGE: $0 (options) [command] (command options)"
    echo
    echo "GLOBAL OPTIONS:"
    echo "    -h --help     Display this usage summary"
    echo "       --version  Show application version"
    echo "    -v --verbose  Increase verbosity +1"
    echo "    -d --debug    Show debugging information"
    echo "    -q --quiet    Only print errors"
    echo
    echo "COMMANDS:"

    ls $SINGULARITY_libexecdir/singularity/cli/*.summary | sed -e 's/.summary//' | sort | while read i; do
        if [ -f "$i.summary" ]; then
            NAME=`basename "$i"`
            SUMMARY=`cat "$i.summary"`
            printf "    %-10s %s\n" "$NAME" "$SUMMARY"
        fi
    done

    echo
    echo "For any additional help or support visit the Singularity"
    echo "website: http://gmkurtzer.github.io/singularity"
    echo
}

while true; do
    case $1 in
        -h|--help)
            usage
            exit
        ;;
        -q|--quiet)
            MESSAGELEVEL=0
            shift 
        ;;
        --version)
            message 1 "$SINGULARITY_version\n"
            exit
        ;;
        -d|--debug)
            MESSAGELEVEL=5
            DEBUG=1
            message 4 "enabling debugging\n"
            #set -x
            shift
        ;;
        -v|--verbose)
            MESSAGELEVEL=`expr $MESSAGELEVEL + 1`
            message 2 "increasing verbosity level ($MESSAGELEVEL)\n"
            shift
        ;;
        -*)
            message ERROR "Unknown argument: $1\n"
            exit 1
        ;;
        *)
            message 4 "ending argument loop\n"
            break
        ;;
    esac
done

COMMAND="$1"

if [ -z "$COMMAND" ]; then
    usage
    exit 0
fi

shift

if [ -x "$SINGULARITY_libexecdir/singularity/cli/$COMMAND.exec" ]; then
    exec $SINGULARITY_libexecdir/singularity/cli/$COMMAND.exec "$@"
else
    message ERROR "Unknown command '$COMMAND'\n"
    exit 1
fi

# We should never get here...
exit 255
