#!/bin/bash
a=sfslinux@gmail.com 
v="171129" #sfs
[ "$1" = "--help" ] && echo "Usage: $0 [directory]
    mkpfs, pfsmergr GUI " && exit
TMPDIR="/tmp/`basename $0`-$$"
m="выберите каталог с файлами или модулями"
dir3="выберите каталог и имя модуля.pfs"
[ "$1" ] && dir2="$1.pfs" || dir2="$dir3"
echo "$dir2" >$TMPDIR

if [ "$1" ] ;then
    [ "$1" = "%F" ] && dir="$m" || dir="$1" 
else    dir="$m"
fi

TDIR(){
if [ ! -d "$1" ] ;then
    [ ! "$2" ] && (ntf -e "Ошибка" "$1 не каталог" &) && ($0 &) && exit 
    else  dir2="`basename "$1"`.pfs"
fi
}
TDIR "$1" +
which pfs && www="http://wiki.puppyrus.org/puppyrus/pr218/pfs3" || www="http://wiki.puppyrus.org/puppyrus/pr218/pfs"
MAIN_DIALOG='	
<window title="pfs-mk v.'$v'"  window-position="1" icon-name="application-pfs">
 <vbox>
    <text>
      <label>Создание модуля.pfs из каталога</label>
    </text>
   <frame Входной каталог:                                                 ->
   <hbox>
    <entry activates_default="true" accept="directory" icon="gtk-open">
#     <label>Выберите или создайте директорию:</label>
     <variable>d</variable>
	 <input>echo "'$dir'"</input>
#         <default>'$1'</default>
#	 <input>echo $curl-$ssh-$win</input>
#	<input file>'"$TMPDIR"'</input>
				<action>echo `basename "$d"`.pfs >'$TMPDIR'</action>
				<action>refresh:d2</action>

    </entry>

    <button tooltip-text="'$m'">
     <input file icon="gtk-directory"></input>
     <action type="fileselect">d</action>
    </button>
   </hbox>

   </frame>

   <frame Выходной модуль.pfs:                                                 ->
   <hbox>
    <entry activates_default="true" accept="directory" icon="gtk-open">
     <variable>d2</variable>
#	 <input>echo "'$dir2'"</input>
#         <default>'$1'</default>
#	 <input>echo $curl-$ssh-$win</input>
	<input file>'"$TMPDIR"'</input>
    </entry>

    <button tooltip-text="'$dir3'">
     <input file icon="gtk-directory"></input>
     <action type="fileselect">d2</action>
    </button>
   </hbox>

   <hbox>

   <button can-default="true" has-default="true" tooltip-text="Объединение модулей.pfs 
или каталогов с распакованными .pfs
из входного каталога в мета-модуль.pfs">
    <label>Склеить</label>
    <input file icon="gtk-add"></input>
    <action>EXIT:mrg</action>
   </button>

   <button can-default="true" has-default="true" tooltip-text="Создание модуля.pfs из 
файлов входного каталога">
    <label>Создать модуль</label>
    <input file icon="application-pfs"></input>
    <action>EXIT:mk</action>
   </button>
   </hbox>

   </frame>

    <hbox>
   <button tooltip-text="WWW справка">
    <label>Справка</label>
    <input file icon="gtk-help"></input>
    <action>defaultbrowser "'$www'" &</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"
echo $ret #;exit
rm $TMPDIR*
#MAIN
[ "$EXIT" = "Cancel" ] && exit
[ "$EXIT" = "abort" ] && exit

TDIR "$d"
#echo d2=$d2-$m2 ;exit
if [ "$d2" != "$dir2" ] ;then
    TDIR "`dirname $d2`" ; to="$d2"
    [ -d "$d2" ] && (ntf -e "Ошибка" "$to каталог, а не файл" &) && ($0 &) && exit
# ntf -a "sudo mkpfs "$d" "$to""
fi

[ "$d" = "$m" ] || [ ! "$d" ] && (ntf -e "Ошибка" "Каталог не выбран" &) && ($0 &) && exit

mkpfs=mkpfs
if [ "$EXIT" = "mk" ] ;then
     #ntf -w  "wt "sudo mkpfs "$d" "$to"" d=$d d2=$d2" 
    if [ "`echo "$d$d2"| grep " "`" ];then
        to="$d2"
        er="`sudo $mkpfs "$d" -o "$to" 2>&1`" && ntf -i "`realpath "$to"`" "успешно создан" || ntf -a "$er"
    else    
	 #ntf -w  "wt "sudo mkpfs "$d" "$to"" d=$d d2=$d2" 
	#sudo mkpfs "$d" -o "$to"
	wt "sudo $mkpfs "$d" -o "$to""
    fi
else 
    [ ! "$to" ] && to="$d2" || to="`basename $d`.pfs"
    pfs="$(find "${d}" -mindepth 1 -maxdepth 1 -type f -name '*.pfs' 2>/dev/null)" && dm=m
    [ ! "$pfs" ] && pfs="$(find "${d}" -mindepth 1 -maxdepth 1 -type d  2>/dev/null)" && dm=d
    [ ! "$pfs" ] && (ntf -e "Ошибка" "$d не содержит модулей.pfs и каталогов с распакованными pfs" &) && ($0 &) && exit
    which pfs && pfsmerge="mkpfs -$dm" && o="-o" || pfsmerge=pfsmerge
    if [ "`echo "$d$to"| grep " "`" ];then
        er="`sudo $pfsmerge "$d" $o "$to" 2>&1`" && ntf -i "`realpath "$to"`" "успешно создан" || ntf -a "$er"
    else wt "sudo $pfsmerge "$d" $o "$to""
    fi
fi
