#!/bin/bash
a=sfslinux@gmail.com 
v="160412" #sfs

w1="webdav.yandex.ru" ;ww1="https://disk.yandex.ru"
w2="dav.2safe.com" ;ww2="https://2safe.com"
d="/mnt/net" ; ip="192.168.0.1" ; l="Администратор" ; pass="111111" ; sh="share"
sudo="`which sudo`"
#TMPDIR=/tmp/gtkdialog/examples/"`basename $0`"
#TMPDIR=/tmp/"`basename $0`"
TMPDIR="/tmp/`basename $0`-$$"
#mkdir -p "$TMPDIR"

funcbtnCreate() {
	echo '<button image-position="'$2'">
			<label>"'"$3"'"</label>
			<input file stock="'$4'"></input>
			<action>echo "'"$5"' '$1'"</action>
			<action function="'"$3"'">'$1'</action>
		</button>'
}

funcrdoCreate() {
	echo '<label>'"$2"'</label>
			<variable>'$1'</variable>
#			<action>echo '$1' changed to $'$1'</action>
		</radiobutton>'
}

MAIN() {
echo /mnt/net >$TMPDIR
hd="https://wiki.archlinux.org/index.php/Davfs"
hf="https://wiki.archlinux.org/index.php/Curlftpfs"
hs="https://wiki.archlinux.org/index.php/Sshfs"
hw="https://wiki.archlinux.org/index.php/Samba_(Русский)#.D0.9C.D0.BE.D0.BD.D1.82.D0.B8.D1.80.D0.BE.D0.B2.D0.B0.D0.BD.D0.B8.D0.B5_.D1.80.D0.B5.D1.81.D1.83.D1.80.D1.81.D0.BE.D0.B2_Samba"
echo "$hw" >$TMPDIR-w
MAIN_DIALOG='	
<window title="Сетевые ресурсы v.'$v'"  window-position="1" icon-name="gtk-connect">
 <vbox>
	<hbox homogeneous="true">
		<frame Выбор сетевого ресурса для монтирования:>
<hbox homogeneous="false">
			<radiobutton>
				<action>echo "'$hd'" >'$TMPDIR-w'</action>
				<action>echo /mnt/net >'$TMPDIR'</action>
				<action>refresh:d</action>
				<action>if true disable:ip</action>
				<action>if true disable:sh</action>
				'"`funcrdoCreate r_yan "$w1"`"'
#<hbox homogeneous="true">
			<radiobutton tooltip-text="Для анонимного ftp оставьте 
поля Логин и Пароль пустыми">
				<action>echo "'$hf'" >'$TMPDIR-w'</action>
				<action>echo /mnt/ftp >'$TMPDIR'</action>
				<action>refresh:d</action>
				<action>if true enable:ip</action>
				<action>if true enable:sh</action>
				'"`funcrdoCreate r_curl "FTP"`"'
			<radiobutton>
				<action>echo "'$hs'" >'$TMPDIR-w'</action>
				<action>echo /mnt/ssh >'$TMPDIR'</action>
				<action>refresh:d</action>
				<action>if true enable:ip</action>
				<action>if true enable:sh</action>
				'"`funcrdoCreate r_ssh "SSH"`"'
			<radiobutton active="true">
				<action>echo "'$hw'" >'$TMPDIR-w'</action>
				<action>echo /mnt/win >'$TMPDIR'</action>
				<action>refresh:d</action>
				<action>if true enable:ip</action>
				<action>if true enable:sh</action>
				'"`funcrdoCreate r_win "сеть Windows"`"'
</hbox>
 		</frame>
	</hbox>

   <frame Параметры подключения:>
    <hbox>
    <text width-request="70">
      <label>IP:</label>
    </text>
	<entry><variable>ip</variable>
        <default>'$ip'</default>
	</entry>
    </hbox>

    <hbox>
    <text width-request="70">
      <label>Каталог:</label>
    </text>
	<entry>
        <default>'$sh'</default>
	<variable>sh</variable>
	</entry>
    </hbox>

    <hbox>
    <text width-request="70">
      <label>Логин:</label>
    </text>
	<entry activates_default="true">
        <default>'$l'</default>
	<variable>l</variable>
	</entry>
    </hbox>

    <hbox>
    <text width-request="70">
     <label>Пароль:</label>
    </text>
    <entry activates_default="true" tooltip-text="Пароль для доступа">
#    <entry  activates_default="true">
       <default>'$pass'</default>
       <visible>password</visible>
       <variable>pass</variable>
     </entry>
     </hbox>

   </frame>

   <frame Точка монтирования:>
   <hbox>
#    <text>
#      <label>Точка монтирования:</label>
#    </text>
    <entry activates_default="true" accept="newdirectory" icon="gtk-open">
#     <label>Выберите или создайте директорию:</label>
     <variable>d</variable>
#	 <input>echo "выберите директорию"</input>
#         <default>'$d'</default>
#	 <input>echo $curl-$ssh-$win</input>
	<input file>'"$TMPDIR"'</input>
    </entry>

    <button>
     <input file icon="gtk-directory"></input>
     <action type="fileselect">d</action>
    </button>
   </hbox>
   </frame>

    <hbox>
   <button tooltip-text="Содержание изменяется 
от выбора ресурса">
    <label>Справка</label>
    <input file icon="gtk-help"></input>
    <action>defaultbrowser "`cat '$TMPDIR-w'`" &</action>
   </button>
   <button can-default="true" has-default="true">
    <label>Монтировать</label>
    <input file icon="gtk-connect"></input>
    <action>EXIT:OK</action>
   </button>
   <button cancel></button>
#  <button ok></button>

    </hbox>
    
 </vbox>
</window>'

export GUI="`echo "$MAIN_DIALOG" | sed -e 's/^#.*//'`"
ret="`gtkdialog --program=GUI --center`"

eval "$ret"
rm $TMPDIR*
#MAIN
#echo $ret ;exit
#echo r_win=$r_win;exit
case "$EXIT" in
"OK" ) 
[ "$d" = "выберите директорию" ] && ntf -a "ОШИБКА" "Не выбрана точка монтирования"  && MAIN

[ -d $d ] || `which sudo` mkdir -p  "$d"

ERR() { ntf -a "ОШИБКА! Ресурс НЕ монтирован" "$e" && MAIN ; }
OK()  { ntf -i "ОК! \"//$ip/$sh\"" "монтирован в \"$d\"" ; }
#ERR;OK;exit

if [ "$r_win" = "true" ] ;then
#echo "mount -t cifs -o user=$l,pass=$pass,codepage=866,iocharset=utf8 //$ip/$sh $d"
#mount -t cifs -o user=$l,pass=$pass,codepage=866,iocharset=utf8 //$ip/$sh $d && ntf -i "ОК" "\"//$ip/$sh\" монтирован в \"$d\"" && exit
#echo "mount -t cifs -o user=$l,pass=$pass,iocharset=utf8 //$ip/$sh $d"
e="`$sudo mount -t cifs -o user=$l,pass=$pass,iocharset=utf8 //"$ip"/"$sh" "$d" 2>&1`" && OK && exit
ERR

elif [ "$r_curl" = "true" ] ;then
#$sudo curlftpfs  ftp.yandex.ru/puppyrus/ 1 -o user="anonymous:pass"
[ "$l" ] || l=anonymous
#wt "$sudo curlftpfs  "$ip"/"$sh"/ "$d" -o user="$l:$pass" -o allow_other 2>&1" #&& OK && exit
e="`$sudo curlftpfs  "$ip"/"$sh"/ "$d" -o user="$l:$pass" -o allow_other 2>&1`" && OK && exit
#$sudo echo "curlftpfs#$l:$pass@$ip/$sh $d fuse auto,user,uid=0,allow_other,_netdev 0 0" >>/etc/fstab 
ERR

elif [ "$r_ssh" = "true" ] ;then
e="`$sudo DROPBEAR_PASSWORD="$pass" sshfs "$l"@"$ip":"$sh" "$d" -o allow_other 2>&1`" && OK && exit
#urxvt -e "sshfs "$l"@"$ip":"$sh" "$d"" && ntf -i "ОК" "\"//$ip/$sh\" монтирован в \"$d\"" && exit
ERR
fi

#[ "$r_2s" = "true" ]  && `which sudo` mount-dav "$w2" "$l" "$pass" "$d"
#[ "$r_yan" = "true" ] && `which sudo` mount-dav "$w1" "$l" "$pass" "$d"
[ "$r_2s" = "true" ]  && `which sudo` wdfs https://"$w2" "$d" -o username="$l",password="$pass"
#[ "$r_yan" = "true" ] && `which sudo` wdfs https://"$w1" "$d" -o username="$l",password="$pass"
[ "$r_yan" = "true" ] && wt "`which sudo` wdfs https://"$w1" "$d" -o username="$l" -o allow_other -p "$pass"" "WebDav подключение..." && defaultfilemanager "$d" &
;;
*       ) exit;;
esac
}
MAIN

