#!/bin/bash
# -*- coding: utf-8 -*-

# antiX SAMBA Manager script
# Version 0.8
# GPL v.3
# Written 2/2024 by Robin for antiX community
# questions, suggestions and bug reporting please to www.antixforum.com

TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=antiX-samba-manager
export TEXTDOMAINDIR TEXTDOMAIN
source gettext.sh

# declare vars
tmp_entry="/dev/shm/asm-$$16"

# housekeeping
function cleanup() {
    rm -f "$tmp_entry" >/dev/null
}
trap cleanup EXIT


cat <<SHOW_ENTRY >"$tmp_entry"
ifsbak="\$IFS"; IFS="|"
     yad --on-top --center --undecorated \
         --close-on-unfocus --skip-taskbar --no-buttons \
         --width=430 --height=230 --borders=5 \
         --text-info --wrap <<<"\$(LANG=C mount | grep cifs | grep \$(echo \$2 | cut -d'|' -f2))"
IFS="\$ifsbak"
SHOW_ENTRY
chmod 755 "$tmp_entry"

ifsbak="$IFS"; IFS="|"
while true; do
    a=""
    remote_mounts="$(df -a --output=fstype,target | grep '^cifs ' | awk -F'^[[:blank:]]*([^ \t]*[ \t]+){1}' '{ printf ""$2"|" }')"
    for i in $remote_mounts; do
        a="${a}FALSE|$i|$(LANG=C mount | grep cifs | grep -F 'on '"$i"' ' | sed -n 's/^.*username=\(..*\)/\1/p' | cut -d, -f1)|$(LANG=C mount | grep cifs | grep -F 'on '"$i"' ' | sed -n 's/^\(..*\) on \/.*/\1/p')|Double click highlighted for Details. Tick boxes for button actions (unmount)|"
    done
    remote_umount="$(yad --center --title="$(eval_gettext "antiX SAMBA mount-manager")" \
                        --height=270 \
                        --window-icon="antiX-smb-mgr04" \
                        --text="$(eval_gettext "The following remote filesystems are currently mounted:")" \
                        --list \
                        --checklist \
                        --print-column=2 \
                        --tooltip-column=5 --hide-column=5 \
                        --dclick-action="bash -c "\""bash "$tmp_entry" %s"\""" \
                        --column="✅" \
                        --column="$(eval_gettext "Mountpoint")" \
                        --column="$(eval_gettext "Remote-Account")" \
                        --column="$(eval_gettext "Server")" $a \
                        --column="Tip":TIP \
                        --button="$(eval_gettext "Manage SAMBA shares")"!!"$(eval_gettext "Add or remove SAMBA shared folders on your local device and manage SAMBA printer sharing.")":2 \
                        --button="$(eval_gettext "Unmount ticked")"!!"$(eval_gettext "Tick the checkboxes of shares you want to unmount before pressing this button.")":4 \
                        --button="$(eval_gettext "Mount another one")"!!"$(eval_gettext "Mount additional remote shares not already listed above as mounted.")":6 \
                        --button="$(eval_gettext "Leave")"!!"$(eval_gettext "This quits antiX Samba manager. Additionally you may leave from any of its windows immediately by clicking the x in upper window border or using the ESC button.")":1 | tr -d \\n; exit "${PIPESTATUS[0]}")"
    case $? in
        "1"|"252") break;;
        "2") antiX-setup-samba-shares
             [ $? -eq 252 ] && exit 0;;
        "4") if ! [ -z "$remote_umount" ]; then
                for i in $remote_umount; do
                    gksu umount "$i"
                    if [ $? -ne 0 ]; then
                        yad --skip-taskbar --timeout=3 --on-top \
                            --center --width 300 --borders=5 \
                            --no-buttons --undecorated \
                            --text-info >/dev/null <<<"$(eval_gettext "U(n)mount failed.")"
                    fi
                done
             fi;;
        "6") antiX-mount-samba-shares true
             [ $? -eq 252 ] && exit 0;;
    esac
done
IFS="$ifsbak"
