#!/bin/bash
# 
# Copyright (c) 2017-2018, SyLabs, Inc. All rights reserved.
# Copyright (c) 2017, SingularityWare, LLC. All rights reserved.
#
# Copyright (c) 2015-2017, Gregory M. Kurtzer. All rights reserved.
# 
# 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. 
# 
# 

# Relax restrictive umasks that can cause several problems
umask 0022

prefix="/usr/local"
exec_prefix="${prefix}"
libexecdir="${exec_prefix}/libexec"
sysconfdir="${prefix}/etc"
localstatedir="${prefix}/var"
bindir="${exec_prefix}/bin"
home=`dirname  "$0"`

SINGULARITY_version="2.6.1-HEAD.9103f01"
SINGULARITY_libexecdir="$libexecdir"
SINGULARITY_sysconfdir="$sysconfdir"
SINGULARITY_localstatedir="$localstatedir"
SINGULARITY_bindir="$bindir"

SINGULARITY_MESSAGELEVEL=1
PATH="/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin"

export SINGULARITY_bindir SINGULARITY_sysconfdir SINGULARITY_libexecdir SINGULARITY_localstatedir SINGULARITY_version SINGULARITY_MESSAGELEVEL PATH

# 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

message 5 "Starting argument loop\n"

while true; do
    case ${1:-} in
        -h|--help|help)

            shift
            exec $SINGULARITY_libexecdir/singularity/cli/help.exec "$@"
        ;;
        -q|--quiet)
            SINGULARITY_MESSAGELEVEL=0
            shift 
        ;;
        --version)
            message 1 "$SINGULARITY_version\n"
            exit
        ;;
        -s|--silent)
            SINGULARITY_MESSAGELEVEL=-3
            shift
        ;;
        -d|--debug)
            SINGULARITY_MESSAGELEVEL=5
            message 4 "Enabling debugging\n"
            shift
        ;;
        -x|--sh-debug)
            SHELL_DEBUG=1
            export SHELL_DEBUG
            message 4 "Enabling shell debugging\n"
            shift
        ;;
        -v|--verbose)
            SINGULARITY_MESSAGELEVEL=`expr $SINGULARITY_MESSAGELEVEL + 1`
            message 2 "Increasing verbosity level ($SINGULARITY_MESSAGELEVEL)\n"
            shift
        ;;
        -vv)
            SINGULARITY_MESSAGELEVEL=`expr $SINGULARITY_MESSAGELEVEL + 2`
            message 2 "Increasing verbosity level ($SINGULARITY_MESSAGELEVEL)\n"
            shift
        ;;
        -vvv)
            SINGULARITY_MESSAGELEVEL=`expr $SINGULARITY_MESSAGELEVEL + 3`
            message 2 "Increasing verbosity level ($SINGULARITY_MESSAGELEVEL)\n"
            shift
        ;;
        -vvvv)
            SINGULARITY_MESSAGELEVEL=`expr $SINGULARITY_MESSAGELEVEL + 4`
            message 2 "Increasing verbosity level ($SINGULARITY_MESSAGELEVEL)\n"
            shift
        ;;
        -*)
            message ERROR "Unknown argument: $1\n"
            exit 1
        ;;
        *)
            message 5 "Ending argument loop\n"
            break
        ;;
    esac
done

message 2 "Singularity version: $SINGULARITY_version\n"

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


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


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

