#!/usr/bin/env qsh

BASE_DIR=$(cd "$(dirname "$0")"; pwd)
BASE_NAME=$(basename "$BASE_DIR")
PACKAGE_DIR="$BASE_DIR/packages"
PACKAGE_NAME="icebox2"

SUBSYSTEM="icebreak"

echo "Script: $0, base: $BASE_NAME, path: $BASE_DIR, packages: $PACKAGE_DIR"

load () {
    if [ "$CAN_DOWNLOAD" -eq 1 ]; then
        echo "Downloading $1..."
        curl --connect-timeout 5 -k \
        -H 'User-Agent: Mozilla/5.0 (Windows NT x.y; rv:10.0) Gecko/20100101 Firefox/10.0' \
        "http://webfiles.system-method.com/packages/Icebox2pkg/$1" -o "$PACKAGE_DIR/$1"
    fi

    if [ ! -f "$PACKAGE_DIR/$1" ]; then    
        >&2 echo "File $1 does not exists, and I am unable to download it."
        exit 1
    fi
}


loadAndInstallDependency () {
    if load "$1" ; then
        if [ "$CAN_INSTALL" -eq 1 ]; then
            tar -xf "$PACKAGE_DIR/$1" -C "/$SUBSYSTEM/system/shared"
        fi
    else
        >&2 echo "Failure during load of dependency $1"
        exit 1
    fi
}


loadAndInstallProduct  () {
    if load "$1" ; then
        if [ "$CAN_INSTALL" -eq 1 ]; then
            tar -xf "$PACKAGE_DIR/$1" -C /www
        fi
    else
        >&2 echo "Failure during load of product $1"
        exit 1
    fi
}


loadAndInstallLibrary () {
    if load "$1" ; then
        if [ "$CAN_INSTALL" -eq 1 ]; then
            if [ -n "$2" ]; then
                system -v "crtsavf qgpl/$2"
                system -v "clrsavf qgpl/$2"
                system -v "CPYFRMSTMF FROMSTMF('$PACKAGE_DIR/$1') TOMBR('/QSYS.lib/QGPL.LIB/$2.FILE') MBROPT(*REPLACE)"   
                system -vK "RSTLIB SAVLIB($2) DEV(*SAVF) SAVF(qgpl/$2) MBROPT(*ALL) ALWOBJDIF(*ALL)  "
                system -v "dltf qgpl/$2"
            else
                >&2 echo "Argument 2, library must be specified"
                exit 1
            fi
        fi
    else
        >&2 echo "Failure during load of library $1"
        exit 1
    fi
}

CAN_INSTALL=0

OS=$(uname -s)
if [ "$OS" = "OS400" ]; then
    CAN_INSTALL=${SHOULD_INSTALL:-1}

    if [ -f "$BASE_DIR/utils/lib" ]; then
        LD_LIBRARY_PATH="$BASE_DIR/utils/lib"
        export LD_LIBRARY_PATH

        alias curl="\$BASE_DIR/utils/bin/curl"
    fi
fi

CAN_DOWNLOAD=${SHOULD_DOWNLOAD:-1}
if ! type curl 1>/dev/null ; then
    CAN_DOWNLOAD=0
fi

export BASE_DIR
export PACKAGE_DIR
export CAN_INSTALL
export CAN_DOWNLOAD

mkdir -p "$PACKAGE_DIR" 2>/dev/null
if ! cd "$BASE_DIR" ; then
    >&2 echo "Could not enter folder $BASE_DIR, aborting"
    exit 1
fi


echo "Checking dependencies"
while IFS="" read -r FILE || [ -n "$FILE" ]
do
    if ! loadAndInstallDependency "$FILE" ; then
      exit 1
    fi
done <"$BASE_DIR/dependencies.txt"


echo "Checking products"
while IFS="" read -r FILE || [ -n "$FILE" ]
do
    if ! loadAndInstallProduct "$FILE" ; then
      exit 1
    fi
done <"$BASE_DIR/products.txt"


echo "Checking libraries"
while IFS=" " read -r FILE LIBRARY || [ -n "$FILE" ]
do
    if ! loadAndInstallLibrary "$FILE" "$LIBRARY" ; then
      exit 1
    fi
done <"$BASE_DIR/libraries.txt"


if [ "$CAN_INSTALL" -eq 1 ]; then
    echo ""
    echo "Installing..."
    system "$SUBSYSTEM/addicesvr svrid(icebox2) TEXT('IceBox bundle: Portfolio, Icecap II') SVRPORT(7070) HTTPPATH('/www/portf6/admin') WWWDFTDOC('index.html') WEBCONFIG('/www/portf6/admin/webconfig.xml') APPLIB(ICEBOX2) CCSID(277)"
    system "$SUBSYSTEM/stricesvr svrid(icebox2) "
    echo "Installation complete. Open you browser at port 7070"
else
    PACKAGE="/tmp/icebox2-offline.tar"
    echo ""
    echo "Creating package $PACKAGE for offline install..."
    (cd "$BASE_DIR/.." && cp -R "$BASE_NAME" "$PACKAGE_NAME" && tar cf "$PACKAGE" "$PACKAGE_NAME")
    echo "Packaging complete, please copy $PACKAGE to IBM i /tmp folder, and run"
    echo "QSH CMD('cd /tmp && tar xf $PACKAGE && $PACKAGE_NAME/install.sh')"
fi