#!/usr/bin/sh
#
# installing .ssh keys and authorizations
exec 1>/root/init-script.$$.out
exec 2>/root/init-script.$$.err
#
# --UseCase Express --SMT 192.168.160.1 --Root /data/SAP_HANA --FingerPrint 7F:8D:6D:B5:31:3E:66:30:5D:27:F5:68:A0:12:AB:2B:79:CA:E9:73

while [ $# -gt 0 ]; do

    case "$1" in
        --UseCase )
                    UseCase="$2"
                    shift
                    ;;
        --SMT )
                    SMT="$2"
                    shift
                    ;;
        --Root )
                    LsRoot="$2"
                    shift
                    ;;
        --FingerPrint )
                    SMTFP="$2"
                    shift
                    ;;
        
        
    esac 
    shift
done
#
mkdir -p /root/.ssh
rsync -a $LsRoot/files/root/.ssh/ /root/.ssh
chmod 700 /root/.ssh
chmod 600 /root/.ssh/id*
#
rsync -a $LsRoot/files/etc/ /etc
#
# installing sbin like SAPHana-Checks
#
rsync -a $LsRoot/files/usr/sbin/ /usr/sbin
#
# auto registration to smt.suse.de
#
clientSetup4SMT.sh  --yes --fingerprint "$SMTFP" --host $SMT
#
# optional: install updates
#
zypper --non-interactive up
#
# optional: install ha_sles pattern
#
# zypper in -t pattern ha_sles
systemctrl stop sapconf
systemctrl disable sapconf
systemctrl enable saptune
systemctrl start saptune

# TODO: if sap-hana and sap-nw pattern installed then S4HANA-APP+DB
if zypper se --installed-only -t pattern sap-hana
    st_solution="S4HANA-DBSERVER"
else
    st_solution="S4HANA-APPSERVER"
fi
saptune solution apply "$st_solution"
