#!/bin/bash
#
# Copyright (c) 2017-2018, SyLabs, Inc. All rights reserved.
# Copyright (c) 2017, SingularityWare, LLC. All rights reserved.
# Copyright (c) 2017, Vanessa Sochat. All rights reserved.
#
# See the COPYRIGHT.md file at the top-level directory of this distribution and at
# https://github.com/singularityware/singularity/blob/master/COPYRIGHT.md.
#
# This file is part of the Singularity Linux container project. It is subject to the license
# terms in the LICENSE.md file found in the top-level directory of this distribution and
# at https://github.com/singularityware/singularity/blob/master/LICENSE.md. No part
# of Singularity, including this file, may be copied, modified, propagated, or distributed
# except according to the terms contained in the LICENSE.md file.
#


##############################################################################
# SECTIONS
##############################################################################


singularity_section_exists() {
    SECTION="${1:-}"
    FILE="${2:-}"
    TOGGLE=""
    if [ ! -f "$FILE" ]; then
        message ERROR "File not found ($FILE)\n"
        exit 1
    fi
    if ! egrep -i -q -- "^%$SECTION\W*" "$FILE"; then
        return 1
    fi

    return 0
}


singularity_section_args() {
    SECTION="${1:-}"
    FILE="${2:-}"
    TOGGLE=""
    if [ ! -f "$FILE" ]; then
        message ERROR "File not found ($FILE)\n"
        exit 1
    fi

    egrep -i -- "^%$SECTION\W*" "$FILE" | sed -e "s@%$SECTION\s*@@i"

    return 0
}

singularity_section_get() {
    SECTION="${1:-}"
    FILE="${2:-}"
    if [ ! -x "$SINGULARITY_libexecdir/singularity/bin/get-section" ]; then
        message ERROR "Could not locate get-section program\n"
        exit 255
    fi

    if [ ! -f "$FILE" ]; then
        message ERROR "File not found ($FILE)\n"
        exit 1
    fi

    eval "$SINGULARITY_libexecdir/singularity/bin/get-section" "$SECTION" "$FILE"

    return 0
}


get_section() {

    SECTION="$1"
    RECIPE="$2"
    FOUND="no"

    while read -r line
    do

 
        if echo $line | grep -q -e "^%$SECTION$"; then

            # If not found, continue
            if [[ "$FOUND" == "no" ]]; then
                FOUND="yes"
            else
                return 0
            fi

        # We didn't match this line but...
        else
            # It's the start of a new section
            if echo $line | grep -q -e "^%"; then

                # We already found it, we're done
                if [[ "$FOUND" == "yes" ]]; then
                    return 0
                fi

            else
                # We already found it, still in section
                if [[ "$FOUND" == "yes" ]]; then
                    if [ ! -z "${line}" ]; then
                        echo $line
                    fi 

                fi            
            fi
        fi

    done < $RECIPE

}



##############################################################################
# APPS
##############################################################################


singularity_app_install_get() {
    APPNAME="${1:-}"
    FILE="${2:-}"

    if [ ! -f "$FILE" ]; then
        message ERROR "File not found ($FILE)\n"
        exit 1
    fi

    # To install application, much change to its base and back so paths are relative
    SECTION="appinstall ${APPNAME}"
    printf "cd /scif/apps/${APPNAME}\n"
    get_section "$SECTION" "$FILE"
    return 0
}


singularity_app_save() {
    APPNAME="${1:-}"
    FILE="${2:-}"
    OUTPUT_FILE="${3:-}"

    if [ ! -x "$SINGULARITY_libexecdir/singularity/bin/get-section" ]; then
        message ERROR "Could not locate get-section program\n"
        exit 255
    fi

    if [ ! -f "$FILE" ]; then
        message ERROR "File not found ($FILE)\n"
        exit 1
    fi

    SECTION_FOUND="No"
    while read line
    do
        if [[ "$line" == "%appinstall $APPNAME" ]]; then # start of section
            echo "$line" >> $OUTPUT_FILE
            SECTION_FOUND="Yes"
        elif [[ "$line" == %* ]]; then                   # end of section
            if [[ "$SECTION_FOUND" == "Yes" ]]; then
                return 0
            fi
        else
            if [[ "$SECTION_FOUND" == "Yes" ]]; then
                echo "$line" >> $OUTPUT_FILE             # line in section
            fi
        fi
    done < "$FILE"
    return 0
}


singularity_app_init() {

    APPNAME="${1:-}"
    APPROOT="${2:-}"

    APPBASE="${APPROOT}/scif/apps/${APPNAME}"
    APPMETA="${APPBASE}/scif"

    if [ ! -d "${APPBASE}" ]; then
        mkdir -p "${APPBASE}/scif"
        mkdir -p "${APPBASE}/bin"
        mkdir -p "${APPBASE}/lib"
        mkdir -p "${APPMETA}"
        mkdir -p "${APPMETA}/env"
        message 2 "Creating ${APPBASE}...\n"
    fi

    APPDATA="${APPROOT}/scif/data/${APPNAME}"
    if [ ! -d "${APPDATA}" ]; then
        mkdir -p ${APPDATA}
        mkdir -p "${APPDATA}/input"
        mkdir -p "${APPDATA}/output"
        message 2 "Creating ${APPDATA}...\n"
    fi

    # App specific variables should be exposed at run, shell, exec
    echo "
    SINGULARITY_APPNAME=$APPNAME
    SINGULARITY_APPROOT=\"/scif/apps/${APPNAME}\"
    SINGULARITY_APPMETA=\"/scif/apps/${APPNAME}/scif\"
    SINGULARITY_DATA=\"/scif/data\"
    SINGULARITY_APPDATA=\"/scif/data/${APPNAME}\"
    SINGULARITY_APPINPUT=\"/scif/data/${APPNAME}/input\"
    SINGULARITY_APPOUTPUT=\"/scif/data/${APPNAME}/output\"
    export SINGULARITY_APPDATA SINGULARITY_APPNAME SINGULARITY_APPROOT SINGULARITY_APPMETA SINGULARITY_APPINPUT SINGULARITY_APPOUTPUT SINGULARITY_DATA
    " > "${APPMETA}/env/01-base.sh"

}
