#!/usr/bin/env bash
# ======================================================================
#
# DEPLOYMENT POC CLIENT
#
# ----------------------------------------------------------------------
# 2021-04-19  v0.1  <axel.hahn@iml.unibe.ch>  initial version
# ======================================================================


# ----------------------------------------------------------------------
# CONFIG
# ----------------------------------------------------------------------
cd $( dirname $0 )
selfdir=$( /bin/pwd )

tmpdir=/var/tmp/imldeployment_packages

wait=0
# wait=1


# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------


# get a list profiles by searching a config.sh
# no param
function getprofiles(){
    find ${selfdir}/profiles/ -name "config.sh" | rev | cut -f 2 -d "/" | rev
}

# set a profile, load it, verify required parameters
# param  string  name of a subdir in ./profiles/
function setprofile(){
    profile=$1

    # source config for software download - as default.
    . ${selfdir}/bin/getfile.sh.cfg 

    # my install dir
    installdir=

    # fileowner
    appowner=

    profiledir=${selfdir}/profiles/${profile}
    . ${profiledir}/config.sh || exit 11

    echo "[${profiledir}/config.sh] was loaded."
    if [ -z "$installdir" -o -z "${IMLCI_PHASE}" -o -z "${IMLCI_PROJECT}" ]; then
        echo "to be defined in ${profiledir}/config.sh:"
        echo "installdir    = $installdir"
        echo "These variables must be set in bin/getfile.sh.cfg or in [profile]/config.sh:"
        echo "IMLCI_PHASE   = $IMLCI_PHASE"
        echo "IMLCI_PROJECT = $IMLCI_PROJECT"
        exit 12
    fi 
    echo "OK, profile [${profile}] was set."

    downloadfile="${tmpdir}/${IMLCI_PROJECT}.tgz"
    downloadtmp="${tmpdir}/${IMLCI_PROJECT}.tgz.tmp"
}

# output a colored infoline with date and given message
# param  string  message text
function header(){
    local COLOR="\033[34m"
    local NO_COLOR="\033[0m"
    echo
    echo -en "${COLOR}"
    echo ______________________________________________________________________
    echo -n ">>>>>>>>>> $(date) "
    test ! -z "$profile" && echo -n "${profile} :: " 
    echo -n "$*"
    echo -en "${NO_COLOR}"
    if [ "$wait" = "1" ]; then
        echo -n " RETURN"; read dummy;
    fi 
    echo
}

# execute a task/ hook - if the given task script exists and has executable
# persmissions; if not it is not an error
# param  string  filename
function run_task(){
    local taskscript=$1
    if [ -x "${taskscript}" ]; then
        echo "INFO: starting script ${taskscript}..."
        . "${taskscript}" || exit 10
    else
        test -f "${taskscript}" && ( echo "SKIP: task script ${taskscript} is not executable." ; ls -l "${taskscript}")
        test -f "${taskscript}" || echo "SKIP: task script ${taskscript} does not exist."
    fi
}

# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

cd $( dirname $0 )

if [ $# -eq 0 ]; then
    header "looping over all profiles"
    getprofiles
    echo
    for myprofile in $( getprofiles )
    do
        $0 $myprofile    
    done
    rc=$?
    profile=
    header "loop over projects is finished"
    echo exiting with statuscode $rc
    exit $rc
fi

echo
echo
echo "<<<<<<<<<<##########|  POC :: DEPLOYMENT SCRIPT  |##########>>>>>>>>>>"
echo


# ----------------------------------------------------------------------
header "Set profile [$1]"
setprofile $1


# ----------------------------------------------------------------------
header "Download ${IMLCI_PROJECT}.tgz"
isupdate=0
${selfdir}/bin/getfile.sh -f ${IMLCI_PROJECT}.tgz -o ${downloadtmp} || exit 1


# ----------------------------------------------------------------------
header "Detect if download is newer than last download."
if [ -f ${downloadfile} ]; then
    # ls -l "${downloadfile}" "${downloadtmp}" 
    diff "${downloadfile}" "${downloadtmp}"
    if [ $? -eq 0 ]; then
        echo "INFO: the downloaded file is the same like last download."
        rm -f "${downloadtmp}"
    else
        echo "OK: donwload contains an update."
        isupdate=1
        mv "${downloadtmp}" "${downloadfile}"
    fi
else
    echo "INFO: last download not available - first install or a forced update."
    isupdate=1
    mv "${downloadtmp}" "${downloadfile}"
fi
ls -l "${downloadfile}"


# ----------------------------------------------------------------------
header "Switch into install dir ${installdir} ..."
test -d "${installdir}" || mkdir -p "${installdir}"
cd ${installdir} || exit 2


# ----------------------------------------------------------------------
header "PRE tasks"
# what you could do here:
# - enable maintenance flag
# - stop service
# - clenaup directory ... up to remove all current files
run_task "${profiledir}/tasks_preinstall.sh"


# ----------------------------------------------------------------------
header "PRE tasks II - cleanup"
test "$cleanup_preview" -eq "1" || echo "SKIP: preview of cleanup is disabled."
test "$cleanup_preview" -eq "1" && "${selfdir}/bin/preinstall_cleanup.sh" "${installdir}" "${downloadfile}"

test "$cleanup_force" -eq "1"   || echo "SKIP: cleanup files is disabled."
test "$cleanup_force" -eq "1"   && "${selfdir}/bin/preinstall_cleanup.sh" "${installdir}" "${downloadfile}" "force"


# ----------------------------------------------------------------------
header "Extract ${downloadfile} $( pwd )"
tar -xzf "${downloadfile}" . || exit 3
ls -l


# ----------------------------------------------------------------------
# header "Remove download archive ${IMLCI_PROJECT}.tgz"
# echo rm -f  ${IMLCI_PROJECT}.tgz


# ----------------------------------------------------------------------
header "Update config files"
echo "Showing replacements:" ; grep '@replace\[' hooks/templates/*
run_task "${profiledir}/tasks_config.sh"


# ----------------------------------------------------------------------
header "Set file owner [${appowner}]"
if [ ! -z "${appowner}" ]; then
    sudo chown -R $appowner * || exit 5
    ls -l
else
    echo "SKIP: variable appowner was not set"
fi


# ----------------------------------------------------------------------
header "POST tasks"
# what you could do here:
# - start current deploy scripts
# - apply database updates
# - set permissions
# - start service
# - remove maintenance flag
# - send success message as email/ slack/ [another fancy tool]
run_task "${profiledir}/tasks_postinstall.sh"


# ----------------------------------------------------------------------
header "DONE :-)"


# ----------------------------------------------------------------------
