#!/bin/sh
#(c) Copyright Barry Kauler, February 2017. License: GPL V3 (/usr/share/doc/legal)
#called from /usr/sbin/easy-containers
#passed param is name of container. offer to load any extra sfs files.
#170523 replaced 'EC_LAYER' with 'EASY_LAYER'
#170523 may have a glob wildcard, ex: EASY_LAYER_RO1=devx*.sfs
#170527 fixes.

EC_MANAGE="$1"
[ ! "$EC_MANAGE" ] && exit

export TEXTDOMAIN=easy-containers
export OUTPUT_CHARSET=UTF-8

. /etc/rc.d/PUPSTATE #has BOOT_DEV, BOOT_FS, BOOT_DIR, WKG_DEV, WKG_FS, WKG_DIR, QSFS_PATH
. /etc/DISTRO_SPECS
mkdir -p /tmp/easy_containers
touch /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_MANAGE}/configuration

echo -n '' > /tmp/easy_containers/sfs-list
SFSVARS="$(grep '^EASY_LAYER_RO' /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_MANAGE}/configuration | cut -f 2 -d '=' | cut -f 2 -d "'" | sed -e 's%\.%\\.%g' -e 's%\*%.*%g' | grep -v '^$')" #170523 convert glob * to regular-expression. 170527
echo -n "$SFSVARS" > /tmp/easy_containers/sfs-list-wanted #170527

EXTRASFSS="$(find /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION} -maxdepth 1 -type f -name '*.sfs' | grep -v -E '/q\.sfs|/session\.sfs|/rw\-[0-9]*\.sfs' | rev | cut -f 1 -d '/' | rev)"
if [ ! "$EXTRASFSS" ];then
  SFSXML="<text><label>There are no extra SFS packages to load</label></text>"
else
 CNT=1
 for aSFS in `echo -n "$EXTRASFSS" | tr '\n' ' '`
 do
  if [ "$(echo -n "$aSFS" | grep -f /tmp/easy_containers/sfs-list-wanted)" == "" ];then
   DEFCHK='false'
  else
   DEFCHK='true'
  fi
  SFSCHECK="${SFSCHECK}<checkbox><variable>SFSCHK${CNT}</variable><label>${aSFS}</label><default>${DEFCHK}</default></checkbox>"
  echo "SFSCHK${CNT}=${aSFS}" >> /tmp/easy_containers/sfs-list
  CNT=`expr $CNT + 1`
 done
 . /tmp/easy_containers/sfs-list
 SFSXML="${SFSCHECK}"
fi



export EC_SFS_DLG1="<window title=\"$(gettext "Easy Containers: manage SFS")\" icon-name=\"gtk-convert\">
<vbox>
  <text use-markup=\"true\"><label>\"$(gettext 'Each container in Easy Linux runs on its own layered filesystem. The bottom read-only layer is <i>q.sfs</i>, the entire Easy Linux filesystem. Above q.sfs, it is possible to insert more read-only layers. Choose from the available list:')\"</label></text>
  ${SFSXML}
  <text use-markup=\"true\"><label>\"
<b>$(gettext 'Technical notes')</b>
$(gettext 'Extra SFS files are located in the repository folder of the currently-running version of Easy Linux.')\"</label></text>
<text><label>$(gettext 'Repository folder:') /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION}</label></text>
<text><label>$(gettext 'The user may download and manually place .sfs files in this folder.')</label></text>

  <hbox>
    <button><label>$(gettext 'Commit')</label><action>exit:sfsload</action></button>
    <button cancel></button>
  </hbox>
</vbox>
</window>"

RETSTRING1="$(gtkdialog --program=EC_SFS_DLG1 --center)"
[ $? -ne 0 ] && exit
eval "$RETSTRING1"

if [ "$EXIT" == "sfsload" ];then
 #170523 a text string would be helpful...
 sed -i '/Easy layer variables are numbered/d' /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_MANAGE}/configuration
 echo "#Easy layer variables are numbered, higher number, higher layer. 'RO0' is lowest, reserved for q.sfs" >> /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_MANAGE}/configuration
 #remove all existing sfs entries...
 grep -v '^EASY_LAYER_RO' /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_MANAGE}/configuration > /tmp/easy_containers/tmp-configuration
 cp -f /tmp/easy_containers/tmp-configuration /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_MANAGE}/configuration
 #append new...
 CNT=1; FILELIST=""
 for aSFSCHK in `echo "$RETSTRING1" | grep '^SFSCHK' | grep '"true"' | cut -f 1 -d '=' | tr '\n' ' '`
 do
  SFSFILE="$(grep "^${aSFSCHK}=" /tmp/easy_containers/sfs-list | cut -f 2 -d '=')"
  #170523 convert so has glob wildcard...
  SFSPREFIX="$(echo -n "$SFSFILE" | grep -o '^[^0-9]*' | sed -e 's%-$%%')"
  xSFSFILE="${SFSPREFIX}-*.sfs"
  echo "EASY_LAYER_RO${CNT}=${xSFSFILE}" >> /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_MANAGE}/configuration
  FILELIST="${FILELIST} ${xSFSFILE}"
  CNT=`expr $CNT + 1`
 done
 if [ $CNT -eq 1 ];then #170527
  pupdialog --timeout 10 --background '#80FF80' --backtitle "Extra SFSs" --msgbox "No extra SFS files will be loaded at bootup" 0 0
 else
  pupdialog --timeout 5 --background '#80FF80' --backtitle "Extra SFSs selected" --msgbox "These extra SFS files will be loaded:
${FILELIST}" 0 0
 fi
fi

###end###
