#!/bin/sh
#(c) copyright Barry Kauler 2017, bkhome.org
#Licence 2018 GPL V3 (/usr/share/doc/legal)
#called from the "sfsget" icon on the desktop, or via the Setup menu.
#181031 first version.
#181106 test for sha512sum or sha256sum
#181107 fix when no pkgs of current version. hide install btn.
#181108 rename 'load-list' to 'base-load-list'. fixes.
#181120 new button "INSTALL" when sfs already downloaded. extra install checking.
#181122 q*.sfs renamed to easy*.sfs  181124 fix.
#181123 took me by surprise, the "*" fed into for-loop got expanded, completely wrong.
#181124 fix bug.

export TEXTDOMAIN=sfsget
export OUTPUT_CHARSET=UTF-8
#export ORIGLANG="$LANG"
#export LANG=C

. /etc/rc.d/PUPSTATE #has PUP_HOME
. /etc/DISTRO_SPECS #has DISTRO_BINARY_COMPAT, DISTRO_COMPAT_VERSION DISTRO_DB_SUBNAME
. /root/.packages/DISTRO_PET_REPOS #has PET_REPOS, PACKAGELISTS_PET_ORDER, SFS_REPOS

[ ! "$SFS_REPOS" ] && exit 1
mkdir -p /tmp/sfsget

err_exit_func() {
 echo "${2}"
 popup "process=wait background=#ff8080 level=top terminate=ok|<b>${2}</b>"
 exit ${1}
}

check_hash_func() {
 #passed in: $DB_hash $DB_fullfilename
 aDB_hash="${1}"
 aDB_fullfilename="${2}"
 HASHLEN="$(echo -n "$aDB_hash" | wc -m)"
 case "$HASHLEN" in
  128) HASHEXE="sha512sum" ;;
  *)   HASHEXE="sha256sum" ;; #64
 esac
 HASH="$(${HASHEXE} ${aDB_fullfilename} | cut -f 1 -d ' ')"
 if [ "$HASH" != "$aDB_hash" ];then #181124
  M_f3="<b>${aDB_fullfilename}</b>
$(gettext 'File checksum wrong, aborting install')"
  popup "background=#ff8080 level=top terminate=ok process=wait|<big>${M_f3}</big>"
  [ -f ./${aDB_fullfilename} ] && rm -f ./${aDB_fullfilename} #181108 precaution.
  exit 1
 fi
}

export HLP_REPO="<window title=\"Help: SFS online repositories\" icon-name=\"gtk-index\" window_position=\"1\">
  <vbox>
    <text use-markup=\"true\">
     <label>\"$(gettext 'SFS packages are available in online repositories, for installation in EasyOS. You need to choose which repository, in the case of there being more than one.')

<b>$(gettext 'Technical notes')</b>
$(gettext 'Anyone can setup an SFS repository, and provide the URL for others to use. To setup, there are certain guidelines:')

$(gettext 'The SFS file must be created by the <b>dir2sfs</b> utility. This ensures that correct meta-data is created inside the SFS file. The procedure is simple, open a terminal where the folder is that is desired to convert to a SFS, and run <i>dir2sfs name-of-folder</i>. The folder needs to be named in the format <b>name_version_arch</b>')

$(gettext 'The actual SFS files are required to be stored three-deep in the repository. The dir2sfs utility provides guidance for this. Examples:')
 easyos/oe/pyro  puppy/slackware/14.2  puppy/ubuntu/16.04

$(gettext 'Users can add a new URL to the variable <b>SFS_REPOS</b>, found in file /root/.packages/DISTRO_PET_REPOS -- this is a space-delimited list of URLs.')\"</label>
      <variable>HLP_REPO</variable>
    </text>
    <hbox>
    <button>
      <label>Close</label>
      <action type=\"closewindow\">HLP_REPO</action>
    </button>
    </hbox>
  </vbox>
 </window>"

export HLP_SFSGET="<window title=\"Help: SFS packages\" icon-name=\"gtk-index\" window_position=\"1\">
  <vbox>
    <text use-markup=\"true\">
     <label>\"$(gettext 'An SFS package is, essentially, a folder that has been converted into a single file and made read-only and compressed. This file may be <i>mounted</i> as a filesystem and the original folder contents accessed.')

$(gettext 'As to what is inside an SFS file, it can be anything, from a single app such as LibreOffice, to a collection of apps on a theme such as graphics editing. Or, it may even be a complete Linux distribution.')

$(gettext 'Yes, a complete Linux distro, that you can run inside EasyOS, and flick into and out of!')

$(gettext 'SFS packages as implemented in EasyOS, also go by the name <b>EasyPak</b>, as they are <i>universal packages</i> similar to <b>FlatPak</b>, but simple to use and very light-weight. It is <b>EasyContainers</b> that makes SFS packages so powerful, our simpler alternative to the mainstream container mechanisms such as <b>Docker</b>.')

<b>$(gettext 'Installation')</b>
$(gettext 'After clicking the <b>DOWNLOAD</b> button, the package will be downloaded, and an offer made to install as a new container, or as a layer in an existing container, or as a layer in the main filesystem.')

<b>$(gettext 'NOTICE:')</b>
$(gettext 'On the desktop you will see an icon labeled <i>petget</i>, alongside <i>sfsget</i>. PETget is the traditional package manager, and you may use it to install themes and other small utilities. However, for the larger apps and improved security and maintainability, and of course to work with EasyContainers, please choose SFSget.')\"</label>
      <variable>HLP_SFSGET</variable>
    </text>
    <hbox>
    <button>
      <label>Close</label>
      <action type=\"closewindow\">HLP_SFSGET</action>
    </button>
    </hbox>
  </vbox>
 </window>"

export HLP_SFSINSTALL="<window title=\"Help: SFS package install\" icon-name=\"gtk-index\" window_position=\"1\">
  <vbox>
    <text use-markup=\"true\">
     <label>\"$(gettext 'Downloaded SFS file and dependencies may be found in /mnt/wkg/sfs. These may be used as required.')

<b>$(gettext 'New container')</b>
$(gettext 'A container will be created in /mnt/wkg/containers, usually with an icon on the desktop and menu entry to launch it.')

<b>$(gettext 'Existing container')</b>
$(gettext 'Containers are built with a layered filesystem. Any SFS many be inserted into the layers of an existing container.')

<b>$(gettext 'Main filesystem')</b>
$(gettext 'The EasyOS desktop is not a container, but it is built with layered SFS files, with easy.sfs at the bottom. SFS files may be inserted, for example the <b>devx</b> SFS file.')

<b>$(gettext 'Do not install')</b>
$(gettext 'Do nothing with the downloaded SFS file for now. Later, <b>Easy BootManager</b> and <b>Easy Container Management</b> in the Filesystem menu may be used to create containers and manage insertion of SFS files.')

<b>$(gettext 'Technical notes:')</b>
$(gettext 'Every downloaded SFS file has an associated <b>.specs</b> file. Look at these for further information about the capabilities of the SFS.')\"</label>
      <variable>HLP_SFSINSTALL</variable>
    </text>
    <hbox>
    <button>
      <label>Close</label>
      <action type=\"closewindow\">HLP_SFSINSTALL</action>
    </button>
    </hbox>
  </vbox>
 </window>"


###DB_repo###
for ONECHOICE in ${SFS_REPOS}
do
 aREPO="$(echo -n "$ONECHOICE" | cut -f 2 -d '|')"
 ITEMSREPO="${ITEMSREPO}<item>${aREPO}</item>"
done


#  window_position=\"1\"
export SFSGET_DLG1="<window title=\"$(gettext 'SFSget: SFS package installer')\" icon-name=\"gtk-about\">
  <vbox>
    
    <text use-markup=\"true\">
      <label>\"<b>$(gettext 'SFS packages are large apps, or collections of apps, that may be installed either on the main desktop or in containers.')</b>\"</label>
    </text>

    <text>
      <label>\"$(gettext 'SFSget is the preferred mechanism for installing packages in EasyOS. For more information about what SFS packages are, click the Help button at bottom-right of this window.')\"</label>
    </text>

    <text>
      <label>\"  \"</label>
    </text>
    
    <frame $(gettext 'STEP1: Choose repository')>
      <hbox>
        <text space-expand=\"true\">
          <label>$(gettext 'Please choose the SFS online repository:')</label>
        </text>
        <button>
          <input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input>
          <action type=\"launch\">HLP_REPO</action>
        </button>
      </hbox>
      <combobox width-request=\"480\">
        <variable>DB_repo</variable>
	    ${ITEMSREPO}
      </combobox>
    </frame>

    <text>
      <label>\"  \"</label>
    </text>
    
    <frame $(gettext 'STEP2: Browse repository')>
      <hbox>
        <vbox>
          <text>
            <label>\"$(gettext 'Having chosen the online repository in STEP1, the SFS files may now be browsed, and one chosen for download and installation.')\"</label>
          </text>
          <text>
            <label>\"$(gettext 'Click the button to browse:')\"</label>
          </text>
        </vbox>
        <button>
          <input file>/usr/share/pixmaps/puppy/package_sfs.svg</input>
          <action type=\"exit\">BROWSE</action>
        </button>
      </hbox>
    </frame>

    <text>
      <label>\"  \"</label>
    </text>
    
    <hbox>
      <button cancel></button>
      <button>
        <input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input>
        <action type=\"launch\">HLP_SFSGET</action>
      </button>
    </hbox>
  </vbox>
</window>"

RETPARAMS="$(gtkdialog --program=SFSGET_DLG1 --center)"
[ $? -ne 0 ] && exit 0
eval "$RETPARAMS"
[ "$EXIT" != "BROWSE" ] && exit 0

###browse###
repostring="$(echo -n "$DB_repo" | sed -e 's%[^a-zA-Z0-9]%%g')"
if [ ! -s /tmp/sfsget/SFS_PROBE_${repostring} ];then
 MSG1="$(gettext 'Please wait, probing online repository...')"
 popup "background=#ffc040 level=top terminate=never|<big><b>${MSG1} </b></big>"
 SFS_PROBE="$(wget -4 -t 2 -T 20 --waitretry=20 --spider --recursive --level=4 -P /tmp/sfsget --accept "*.sfs" -S ${DB_repo}/  2>&1 | grep '\.sfs$' | rev | cut -f 1-4 -d '/' | rev)"
 #ex returned line: puppy/t2/racy/racy_5.5-r0_i686.sfs
 echo "$SFS_PROBE" > /tmp/sfsget/SFS_PROBE_${repostring}
 sync
 killall popup
fi
SFS_PROBE="$(cat /tmp/sfsget/SFS_PROBE_${repostring})"

if [ "$SFS_PROBE" == "" ];then
 MSG2="$(gettext 'ERROR: something wrong, no SFS files found. Aborting.')"
 popup "background=#ff8080 level=top terminate=ok|<big><b>${MSG2} </b></big>"
 exit 1
fi

PATHS="$(echo -n "${SFS_PROBE}" | cut -f 1,2,3 -d '/' | sort -u)"
for aPATH in $PATHS
do
 [ ! "$aPATH" ] && continue
 mkdir -p /mnt/wkg/sfs/${aPATH}
done

#download icons and .specs files...
for aSPEC in ${SFS_PROBE}
do
 aSFS="${aSPEC##*/}" #ex: racy_5.5-r0_i686.sfs
 aPATH="${aSPEC%/*}" #ex: puppy/t2/racy
 aNAME="${aSFS%%_*}" #ex: racy
 aPKGNAME="${aSFS/.sfs/}" #ex: racy_5.5-r0_i686
 if [ ! -f /mnt/wkg/sfs/${aPATH}/${aPKGNAME}.specs ];then
  wget -4 -P /mnt/wkg/sfs/${aPATH} ${DB_repo}/${aPATH}/${aPKGNAME}.specs
 fi
 if [ ! -f /mnt/wkg/sfs/${aPATH}/${aNAME}.png ];then
  wget -4 -P /mnt/wkg/sfs/${aPATH} ${DB_repo}/${aPATH}/${aNAME}.png
 fi
done

###main gui###
PATHRADIO=""
for aPATH in $PATHS
do
 [ ! "$aPATH" ] && continue
 aTREE1="$(ls /mnt/wkg/sfs/${aPATH}/*.specs | sort | head -n 1 | rev | cut -f 2- -d '.' | cut -f 1 -d '/' | rev).sfs" #ex: racy_5.5-r0_amd64.sfs
 PATHRADIO="${PATHRADIO}
 <radiobutton>
  <label>${aPATH}</label>
  <action signal=\"button-release-event\">echo -n ${aPATH} > /tmp/sfsget/current-path</action>
  <action signal=\"button-release-event\">/usr/local/sfsget/filterpkgs ${aPATH}</action>
  <action signal=\"button-release-event\">/usr/local/sfsget/pkgtext-compose ${aTREE1}</action>
  <action signal=\"button-release-event\">echo -n ${aTREE1} > /tmp/sfsget/fullfilename</action>
  <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/sfsget/vis-data-flg )\">show:BOX_DATA</action>
  <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/sfsget/vis-data-flg )\">hide:BOX_DATA</action>

  <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/sfsget/vis-install-flg )\">show:BTN_DOWNLOAD</action>
  <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/sfsget/vis-install-flg )\">hide:BTN_DOWNLOAD</action>
  <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/sfsget/vis-install-flg )\">hide:PIX_DOWNLOADED</action>
  <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/sfsget/vis-install-flg )\">show:PIX_DOWNLOADED</action>
  <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/sfsget/vis-install-flg )\">hide:BTN_DOWNLOADED</action>
  <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/sfsget/vis-install-flg )\">show:BTN_DOWNLOADED</action>
  <action signal=\"button-release-event\">refresh:BTN_DOWNLOAD</action>
  <action signal=\"button-release-event\">refresh:PIX_DOWNLOADED</action>
  <action signal=\"button-release-event\">refresh:BTN_DOWNLOADED</action>

  <action signal=\"button-release-event\">refresh:TREE1</action>
  <action signal=\"button-release-event\">refresh:PKGICON</action>
  <action signal=\"button-release-event\">refresh:PKGTEXT</action>
  <action signal=\"button-release-event\">refresh:FULLFILENAME</action>
  <action signal=\"button-release-event\">refresh:BOX_DATA</action>
 </radiobutton>"
done
currentpath="$(echo "$PATHS" | head -n 1)"
echo -n "$currentpath" > /tmp/sfsget/current-path

/usr/local/sfsget/filterpkgs ${currentpath} #ex: puppy/slackware/14.2
#...creates /tmp/sfsget/filterpkgs.results.tree, and symlink /tmp/sfsget/name.png

TREE1="$(cat /tmp/sfsget/filterpkgs.results.tree | head -n 1 | cut -f 1 -d '|')" #ex: devx_0.9.7.1_amd64.sfs
/usr/local/sfsget/pkgtext-compose "${TREE1}"
#...creates /tmp/sfsget/pkgtext
echo -n "${TREE1}" > /tmp/sfsget/fullfilename

if [ -f /mnt/wkg/sfs/${currentpath}/${TREE1} ];then #181107 ex: /mnt/wkg/sfs/puppy/t2/racy/racy_5.5-r0_i686.sfs
 VIS_DOWNLOAD="false"
 VIS_DOWNLOADED="true"
else
 VIS_DOWNLOAD="true"
 VIS_DOWNLOADED="false"
fi
test_installed_func() {
 tPATH="$(cat /tmp/sfsget/current-path)"
 tPKG="$(cat /tmp/sfsget/fullfilename)"
 if [ -f /mnt/wkg/sfs/${tPATH}/${tPKG} ];then
  echo -n "false" > /tmp/sfsget/vis-install-flg
 else
  echo -n "true" > /tmp/sfsget/vis-install-flg
 fi
}
export -f test_installed_func

VIS_DATA='true'
[ "$TREE1" == "" ] && VIS_DATA='false'

export SFSGET_MAINDLG="<window title=\"$(gettext 'SFSget package installer')\" icon-name=\"gtk-about\" allow-grow=\"false\">
  <vbox>
    <hbox>
      <vbox>
        <text><label>\"  \"</label></text>
        <text><label>$(gettext 'Paths:')</label></text>
        ${PATHRADIO}
      </vbox>
      <vbox>
        <tree>
          <label>$(gettext 'Package|Description')</label>
          <height>200</height><width>568</width>
          <variable>TREE1</variable>
          <input icon-column=\"1\">cat /tmp/sfsget/filterpkgs.results.tree</input>
          <action signal=\"button-release-event\">ln -snf /usr/share/pixmaps/\${TREE1/_*/}.png /tmp/sfsget/name.png</action>
          <action signal=\"button-release-event\">/usr/local/sfsget/pkgtext-compose \${TREE1}</action>
          <action signal=\"button-release-event\">echo -n \${TREE1} > /tmp/sfsget/fullfilename</action>
          
          <action signal=\"button-release-event\">test_installed_func</action>
          <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/sfsget/vis-install-flg )\">show:BTN_DOWNLOAD</action>
          <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/sfsget/vis-install-flg )\">hide:BTN_DOWNLOAD</action>
          <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/sfsget/vis-install-flg )\">hide:PIX_DOWNLOADED</action>
          <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/sfsget/vis-install-flg )\">show:PIX_DOWNLOADED</action>
          <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/sfsget/vis-install-flg )\">hide:BTN_DOWNLOADED</action>
          <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/sfsget/vis-install-flg )\">show:BTN_DOWNLOADED</action>
          <action signal=\"button-release-event\">refresh:BTN_DOWNLOAD</action>
          <action signal=\"button-release-event\">refresh:PIX_DOWNLOADED</action>
          <action signal=\"button-release-event\">refresh:BTN_DOWNLOADED</action>
          
          <action signal=\"button-release-event\">refresh:PKGICON</action>
          <action signal=\"button-release-event\">refresh:PKGTEXT</action>
          <action signal=\"button-release-event\">refresh:FULLFILENAME</action>
        </tree>
      </vbox>
    </hbox>
    
    <hbox visible=\"${VIS_DATA}\">
      <vbox>
        <pixmap>
          <height>96</height>
          <width>96</width>
          <input file>/tmp/sfsget/name.png</input>
          <variable>PKGICON</variable>
        </pixmap>
        <text>
         <input file>/tmp/sfsget/fullfilename</input>
         <variable>FULLFILENAME</variable>
        </text>
        <hbox homogenous=\"true\">
          <button space-expand=\"true\" visible=\"${VIS_DOWNLOAD}\">
            <label>$(gettext 'DOWNLOAD')</label>
            <variable>BTN_DOWNLOAD</variable>
            <action>exit:DOWNLOAD</action>
          </button>
          <pixmap space-expand=\"true\" visible=\"${VIS_DOWNLOADED}\">
            <width>48</width>
            <height>48</height>
            <input file>/usr/share/pixmaps/easyshare/tick_green.svg</input>
            <variable>PIX_DOWNLOADED</variable>
          </pixmap>
        </hbox>
        <hbox homogenous=\"true\">
          <button space-expand=\"true\" visible=\"${VIS_DOWNLOADED}\">
            <label>$(gettext 'INSTALL')</label>
            <variable>BTN_DOWNLOADED</variable>
            <action>exit:INSTALL</action>
          </button>
        </hbox>
      </vbox>
      <vbox>
        <text use-markup=\"true\">
          <input file>/tmp/sfsget/pkgtext</input>
          <variable>PKGTEXT</variable>
        </text>
      </vbox>
      <variable>BOX_DATA</variable>
    </hbox>
    
    <hbox>
      <pixmap space-expand=\"false\" space-fill=\"false\">
        <width>24</width>
        <height>24</height>
        <input file>/usr/share/pixmaps/easyshare/tick_green.svg</input>
      </pixmap>
      <text space-expand=\"false\" space-fill=\"false\">
        <label>$(gettext ' = already downloaded')</label>
      </text>
      <text space-expand=\"true\" space-fill=\"true\"><label>\" \"</label></text>
      <button>
        <label>$(gettext 'Quit')</label>
        <action type=\"exit\">QUIT</action>
      </button>
      <button>
        <input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input>
        <action type=\"launch\">HLP_SFSGET</action>
      </button>
    </hbox>

  </vbox>
</window>"

echo "$SFSGET_MAINDLG" > /tmp/sfsget/SFSGET_MAINDLG
RETPARAMS="$(gtkdialog --program=SFSGET_MAINDLG --center)"
[ $? -ne 0 ] && exit 0
echo "$RETPARAMS" > /tmp/sfsget/RETPARAMS_MD
eval "$RETPARAMS"
OK=1
[ "$EXIT" == "DOWNLOAD" ] && OK=0
[ "$EXIT" == "INSTALL" ] && OK=0
[ $OK -eq 1 ] && exit

###download###
#$DB_repo is the online URL.
#/tmp/sfsget/current-path is the path inside URL, ex: puppy/t2/racy
#/tmp/sfsget/fullfilename is selected SFS, ex: racy_5.5-r0_i686.sfs
echo -n '' > /tmp/sfsget/sfs-deps-list
DB_path="$(cat /tmp/sfsget/current-path)"
DB_fullfilename="$(cat /tmp/sfsget/fullfilename)"
echo "$DB_fullfilename" > /tmp/sfsget/sfs-load-list
CURRDIR="`pwd`"
cd /mnt/wkg/sfs/${DB_path}

if [ "$EXIT" == "DOWNLOAD" ];then
 download_file ${DB_repo}/${DB_path}/${DB_fullfilename}
 if [ $? -ne 0 ];then
  M_f1="<b>${DB_fullfilename}</b>
$(gettext 'Failed to download, aborting install')"
  popup "background=#ff8080 level=top terminate=ok process=wait|<big>${M_f1}</big>"
  [ -f ./${DB_fullfilename} ] && rm -f ./${DB_fullfilename} #181108 precaution.
  exit 1
 fi

 FULLPKGNAME="$(basename "$DB_fullfilename" .sfs)"
. /mnt/wkg/sfs/${DB_path}/${FULLPKGNAME}.specs

 #check hash... 181106 length...
 check_hash_func "${DB_hash}" "${DB_fullfilename}"
else #already downloaded...
 FULLPKGNAME="$(basename "$DB_fullfilename" .sfs)"
. /mnt/wkg/sfs/${DB_path}/${FULLPKGNAME}.specs
fi #download

#181123 took me by surprise, the "*" fed into for-loop got expanded, completely wrong...
DEPS="$(echo -n "$DB_dependencies" | tr ',' ' ' | sed -e 's%\*%STAR%g')"

#download deps, if not already...
#download deps...
#ex: DB_dependencies=rust_3.9,yabbo,easy_1.*
for aDEPSPEC0 in ${DEPS} #181123
do
 aDEPSPEC="$(echo -n "$aDEPSPEC0" | sed -e 's%STAR%*%g')" #181123 change it back to *
 aDEPNAME="${aDEPSPEC/_*/}"
 aDEPVER="${aDEPSPEC#*_}"
 [ "$aDEPNAME" == "$aDEPVER" ] && aDEPVER="*"
 PTNspec="${aDEPNAME}_${aDEPVER}*.specs"
 FNDspec="$(ls -1 /mnt/wkg/sfs/${DB_path}/${PTNspec} | sort --field-separator='_' --key=2 --version-sort | tail -n 1)" #181123
 if [ "$FNDspec" ];then
  DEPfullfilename="$(grep '^DB_fullfilename=' ${FNDspec} | cut -f 2 -d "'")"
  DEPhash="$(grep '^DB_hash=' ${FNDspec} | cut -f 2 -d "'")"
  if [ ! -f /mnt/wkg/sfs/${DB_path}/${DEPfullfilename} ];then #181108 check already downloaded.
   download_file ${DB_repo}/${DB_path}/${DEPfullfilename}
   if [ $? -ne 0 ];then
    M_f2="<b>${DEPfullfilename}</b>
$(gettext 'Failed to download, aborting install')"
    popup "background=#ff8080 level=top terminate=ok process=wait|<big>${M_f2}</big>"
    [ -f /mnt/wkg/sfs/${DB_path}/${DEPfullfilename} ] && rm -f /mnt/wkg/sfs/${DB_path}/${DEPfullfilename} #181108 precaution.
    exit 1
   fi
   check_hash_func "${DEPhash}" "${DEPfullfilename}"
  fi
  echo "${DEPfullfilename}" >> /tmp/sfsget/sfs-load-list
  echo "${DEPfullfilename}" >> /tmp/sfsget/sfs-deps-list
 fi
done

###insert###
#181108 offer to insert as layer into existing container, or main f.s.
M_deps=""
if [ -s /tmp/sfsget/sfs-deps-list ];then
 M_deps="$(gettext 'As well as these dependencies:')
  $(cat /tmp/sfsget/sfs-deps-list | tr '\n' ' ')"
fi
case "$DB_install" in
 desk)
  INSTALLXML="
  <text><label>$(gettext 'This SFS is designed to be installed as a new container only. It will run as a complete desktop, with a icon on the main desktop to launch it.')</label></text>
  <text><label>\"  \"</label></text>
      <hbox>
        <text><label>$(gettext 'Install as a new container:')</label></text>
        <button><label>$(gettext 'NEW')</label><action>exit:NEW</action></button>
      </hbox>
  <text><label>\"  \"</label></text>
  <text use-markup=\"true\"><label>\"<b>$(gettext 'Notice')</b>
$(gettext 'These install options are not being offered:')
 <i>$(gettext 'Insert as layer in existing container')</i>
 <i>$(gettext 'Insert as layer in the main filesystem')</i>\"</label></text>"
 ;;
 *)
  DEPS="$(echo -n "${DB_dependencies}" | tr ',' ' ' | tr ' ' '\n')"
  QDEP="$(echo "$DEPS" | grep -E '^easy$|^easy_')"
  case "$QDEP" in
   easy) QOK='yes' ;;
   easy_*)
    QVER="${QDEP/*_/}"
    QVERptn="$(echo -n "$QVER" | sed -e 's%\*%.*%')" #convert glob wildcard * to .*
    QOK="$(echo -n "${DISTRO_VERSION}" | grep "${QVERptn}")"
   ;;
   *) QOK='' ;;
  esac
  if [ "$QOK" == "" ];then
   INSTALLQXML="
     <text><label>\"  \"</label></text>
     <text use-markup=\"true\"><label>\"<b>$(gettext 'Notice')</b>
$(gettext 'This install option is not being offered, as easy.sfs is not a dependency:')
 <i>$(gettext 'Insert as layer in the main filesystem')</i>\"</label></text>"
  else
   INSTALLQXML="      <hbox>
        <text><label>$(gettext 'Insert as layer in the main filesystem:')</label></text>
        <button><label>$(gettext 'MAIN')</label><action>exit:MAIN</action></button>
      </hbox>"
  fi
  INSTALLXML="
  <text><label>$(gettext 'Please choose to install as a new container, unless you really understand what the other options do...')</label></text>
  <text><label>\"  \"</label></text>
      <hbox>
        <text><label>$(gettext 'Install as a new container:')</label></text>
        <button><label>$(gettext 'NEW')</label><action>exit:NEW</action></button>
      </hbox>
  <text><label>\"  \"</label></text>
      <hbox>
        <text><label>$(gettext 'Insert as layer in existing container:')</label></text>
        <button><label>$(gettext 'EXISTING')</label><action>exit:EXISTING</action></button>
      </hbox>
      ${INSTALLQXML}"
 ;;
esac

export SFSGET_INSTALLDLG="<window title=\"$(gettext 'SFSget package installer')\" icon-name=\"gtk-about\" allow-grow=\"false\">
  <vbox>
    <text>
      <label>\"$(gettext 'This SFS package has been downloaded:')
  /mnt/wkg/sfs/${DB_path}/${DB_fullfilename}
${M_deps}\"</label>
    </text>
    
    <frame $(gettext 'Install')>
      ${INSTALLXML}
    </frame>
    
    <hbox>
      <text><label>$(gettext 'Click EXIT button to not install:')</label></text>
      <button><label>$(gettext 'EXIT')</label><action>exit:NOINSTALL</action></button>
      <button>
        <input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input>
        <action type=\"launch\">HLP_SFSINSTALL</action>
      </button>
    </hbox>
  </vbox>
</window>"

echo "$SFSGET_INSTALLDLG" > /tmp/sfsget/SFSGET_INSTALLDLG
RETPARAMS="$(gtkdialog --program=SFSGET_INSTALLDLG --center)"
[ $? -ne 0 ] && exit 0
echo "$RETPARAMS" > /tmp/sfsget/RETPARAMS_ID
eval "$RETPARAMS"

if [ "$EXIT" == "NOINSTALL" ];then
 if [ -s /tmp/sfsget/sfs-deps-list ];then
  M_noinstall="$(gettext 'The SFS package has been downloaded, but not installed. It has not been deleted, so can be installed at any future date.')"
 else
  M_noinstall="$(gettext 'The SFS package and dependencies have been downloaded, but not installed. They have not been deleted, so can be installed at any future date.')"
 fi
 popup "background=#80ff80 level=top terminate=ok process=wait|${M_noinstall}"
 exit
fi

if [ "$EXIT" == "EXISTING" ];then
 #easy-containers
 /usr/local/easy_containers/extra-sfss-ec
 exit
fi

if [ "$EXIT" == "MAIN" ];then
 /usr/local/easy_containers/extra-sfss-repo
 exit
fi

###new container###
[ "$EXIT" != "NEW" ] && exit
if [ -d /mnt/wkg/containers/${DB_nameonly} ];then
 M_new1="<big>$(gettext 'New Container:') <b>${DB_nameonly}</b></big>
$(gettext 'Sorry, but a container of this name already exists:')
 /mnt/wkg/containers/${DB_nameonly}
$(gettext 'Aborting this install. If you want to delete the existing container, go to Easy Container Mnagement in the Filesystem menu.')"
 popup "background=#ff8080 level=top terminate=ok process=wait|${M_new1}"
 exit
fi
#will have to mount it, to get out the 'configuration' file...
mkdir -p mntpt
busybox mount -t squashfs ${DB_fullfilename} mntpt
mkdir -p /usr/local/easy_containers/templates/${DB_nameonly}
#/usr/local/easy_containers/setup-container will read this...
cp -f --remove-destination mntpt/.control/${DB_nameonly}.configuration /usr/local/easy_containers/templates/${DB_nameonly}/configuration

###setup container###
/usr/local/easy_containers/setup-container ${DB_nameonly}

#/usr/local/easy_containers/start-container will read this...
cp -f mntpt/.control/${DB_nameonly}.specs /mnt/wkg/containers/${DB_nameonly}/specs

#specify which sfs files are to be loaded, start-container will use this...
echo -n "" > /mnt/wkg/containers/${DB_nameonly}/base-load-list #181108
for aSFS in `cat /tmp/sfsget/sfs-load-list`
do
 [ "$aSFS" == "" ] && continue
 ln -s ../../sfs/${DB_path}/${aSFS} /mnt/wkg/containers/${DB_nameonly}/${aSFS/_*/}.sfs
 echo "${aSFS/_*/}.sfs" >> /mnt/wkg/containers/${DB_nameonly}/base-load-list #ex: devx.sfs  181108
done


#create desktop icon and menu entry...
/usr/local/easy_containers/ec-create-desktop-icons-menu ${DB_path} ${FULLPKGNAME}

sync
busybox umount mntpt
rmdir mntpt






###end###
