#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;93m'
MAGENTA='\033[0;35m'
NC='\033[0m' # No Color

log_dir="/var/log/uncom-autonetmount/"
logfile="$log_dir/check_net_disk.log"

# Example:
# server="192.168.3.73"
# mount_dir="/home/uncom/test_net_mount"
# show_notification="yes"
. /usr/share/appdata/autonetmount.conf

function log() {
    echo $1 >> $logfile
}

function ping_f() {
    ping -c 1 $1 ; code_ping=$?
    if [[ $code_ping -eq 0 ]]
    then echo -e "${GREEN} OK, exit ${NC}"
        log "Ping ok ($1)"
        return 0
    else echo "${RED} Ping not ok ${NC}"
        log "Ping not ok: $1 is not available"
        return 1
    fi
}

function check_mount() {
    mount | grep "$1" ; code_mount=$?
    if [[ $code_mount -eq 0 ]]
    then echo -e "${GREEN} OK, exit ${NC}"
        log "$1 is mounted"
        [[ "$(ls -A $1)" ]] ; code_notempty=$?
        if [[ $code_notempty -eq 0 ]]
            then echo -e "${GREEN} OK, $1 not empty ${NC}"
                log "OK, $1 not empty, exiting"
                return 0
            else echo "${RED}  Error, $1 is empty ${NC}"
                log "Error, $1 is empty"
        fi
    else echo "${RED}$1 is not mounted ${NC}"
        log "$1 is not mounted"
    fi
    return 1
}

##########################################

echo -e "${GREEN}Checking log dir${NC}"
[[ -d $log_dir ]] || {
    echo -e "${GREEN}Creating log directory${NC}"
    mkdir -p $log_dir
    date=`date`
    log "Creating log directory: $date"
}

date_now=`date +%s`
date_now_human=`date`

log ""
log ""
log "Check date: $date_now_human"

i=0
while [ $i -lt 10 ]
do
    i=`expr $i + 1`
    x="server_$i"

    # Check if there is no more servers
    if [[ -z ${!x} ]]
    then log "Script work ends (i=$i)"
        exit 0
    fi

    server=${!x}
    x="mount_dir_$i"
    mount_dir=${!x}
    x="show_notification_$i"
    show_notification=${!x}

    log "Server: $server"
    log "Target dir: $mount_dir"
    log "Show notification: $show_notification"

    check_mount "$mount_dir" ; check_one=$?

    if [[ $check_one -ne 0 ]]
    then ping_f "$server" ; check_ping=$?
        if [[ $check_ping -eq 0 ]]
            then echo -e "${GREEN} Mounting: running ${MAGENTA}mount -a ${NC}"
                mount -a
                mount_out=`mount`
                log "mount output: $mount_out"
                check_mount "$mount_dir" ; check_two=$?
                if [[ $check_two -eq 0 ]]
                    then
                        if [[ "xx$show_notification" == "xxyes"  ]]
                        then echo $LANG | grep "ru_" ; code=$?
                            if [ $code -eq 0 ]
                            then
                                title="Внимание!"
                                message="Успешно смонтирован каталог $mount_dir (сервер: $server):"
                            else
                                title="Attention!"
                                message="Successfully mounted $mount_dir (server: $server):"
                            fi
                            for ln in "$(ps -eo user,args | grep "dbus-daemon.*--session.*--address=" | grep -v grep)"; do
                                # get the user name
                                user="$(echo "$ln" | cut -d' ' -f 1)"
                                id=`id -u $user`
                                bus_addr="unix:path=/run/user/$id/bus"
                                # run notify-send
                                sudo -u $user DBUS_SESSION_BUS_ADDRESS="$bus_addr" notify-send "$title" "$message"
                            done
                        else
                            log "Notifications disabled, exiting"
                        fi
                fi
        fi
    fi
    log "######"
done
