#!/usr/bin/bash

. /etc/blackPanther-default-apps.conf

#######
#*********************************************************************************************************
#*   __     __               __     ______                __   __                      _______ _______   *
#*  |  |--.|  |.---.-..----.|  |--.|   __ \.---.-..-----.|  |_|  |--..-----..----.    |       |     __|  *
#*  |  _  ||  ||  _  ||  __||    < |    __/|  _  ||     ||   _|     ||  -__||   _|    |   -   |__     |  *
#*  |_____||__||___._||____||__|__||___|   |___._||__|__||____|__|__||_____||__|      |_______|_______|  *
#* http://www.blackpantheros.eu | http://www.blackpanther.hu - kbarcza[]blackpanther.hu * Charles Barcza *
#*************************************************************************************(c)2002-2021********
##   blackPanther Configuration
#####
debug=syslog
#debug=$(echo $@ | grep DEBUG && echo 1)
#####
NFS_AUTO_ENABLE=no
# Find the connection UUID with "nmcli con show" in terminal.
NETID_CONTROL=no
# All NetworkManager connection types are supported: wireless, VPN, wired...
# Requires NETID_CONTROL
WANTED_CON_UUID="d74d30bb-3136-4ea3-ae54-221ceb2f3b6f"
# owerite any parameter via conf
[ -f /etc/nfs_control.conf ] && . /etc/nfs_control.conf

export TEXTDOMAIN="default-apps"
export TEXTDOMAINDIR="/usr/share/locale"

if [ "$BP_DEFAULT_APPS" = "yes" ]; then

    if [ ! -n "$(echo "$PATH" | grep "/usr/lib/blackPanther-default-apps/bin" 2>/dev/null)" ]&&[ -d /usr/lib/blackPanther-default-apps/ ]; then
        export PATH="/usr/lib/blackPanther-default-apps/bin:$PATH"
    fi
    if [ ! -n "$(echo "$PATH" | grep "/usr/lib/blackPanther-default-serverapps/bin" 2>/dev/null)" ]&&[ -d /usr/lib/blackPanther-default-serverapps/ ]; then
        export PATH="/usr/lib/blackPanther-default-serverapps/bin:$PATH"
    fi
fi

dbg()
{
    if [ "$debug" = "syslog" ];then
	enter=""
       logger -t "NFS" ${@}
    elif [ -n "$debug" ]; then
	enter="\n"
	quiet=-q
        space='echo'
       	$SETCOLOR_FAIL
       	echo -en "DBG: ${@} $enter"
       	$SETCOLOR_NORMAL
    else
	enter="\n"
	echo -en "DBG: $debug ${@} $enter"
    fi 
}

while IFS= read -r LINE ; do
    TARGET="$(echo $LINE | awk -F: '{print $1}' 2>/dev/null)"
    if [ -n "$TARGET" ];then
	STATUS="$(timeout 1 bash -c ": < /dev/tcp/${TARGET}/2049" || echo OFF)"
	if [ "$STATUS" = "OFF" ];then
	    dbg "==> Offline: $TARGET"
	else
	    dbg " ====> Target $TARGET Record:$(echo $LINE | awk -F: '{print $2}')"
	    NFS_AUTO_ENABLE=yes 
	fi
    fi
done <<< "$(grep -w 'nfs' /etc/fstab | grep -v '^#')" 


if [ "$NETID_CONTROL" = "yes" ]; then
    if [ "$CONNECTION_UUID" == "$WANTED_CON_UUID" ];then
	continue
     else
	exit 0
    fi
fi

dbg " ====> Activate by:$1 Enabled:$NFS_AUTO_ENABLE Get:$2 ID:$CONNECTION_UUID"

if [ "$NFS_AUTO_ENABLE" = "yes" ];then
    # Script parameter $1: NetworkManager connection name, not used
    # Script parameter $2: dispatched event
    
    case "$2" in
        "up")
    	    dbg "==> Shares mounting now." 
            mount -a -t nfs4,nfs 
            ;;
        "pre-down");&
        "vpn-pre-down")
    	    dbg "==> Shares umounting now." 
            umount -l -a -t nfs4,nfs -f >/dev/null
            ;;
    esac
fi
