# POSIX shell code
#begin part2
case $PARAMS in # RoxApp right-click menu
  edit-config ) exec defaulttexteditor .config ;;
  unmount ) YAD_TITLE=$i18n_p2_TitleUnmount ;;
  * ) YAD_TITLE=$i18n_p2_TitleMount ;;
esac
init_app2
yad_options1="--selectable-labels --button=gtk-ok --center --text-align=center --text=<big>[?]</big>"
TRY=/tmp/smb-browser
UNC="//$Server/$Share" # Note that we will mount //$IP/$Share
! get_subnets >/dev/null && exit_no_subnets
APPDIR_PARENT=${APPDIR%/*}
if ! mountpoint -q "$APPDIR/$MOUNTNAME" 2>/dev/null; then
  case $PARAMS in unmount|umount )
    # opportunity to reset a stale 'mounted' icon
    ln -sfT $ICON_UNMOUNTED .DirIcon
    exit ;;
  esac
  if [ "$IP" ]; then
    : # when IP address never changes user can force IP address by editing .config
  elif [ -z "$SUBNETSALT" ]; then
    # when IP address can change (default) perform a NETBIOS server name to IP address lookup
    IP=$(nmblookup $Server | grep "<00>" | cut -d " " -f 1)
  else
    # alternatively some users may prefer to scan an address range
    for IP in $(mpscan $TIMEOUT -p $PORT ${SUBNETSALT}.1 - ${SUBNETSALT}.254 2>/dev/null | cut -d " " -f 2); do
      nmblookup -A $IP | grep -q $Server && break
    done
  fi
  ! [ "$IP" ] && IP=$Server # firewalled!
  case $MountOptions in
    *gid=[a-zA-Z]* )
      # replace symbolic gid with numeric gid
      MountOptions="\$(awk -F: -v \"S=$MountOptions\" '
      BEGIN {
        match(S, /,?gid=[^,]+/)
        h   = substr(S, 1, RSTART -1)
        gid = substr(S, RSTART, RLENGTH)
        t   = substr(S, RSTART + RLENGTH)
        p   = index(gid, \"=\")
        h   = h substr(gid, 1, p)
        gid = substr(gid, p +1)
      }

      \$1 == gid { # replace gid
        print h \$3 t
        exit
      }
      ' /etc/group)"
      ;;
  esac
  if [ guest != "$Username" ]; then
    if [ "$Password" ]; then
      ! [ "$ClearText" ] && Password="$(printf %s "$Password"|base64 -d)"
    else
      set_Username_Password
    fi
  fi
  TRY="$TRY-mount.$$"
  : > "$TRY"; : > "$TRY".err
  chmod 700 "$TRY" "$TRY".err && ({
    echo "#!/bin/sh
[ \$(id -u) -ne 0 ] && exec gtksu \"\$(printf '$i18n_p2_GtksuTitleMount' '$UNC')\" \"\$0\" \"\$@\" # exits here
"
    printf "#"; type is_firewalled
    echo "
if ! mount.cifs '//$IP/$Share' '$APPDIR/$MOUNTNAME' -o \"$MountOptions\"; then
  # try 'vers=1.0'
  mount.cifs '//$IP/$Share' '$APPDIR/$MOUNTNAME' -o \"vers=1.0,$MountOptions,vers=1.0\" 2>/dev/null
fi
mountpoint -q '$APPDIR/$MOUNTNAME' 2>/dev/null; retval=\$?
[ 0 = \$retval ] && ln -sfT $ICON_MOUNTED .DirIcon || ln -sfT $ICON_UNMOUNTED .DirIcon
rm \"\$0\"
if [ 0 != \$retval ]; then
  is_firewalled && exit 11 || exit 10
fi
# overall exit 0(mounted) 1(gtksu cancelled) 10(not mounted, firewall off)3 11(mot mounted, firewall on)
"; } >> "$TRY" &&
  chmod o+x "$TRY" &&
  USER="$Username" PASSWD="$Password" "$TRY"
  ) 2>> "$TRY".err && retval=$? || retval=$? # keep! for shell option +e
  rm -f "$TRY"
  unset NOTICE
  if [ $retval -ge 10 ]; then
    case $retval in
      11 ) NOTICE="\r\r<b><span bgcolor=\"yellow\">  $i18n_p2_this_firewall_enabled  </span></b>\r$i18n_p2_port_139\r$i18n_p2_port_445" ;;
    esac
    yad_gtk2 $yad_options1 --form \
      --field="$(printf "$i18n_p2_Could_not_mount\r\r$i18n_p2_Hints$i18n_p2_Hints_1\r\r<span color=\"red\">$(tr '&<>' ' ' < "$TRY".err)</span>$NOTICE\r" "$UNC")":LBL \
    --button="gtk-help:defaultbrowser /usr/share/doc/faqs/smb-browser.html"
  fi
  rm -f "$TRY".err
  [ $retval != 0 ] && exit $retval
  rox -d "$APPDIR/$MOUNTNAME" # rox -d preserves symlink name's path if any
  # rox -x must follow rox -d otherwise rox -d will time out when two ROX-Filer
  # processes owned by different users keep $MOUNTNAME open. Covers a corner case.
  rox -x "$APPDIR_PARENT" # refresh mount point container window so new icon shows up.
else # if already mounted
  case $PARAMS in
  unmount|umount )
    TRY="$TRY-unmount.$$"
    : > "$TRY"; : > "$TRY".err
    chmod 700 "$TRY" "$TRY".err && (
    echo "#!/bin/sh
[ \$(id -u) -ne 0 ] && exec gtksu \"\$(printf '$i18n_p2_GtksuTitleUnmount' '$UNC')\" \"\$0\" \"\$@\" # exits here
umount -n '$APPDIR/$MOUNTNAME'
mountpoint -q '$APPDIR/$MOUNTNAME' 2>/dev/null; retval=\$?
[ 0 = \$retval ] && ln -sfT $ICON_MOUNTED .DirIcon || ln -sfT $ICON_UNMOUNTED .DirIcon
rm \"\$0\"
[ 1 = \$retval ] || exit 10
# overall exit 0(unmounted) 1(gtksu cancelled) 10(mounted)
" >> "$TRY" &&
    chmod o+x "$TRY" &&
    "$TRY"
    ) 2>> "$TRY".err; retval=$?
    rm -f "$TRY"
    [ $retval = 10 ] &&
      yad_gtk2 $yad_options1 --form \
      --field="$(printf "$i18n_p2_Could_not_unmount\r<span color=\"red\">$(tr '&<>' ' ' < "$TRY".err)</span>\r" "$UNC")":LBL \
      --button="gtk-help:defaultbrowser /usr/share/doc/faqs/smb-browser.html"
    rm -f "$TRY".err
    [ $retval != 0 ] && exit $retval
    rox -D "$APPDIR/$MOUNTNAME"
    # Even if rox -x follows rox -D (likewise the mount case) rox -D times out when two
    # ROX-Filer processes owned by different users keep $MOUNTNAME open. So be it.
    rox -x "$APPDIR_PARENT"
    ;;
  * )
    rox -d "$APPDIR/$MOUNTNAME"
    ;;
  esac
fi
#end part2
# vim:ft=sh:

