#!/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.
# 
# This software is licensed under a customized 3-clause BSD license.  Please
# consult LICENSE file distributed with the sources of this project regarding
# your rights to use or distribute this software.
# 
# 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/local"
exec_prefix="${prefix}"
libexecdir="${exec_prefix}/libexec"
sysconfdir="${prefix}/etc"
localstatedir="${prefix}/var"


SINGULARITY_version="2.2"
SINGULARITY_libexecdir="$libexecdir"
SINGULARITY_sysconfdir="$sysconfdir"
SINGULARITY_localstatedir="$localstatedir"

RETVAL=0
MESSAGELEVEL=1
NEW_HOME=""
CHANGE_HOME="NO"

export SINGULARITY_sysconfdir SINGULARITY_libexecdir SINGULARITY_localstatedir RETVAL MESSAGELEVEL NEW_HOME CHANGE_HOME

# 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

while true; do
    case ${1:-} in
        -h|--help|help)
            if [ -z "${2:-}" ]; then
                C="singularity"
            else
                C="${2:-}"
            fi
            if [ -e "$SINGULARITY_libexecdir/singularity/cli/$C.help" ]; then
                cat "$SINGULARITY_libexecdir/singularity/cli/$C.help"
            else
                message ERROR "No help exists for command: '$C'\n"
                exit 1
            fi
            exit
        ;;
        -q|--quiet)
            MESSAGELEVEL=0
            shift 
        ;;
        --version)
            message 1 "$SINGULARITY_version\n"
            exit
        ;;
        -d|--debug)
            MESSAGELEVEL=5
            message 4 "enabling debugging\n"
            shift
        ;;
        -x|--shdebug)
            SHELL_DEBUG=1
            export SHELL_DEBUG
            message 4 "enabling shell debugging\n"
            shift
        ;;
        -v|--verbose)
            MESSAGELEVEL=`expr $MESSAGELEVEL + 1`
            message 2 "increasing verbosity level ($MESSAGELEVEL)\n"
            shift
        ;;
        -vv)
            MESSAGELEVEL=`expr $MESSAGELEVEL + 2`
            message 2 "increasing verbosity level ($MESSAGELEVEL)\n"
            shift
        ;;
        -vvv)
            MESSAGELEVEL=`expr $MESSAGELEVEL + 3`
            message 2 "increasing verbosity level ($MESSAGELEVEL)\n"
            shift
        ;;
        -vvvv)
            MESSAGELEVEL=`expr $MESSAGELEVEL + 4`
            message 2 "increasing verbosity level ($MESSAGELEVEL)\n"
            shift
        ;;
        -vvvv)
            MESSAGELEVEL=`expr $MESSAGELEVEL + 4`
            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

SINGULARITY_COMMAND="${1:-}"
export SINGULARITY_COMMAND

if [ -z "$SINGULARITY_COMMAND" ]; then
    cat "$SINGULARITY_libexecdir/singularity/cli/singularity.help"
    exit 0
fi

shift

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

# We should never get here...
exit 255
