#!/bin/sh
#  VLSMB V1.0
#
# By Tony Brijeski
#
# Kocil: check if samba and rox is exist

if [ ! $DISPLAY = "" ]; then
DCMD="Xdialog --wrap"
CMD="rxvt -e "
else
DCMD="dialog"
CMD=""
fi

Choices=""
TMP=/tmp/tmp
if [ ! -d $TMP ]; then

mkdir -p $TMP
fi

main()
{
while [ 0 ]; do

 $DCMD --title "Vector Linux Remote SMB Share Setup" \
--menu \
"If you are in GUI mode, ROX-Filer will be restarted.\n\
This will kill any other ROX-Filer windows as well.\n\
 Make a selection or CANCEL to exit."  20 76 2  \
"SCAN" "Try to scan the network for servers and shares with smbtree" \
"SETSHARE" "setup the Windows Network share and create a desktop icon" 2> $TMP/smbset
 if [ $? = 1 -o $? = 255 ]; then
  rm -f $TMP/smbset
 clear
  exit 0
 fi
 MAINSELECT="`cat $TMP/smbset`"
 rm $TMP/smbset

if [ "$MAINSELECT" = "SCAN" ]; then

$DCMD --title "Please Wait...." \
 --infobox "Please wait while scanning the network \n\
\n\
Make a note of the SERVER and SHARE names you wish to setup" 0 0

smbtree -N > $TMP/smbtree-list

$DCMD --title "SMBTREE SCAN OF THE NETWORK" \
--textbox $TMP/smbtree-list 0 0
rm -f $TMP/smbtree-list
fi


if [ "$MAINSELECT" = "SETSHARE" ]; then

shareset

if [ "$OKAY" = "YES" ]; then
createshare
fi

fi


done
}


shareset() 
{

OKAY="no"
 $DCMD --title "ENTER SERVER NAME OR IP ADDRESS" \
 --inputbox "Enter only the Netbios name, IP Address or DNS name of the server" 0 0  \
 $SERVERNAME 2> $TMP/smbset

if [ $? = 1 -o $? = 255 ]; then
 rm -f $TMP/smbset
 exit
fi
SERVERNAME="`cat $TMP/smbset`"
rm -f $TMP/smbset


 $DCMD --title "ENTER SHARE NAME" \
 --inputbox "Enter only the Name of the share" 0 0  \
 $SHARENAME 2> $TMP/smbset

if [ $? = 1 -o $? = 255 ]; then
 rm -f $TMP/smbset
 exit
fi
SHARENAME="`cat $TMP/smbset`"
rm -f $TMP/smbset


 $DCMD --title "ENTER WINDOWS USERNAME" \
 --inputbox "Enter the Windows Username for authentication" 0 0 \
 $WINUSER 2> $TMP/smbset

if [ $? = 1 -o $? = 255 ]; then
 rm -f $TMP/smbset
 exit
fi
WINUSER="`cat $TMP/smbset`"
rm -f $TMP/smbset


 $DCMD --title "ENTER WINDOWS USERNAME PASSWORD" \
 --inputbox "Enter the Password for the Windows Username" 0 0 \
 $WINPASS 2> $TMP/smbset

if [ $? = 1 -o $? = 255 ]; then
 rm -f $TMP/smbset
 exit
fi
WINPASS="`cat $TMP/smbset`"
rm -f $TMP/smbset


$DCMD --title "SMB Share Setup" \
--yesno "Here are the settings that will be used. \n\
Server=$SERVERNAME \n\
Share=$SHARENAME \n\
Username=$WINUSER \n\
Password=NOT SHOWN \n\
Are these ok?" 0 0 
if [ $? = 0 ]; then
OKAY="YES"
fi
}


createshare()
{

if [ ! -d "$HOME/.smbshares" ]; then
mkdir $HOME/.smbshares
fi

if [ ! -d "$HOME/mnt" ]; then
mkdir $HOME/mnt
fi

if [ ! -d "$HOME/mnt/$SERVERNAME" ]; then
mkdir $HOME/mnt/$SERVERNAME
fi

if [ ! -d "$HOME/mnt/$SERVERNAME/$SHARENAME" ]; then
mkdir $HOME/mnt/$SERVERNAME/$SHARENAME
fi

if [ -d "$HOME/Choices" ]; then
Choices="Choices"
fi

if [ -d "$HOME/.Choices" ]; then
Choices=".Choices"
fi

if [ "$Choices" = "" ]; then

$DCMD --title "Problem Encountered"  --msgbox \
"Can't find a valid ROX-Filer desktop setup.\n\
\n\
Exiting now." 0 0

exit 1

fi


echo "smbmount //$SERVERNAME/$SHARENAME $HOME/mnt/$SERVERNAME/$SHARENAME -o rw,username=$WINUSER,password=$WINPASS" > $HOME/.smbshares/.mount$SERVERNAME$SHARENAME
echo "rox -n $HOME/mnt/$SERVERNAME/$SHARENAME" >> $HOME/.smbshares/.mount$SERVERNAME$SHARENAME
echo "smbumount $HOME/mnt/$SERVERNAME/$SHARENAME" >> $HOME/.smbshares/.mount$SERVERNAME$SHARENAME
chmod 701 $HOME/.smbshares/.mount$SERVERNAME$SHARENAME

sed -e 's|</pinboard>|<icon x="320" y="200" label="'$SERVERNAME'-'$SHARENAME'">'$HOME'/.smbshares/.mount'$SERVERNAME$SHARENAME'</icon>|g' $HOME/$Choices/ROX-Filer/pb_default > $TMP/pb_default
echo '</pinboard>' >> $TMP/pb_default
rm -f $HOME/$Choices/ROX-Filer/pb_default
mv $TMP/pb_default $HOME/$Choices/ROX-Filer/

sed -e 's|</special-files>||g' $HOME/$Choices/ROX-Filer/globicons > $TMP/globicons
echo '  <rule match="'$HOME'/.smbshares/.mount'$SERVERNAME$SHARENAME'">' >>$TMP/globicons
echo '    <icon>/usr/share/icons/ROX-noia/apps/samba.png</icon>' >>$TMP/globicons
echo '  </rule>' >>$TMP/globicons
echo '</special-files>' >>$TMP/globicons
rm -f $HOME/$Choices/ROX-Filer/globicons
mv $TMP/globicons $HOME/$Choices/ROX-Filer

if [ ! $DISPLAY = "" ]; then
killall -KILL ROX-Filer
launch-roxpinboard &
fi

$DCMD --title "Completed" --msgbox "The Icon on the desktop has been created \n\
 and is called $SERVERNAME-$SHARENAME.\n\
Doubleclicking on the icon will mount the share and open it in ROX-Filer.\n\
As soon as the ROX-Filer window is closed, the share will be unmounted." 0 0
 
}

if [ -z "`skill -n ROX-Filer &> /dev/null`" ]; then
    message "ERROR" "ROX-Filer is not running"
    exit 1
fi

if ! which smbtree &> /dev/null; then
    message "ERROR" "Cannot find Samba"
    exit 1
fi


main
