#!/bin/bash
prg=sfspass
a=sfslinux@gmail.com 
v="140731"
prg="Шифрование файлов"
urxvt=defaultterminal
tmp=`mktemp`
[ ! "`which zip``which gpg`" ] && ntf -e "Утилиты gzip и gpg не установлены" "установите" && exit
msg="read -p \"

Нажмите ENTER для выхода 
\" r"

TSTP() {
	[ "`which $1`" = "" ] && ntf -e "Программа $1" "не обнаружена"  && exit
}

FSEL () {
export DIALOG='	
<window title="'$prg' v.'$v'"  window-position="1" icon-name="gtk-properties">
 <hbox>
 <vbox scrollable="true">

   <frame '$1':>
   <hbox>
    <entry accept="filename">
     <variable>f</variable>
	 <input>echo "'$2'"</input>
    </entry>
    <button>
     <input file stock="gtk-open"></input>
     <action type="fileselect">f</action>
    </button>
    </hbox>

   </frame>

 <hbox>
       <button ok></button>
       <button cancel></button>
 </hbox>

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

ret="`gtkdialog --program=DIALOG --center`"
eval "$ret"
echo $ret; echo $EXIT;echo $f
[ "$f" = "'$2'" ] && ntf -e "ОШИБКА" "Файл не выбран"  && exit
[  "$EXIT" = "Cancel" ] && exit
[  "$EXIT" = "abort" ] && exit
}


MAIN () {
export MAIN_DIALOG='
<window title="'$prg' v.'$v'"  window-position="1" icon-name="gtk-save" resizable="false">
<vbox>
    <text>
      <label>Шифрование файлов</label>
    </text>
    <hbox>

    <frame объект:>
    <combobox>
      <variable>fd</variable>
      <item>файл</item>
      <item>каталог</item>
    </combobox>
    </frame>
    </hbox>

    <hbox>
    <frame метод:>
    <combobox>
      <variable>m</variable>
'
[ "`which zip`" ] && export MAIN_DIALOG=$MAIN_DIALOG'
      <item>zip</item>
'

export MAIN_DIALOG=$MAIN_DIALOG'
      <item>gpg</item>
    </combobox>

    </frame>
    </hbox>


    <hbox>

    <button>
     <input file stock="gtk-redo"></input>
     <action type="exit">exit by enc</action>
     <label>Зашифровать</label>
    </button>

    <button>
     <input file stock="gtk-undo"></input>
     <action type="exit">exit by dec</action>
     <label>Расшифровать</label>
    </button>
    
    <button>
     <input file stock="gtk-quit"></input>
     <label>Выход</label>
    </button>
    
    </hbox>

</vbox>
</window>
'


ret="`gtkdialog --program=MAIN_DIALOG --center`"
eval "$ret"
echo $ret #;exit


case "$EXIT" in
"exit by enc" ) 
echo "exit by enc"
TSTP $m
if [ "$fd" = "файл" ] ;then
	    FSEL "Шифруемый файл" "выберите файл"
	    fadd=$f
	else
export DIALOG='	
<window title="'$prg' v.'$v'"  window-position="1" icon-name="gtk-properties">
 <hbox>
 <vbox scrollable="true" width="400" >


   <frame Каталог шифрования:>
   <hbox>
    <entry accept="directory">
     <variable>d</variable>
	 <input>echo "выберите каталог"</input>
    </entry>
    <button>
     <input file icon="gtk-open"></input>
     <action type="fileselect">d</action>
    </button>
    </hbox>

   </frame>

 <hbox>
       <button ok></button>
       <button cancel></button>
 </hbox>

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

ret="`gtkdialog --program=DIALOG --center`"
eval "$ret"
echo $ret; echo $EXIT;echo $f
[ "$d" = "выберите каталог" ] && ntf -e  "ОШИБКА" "Каталог не выбран"   && exit
fadd=$d
[  "$EXIT" = "Cancel" ] && exit
[  "$EXIT" = "abort" ] && exit
fi


msg2="read -p \"

Нажмите ENTER для выхода, d для удаления зашифрованного $fadd
\" r"

GPG () {
	echo 	"#!/bin/sh
		gpg -o $fenc -c  $fadd && echo \"$fadd успешно зашифрован\"
		if [ \"`echo $fadd |egrep '.tar.bz2$'`\" = \"\" ] ;then
		    #file
		if [ \"\$r\" = \"d\" ] ;then
			rm -vIR $fenc && $msg
		    else
			$msg
		fi
			
		    else
		    #dir
			 rm $fadd ;$msg2 

		if [ \"\$r\" = \"d\" ] ;then
			rm -vIR $fadd0 && $msg
		    else
			$msg
		fi
		
		fi
		" >$tmp
	$urxvt -e sh $tmp 
	
}

if [ "$m" = "gpg" ] ;then
	if [ "$fd" = "каталог" ] ;then
	    FSEL "Шифрованный файл" "$fadd.tar.bz2.$m"
	    fenc=$f
	    cd $fadd && \
	    tar cf - "`pwd`" | bzip2 -f > "$fadd.tar.bz2" && \
	    fadd0=$fadd
	    fadd=$fadd.tar.bz2
	    GPG 
	else
	    FSEL "Шифрованный файл" "$fadd.$m"
	    fenc=$f
	    GPG
	fi
    else
	    FSEL "Шифрованный файл" "$fadd.$m"
	    fenc=$f
	echo "#!/bin/sh
	    cd `dirname $fadd`
	    zip -e -r  $fenc `basename $fadd` ;$msg2
	    [ \"\$r\" = \"d\" ] && rm -vIR $fadd && $msg" >$tmp
	$urxvt -e sh $tmp

fi
    ;;
"exit by dec" ) 
FSEL "Шифрованный файл" "выберите файл"


if [ "$m" = "gpg" ] ;then
#	TSTP gpg
	echo "gpg $f && echo ОК;$msg" >$tmp
	$urxvt -e sh $tmp
    else
#	TSTP unzip
	echo "unzip $f  && echo ОК;$msg" >$tmp
	$urxvt -e sh $tmp
fi
    ;;
"Help" ) 
    HLP;MAIN
    ;;
*       ) exit;;
esac
}
rm $tmp
MAIN
