#!/bin/bash
#
############ automate-00-createNetworks
#
#############################################################################
#
# License: GPL
# (C) 2017-2018 SUSE Linux GmbH, Nuremberg, Germany
# AUTHOR: Fabian Herschel
#
#############################################################################

source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"

mode="all"
node="all"
theGROUP=""
while [[ $# -gt 0 ]]; do
    case "$1" in
        --server ) mode="server";;
        --client ) mode="client";;
        --group=* ) theGROUP=${1#--group=};;
    esac
    shift
done

function filterSystem()
{
    local systemDef="" systemKV=""
    local fields="SYSTEM"
    local f="" ptrF="" outPut=0 outStr=""
    while [ $# -gt 0 ]; do
        case "$1" in
            --system ) systemDef="$2"; shift
                       ;;
            --fields ) fields="$2"; shift
                       ;;
        esac
        shift
    done
    for systemKV in ${systemDef}; do
        sKey="${systemKV%=*}"; sKey=${sKey^^}
        sValue=${systemKV#*=}
        local SYSTEM_$sKey=$sValue
    done
    # BAUSTELLE
    if [[ $theGROUP == "all" && $node == "all" ]]; then
        outPut=1
    elif [[ $theGROUP == "$SYSTEM_OWNER" && $node == "all" ]]; then
        outPut=1
    elif [[ $theGROUP == "all" && $node == "$SYSTEM_SYSTEM" ]]; then
        outPut=1
    elif [[ $theGROUP == "$SYSTEM_OWNER" && $node == "$SYSTEM_SYSTEM" ]]; then
        outPut=1
    fi
    if [ $outPut -eq 1 ]; then
        for f in $fields; do
            ptrF="SYSTEM_$f"
            outStr="${outStr}${outStr:+ }${!ptrF}"
        done
        printf "%s" "$outStr"
    fi
}

# select hyp,system from LandscapeSystems where ...

typeset -a allSystems
for systemDef in "${LandscapeSystems[@]}"
do
    help1=$(filterSystem --fields 'SYSTEM' --system "$systemDef"; printf "\n")
    echo "systemDef=$systemDef help1=$help1"
    if [ -n "$help1" ]; then
        if [ ${#allSystems[@]} -gt 0 ]; then
            allSystems=( "${allSystems[@]}" "$help1" )
        else 
            allSystems=( "$help1" )
        fi
    fi
done

echo "theGROUP=$theGROUP; node=$node allSystems=${allSystems[@]}"

# LandscapeShares=(
#"owner=gli server=192.168.1.3 share=/export/scbsr/@@OWNER@@ mp=/nfs/@@OWNER@@ options=rw,nohide,insecure,no_subtree_check,sync"
#)

set +u
for lsShare in "${LandscapeShares[@]}"; do
    #echo "########### $lsShare ###########################"
    export lsShare
    export SH_OWNER="" SH_SERVER="" SH_SHARE="" SH_MP="" SH_OPTIONS=""  SH_CLIENTS="" SH_MOPT=""
    (
        for lsShareD in ${lsShare}; do
            # name=xxxx  ===> export NET_NAME=xxxx
            # DONE: xxxx could include a val=key sequence
            #echo "lsShareD: $lsShareD"
            cmd=$(awk -F= '{ printf( "export SH_%s=%s",toupper($1),$2); for (i=3; i<NF+1; i++) { printf "=%s",  $i }; print "" }' <<< $lsShareD)
            #echo $cmd
            eval $cmd
        done
        #
        # server
        #
        if [[ "$mode" == "all" || "$mode" == "server" ]]; then
            exports_entry_template="@@SHARE@@ @@CLIENTS@@(@@OPTIONS@@)"
            exports_entry=$(echo "$exports_entry_template" | sed \
                -e "s|@@SHARE@@|$SH_SHARE|g" \
                -e "s|@@CLIENTS@@|$SH_CLIENTS|g" \
                -e "s|@@OPTIONS@@|$SH_OPTIONS|g" \
                -e "s|@@OWNER@@|$SH_OWNER|g" \
                -e "s|@@LandscapeUseCase@@|$LandscapeUseCase|g")
            exports_dir="${exports_entry% *}"
            mkdir -p $exports_dir
            touch "/etc/exports.d/${LandscapeUseCase}.exports"
            grep -v "^${exports_dir}\s" "/etc/exports.d/${LandscapeUseCase}.exports" > "/etc/exports.d/${LandscapeUseCase}.tmp"
            echo "$exports_entry" >> "/etc/exports.d/${LandscapeUseCase}.tmp"
            mv "/etc/exports.d/${LandscapeUseCase}.tmp" "/etc/exports.d/${LandscapeUseCase}.exports"
        fi
        #
        # client
        #
        if [[ "$mode" == "all" || "$mode" == "client" ]]; then
            fstab_entry_template="@@SERVER@@:@@SHARE@@ @@MP@@ nfs @@MOPT@@ 0 0"
            fstab_entry=$(echo "$fstab_entry_template" | sed \
                -e "s|@@SHARE@@|$SH_SHARE|g" \
                -e "s|@@SERVER@@|$SH_SERVER|g" \
                -e "s|@@MP@@|$SH_MP|g" \
                -e "s|@@MOPT@@|$SH_MOUNT_OPTIONS|g" \
                -e "s|@@OWNER@@|$SH_OWNER|g" \
                -e "s|@@LandscapeUseCase@@|$LandscapeUseCase|g" )
            for s in "${allSystems[@]}"; do
                echo "on $s add to fstab:       $fstab_entry"
            done
        fi
    ) 
done | sort


# 192.168.1.1:/data/export/ensanfssimple/SN2/sapmnt-SN2  /sapmnt/SN2  nfs vers=3.0,defaults 0 0
