#!/bin/sh
#written for easyos, to convert a folder into a sfs file.
#181027 first release.
#181029 remove DB_repo, do not want hard-coded url.
#181031 <nameonly>.specs inside sfs, <pkgname_arch>.specs outside.
#181102 first release.
#181105 hack to fix rox startup in container. popup fix. audio applet fix.
#181105 reshape the backround image. cutdown jwm menu some more.
#181106 what if there is already a <folder>/.control/${NAME}.specs?
#181106 change sha512sum to sha256sum.
#181108 fix when no .desktop file. fix icon in .desktop files.
#181109 allow arch to be "src" or "all".
#181115 fixes for xenialpup. improve icon find. 181116
#181117 start-up scripts in /.control in sfs.
#181217 tweak some delays, reports still get mangled background sometimes.
#181225 change "q_" to "easy_"
#190806 buster, slow usb stick, still getting corrupted wallpaper for easy container.
#190911 "easy" container name changed to "pyro" or "buster".
#191006 bug fix.
#191009 maybe fix Name field in .desktop. 191029
#200601 .desktop file needs to be EasyPup compatible. see also /sbin/fixlayers in intrd, sfsget, ec-create-desktop-icons-menu

. /etc/rc.d/PUPSTATE
. /etc/DISTRO_SPECS
mkdir -p /tmp/sfsget
xPWD="`pwd`"
export LANG=C

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

[ ! $DISPLAY ] && err_exit_func 1 "X required for this script. Aborting."
PARAM1="$1"
[ ! "$PARAM1" ] && err_exit_func 2 "No parameter passed-in when called this script. Aborting."
[ ! -d "$PARAM1" ] && err_exit_func 3 "Folder ${PARAM1} does not exist. Aborting."
PATH1="$(dirname "PARAM1")" #if nothing, then "."
DIR="$(basename "$PARAM1" /)" #also drops any tailing "/'
#...expected to be folder of form <name>_<version>[-revision][_<arch>]

if [ "$(echo -n "$DIR" | grep '[a-zA-Z]_[0-9]')" == "" ];then
 echo "Error: folder must be of the format <name>_<version>[-revision][_<arch>]"
 err_exit_func 4 "Aborting."
fi

NAME="${DIR/_*/}"
VERARCH="${DIR#*_}"
VERREV="${VERARCH/_*/}"
ARCH="$(echo -n "$VERARCH" | cut -f 2- -d '_')"
[ "$VERREV" == "$ARCH" ] && ARCH="`arch`"
case "$ARCH" in
 x86_64) ARCH="amd64" ;;
 i*86)    true ;;
 amd64)   true ;;
 aarch64) true ;;
 src|all) true ;; #181109
 *)
  echo "Sorry, only i[456]86, amd64 (x86_64) and aarch64 are supported."
  err_exit_func 5 "Aborting."
 ;;
esac
VER="${VERREV/-*/}"
REV="$(echo -n "$VERREV" | cut -f 2- -d '-')"
[ "$VER" == "$REV" ] && REV=''

#meta-data inside the SFS...
# ${NAME}.specs ${NAME}.configuration
# ${NAME}.configuration (will end up in /usr/local/easy_containers/templates/${NAME}/configuration
#i suggest a folder, /.control will have these files.

#${NAME}.specs to be multi-line, with these variables:
# DB_pkgname DB_nameonly DB_version DB_revision DB_arch DB_categories DB_size DB_path
# DB_fullfilename DB_dependencies DB_description DB_description_long DB_compileddistro DB_compiledrelease
# DB_apps DB_icons DB_install DB_hash 
#DB_categories: comma-separated list of menu categories (for each app).
#DB_apps:       comma-separated list of applications. note, name of executables will be inside the <app>.desktop
#DB_icons:      comma-separated list of icons (incl. extension), for each executable.
#DB_install:    individual|desk|terminal --launch as individual icons on main desktop, or as one desktop icon, or as one terminal on main desktop --latter two, will use $NAME as name of container.
#DB_hash:       sha256sum or sha512sum checksum for the sfs file.


###install mode###
#need to decide this in advance...
##############
/usr/local/sfsget/sfsspec-mode ${NAME}
##############
#...will write to /tmp/sfsget/sfsspec-install-mode
[ $? -ne 0 ] && err_exit_func 6 "/usr/local/sfsget/sfsspec-mode failed. Aborting."
DB_install="$(cat /tmp/sfsget/sfsspec-install-mode)"


###apps###
ICON=""
mkdir -p ${PATH1}/${DIR}/usr/share/applications
case "$DB_install" in
 individual)
  if [ -f ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktopBACKUP ];then
   mv -f ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktopBACKUP ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktop
  fi
  #use the name of the .desktop files...
  APPS="$(find ${PATH1}/${DIR}/usr/share/applications -maxdepth 1 -type f -name '*.desktop' | rev | cut -f 1 -d '/' | cut -f 2- -d '.' | rev | sort)"
  DB_apps="$(echo -n "$APPS" | tr '\n' ',')"
  if [ -f ${PATH1}/${DIR}/usr/share/pixmaps/${NAME}48.png ];then #181117
   ICON="${NAME}48.png"
  elif [ -f ${PATH1}/${DIR}/usr/share/pixmaps/${NAME}.png ];then
   ICON="${NAME}.png"
  fi
 ;;
 desk)
  #only one icon on desktop...
  APPS="$NAME"
  DB_apps="$NAME"
  if [ -f ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktop ];then
   mv -f ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktop ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktopBACKUP
  fi
  mkdir -p ${PATH1}/${DIR}/usr/share/pixmaps
  if [ -f /usr/share/doc/${NAME}48.png ];then
   cp /usr/share/doc/${NAME}48.png ${PATH1}/${DIR}/usr/share/pixmaps/ 2>/dev/null
   ICON="${NAME}48.png"
  elif [ -f ${PATH1}/${DIR}/usr/share/pixmaps/${NAME}48.png ];then #181115
   ICON="${NAME}48.png"
  else
   ICON="${NAME}"
  fi
  #this does not appear in the menu...
  echo "[Desktop Entry]
NoDisplay=true
Name=${NAME}
Exec=xxxxxxxxxxx
Icon=${ICON}
Categories=X-Utility" > ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktop
 ;;
 *) #terminal
  #only one icon on desktop...
  APPS="$NAME"
  DB_apps="$NAME"
  if [ -f ${PATH1}/${DIR}/usr/share/pixmaps/${NAME}48.png ];then #181118
   ICON="${NAME}48.png"
  elif [ -f ${PATH1}/${DIR}/usr/share/pixmaps/${NAME}.png ];then
   ICON="${NAME}.png"
  fi
  if [ -f ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktop ];then
   mv -f ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktop ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktopBACKUP
  fi
  #this does not appear in the menu...
  echo "[Desktop Entry]
NoDisplay=true
Name=${NAME}
Exec=xxxxxxxxxxx
Icon=${NAME}
Categories=X-Utility" > ${PATH1}/${DIR}/usr/share/applications/${NAME}.desktop
 ;;
esac

###category###
. /etc/xdg/menus/hierarchy #has PUPHIERARCHY variable.
DB_categories=""
for aDT in $APPS
do
 [ "$aDT" == "" ] && continue
 CAT=""
 #read Categories from .desktop file, see if any match with puppy menu hierarchy...
 CATS="$(grep '^Categories=' ${PATH1}/${DIR}/usr/share/applications/${aDT}.desktop | cut -f 2 -d '=' | tr ';' ' ')" #may be ; delimited
 for aCAT in $CATS
 do
  aCATptn="[ ,]${aCAT}[ ,]"
  CAT="$(echo "$PUPHIERARCHY" | grep "$aCATptn" | grep -E '^Desktop|^System|^Setup|^Utility|^Filesystem|^Graphic|^Document|^Business|^Personal|^Network|^Internet|^Multimedia|^Fun' | tr -s ' ' | cut -f 1,2 -d ' ' | tr -d ' ' | sed 's%:Sub%%' | tr ':' ';')" #ex: Multimedia;sound
  [ "$CAT" ] && break
 done
 #not found, fallback to looking in categories.dat...
 if [ ! "$CAT" ];then
  aDTptn=" ${aDT} "
  CAT="$(grep -i "$aDTptn" /usr/local/petget/categories.dat | grep '^PKGCAT' | head -n 1 | cut -f 1 -d '=' | cut -f 2,3 -d '_' | sed 's%_Sub%%' | tr '_' ';')" #ex: System;security
 fi
 [ ! "$CAT" ] && CAT="BuildingBlock"
 if [ "$DB_categories" == "" ];then
  DB_categories="$CAT"
 else
  DB_categories="${DB_categories},${CAT}"
 fi
done


###icons###
DB_icons=""
CNTicon=1
mkdir -p ${PATH1}/${DIR}/usr/share/pixmaps
mkdir -p ${PATH1}/${DIR}/usr/share/icons
cp -f /usr/sbin/pngoverlay /tmp/sfsget/
#can get these out of the .desktop files...
for aDT in $APPS
do
 [ "$aDT" == "" ] && continue
 [ "${ICON}" == "" ] && ICON="$(grep '^Icon=' ${PATH1}/${DIR}/usr/share/applications/${aDT}.desktop | cut -f 2 -d '=')"
 #want to create a containerized-icon in /usr/share/pixmaps...
 fndICON=''
 [ ! "$ICON" ] && ICON=zzzzzzzz
 [ -f "${PATH1}/${DIR}${ICON}" ] && fndICON="${PATH1}/${DIR}${ICON}"
 [ ! "$fndICON" ] && [ -f "$ICON" ] && fndICON="$ICON"
 [ ! "$fndICON" ] && fndICON="$(find ${PATH1}/${DIR}/usr/share/icons -mindepth 1 -type f -name "$ICON" -o -name "${ICON}.*" | grep '48x48/apps/' | head -n 1)"
 [ ! "$fndICON" ] && fndICON="$(find ${PATH1}/${DIR}/usr/share/pixmaps -mindepth 1 -type f -name "$ICON" -o -name "${ICON}.*" | head -n 1)"
 [ ! "$fndICON" ] && fndICON="$(find /usr/local/lib/X11/pixmaps -mindepth 1 -type f -name "$ICON" -o -name "${ICON}.*" | head -n 1)"
 [ ! "$fndICON" ] && fndICON="$(find ${PATH1}/${DIR}/usr/share/icons -mindepth 1 -type f -name "$ICON" -o -name "${ICON}.*" | grep 'scalable/apps/' | head -n 1)"
 if [ ! "$fndICON" ];then #fallback
  fndCAT="$(echo -n "$DB_categories" | cut -f ${CNTicon} -d ',' | tr ';' '-')"
  if [ "$fndCAT" ];then
   if [ -f /usr/local/lib/X11/mini-icons/mini-${fndCAT}.xpm ];then #ex: mini-Utility-development.xpm
    fndICON="/usr/local/lib/X11/mini-icons/mini-${fndCAT}.xpm"
   fi
  fi
 fi
 [ ! "$fndICON" ] && fndICON="/usr/share/pixmaps/puppy/execute.svg" #final fallback.
 IMGTYPE="$(echo -n "$fndICON" | rev | cut -f 1 -d '.' | rev)" #ex: xpm
 IMGBASE="$(basename "${fndICON}" .${IMGTYPE})"
 picscale -i "$fndICON" -o /tmp/sfsget/${aDT}.png 48 48 9 #vovchik
 cd /tmp/sfsget
 cp -f /usr/local/lib/X11/pixmaps/ec-overlay48.png ./
 ./pngoverlay ${aDT}.png ec-overlay48.png ec-${aDT}48.png
 cd $xPWD #oh my, pngoverlay has changed $PWD, have to use a different name!
 cp -a -f /tmp/sfsget/ec-${aDT}48.png ${PATH1}/${DIR}/usr/share/pixmaps/
 cp -a -f --remove-destination /tmp/sfsget/${aDT}.png ${PATH1}/${DIR}/usr/share/pixmaps/${aDT}48.png
 if [ "$DB_icons" == "" ];then
  DB_icons="$aDT"
 else
  DB_icons="${DB_icons},${aDT}"
 fi
 CNTicon=$(($CNTicon+1))
done
rm -f /tmp/sfsget/pngoverlay

####install###
#DB_install="individual"
#[ "DB_apps" == "" ] && DB_install="terminal" #no .desktop files, must run as a terminal. um, think 'desk' ok also.

###hash###
#cannot create hash *before* creating the sfs file!
DB_hash=""

###arch###
ARCH="`arch`"
case "$ARCH" in
 x86_64) ARCH="amd64" ;;
esac
DB_arch="$ARCH"

###basic puppy fields###
if [ "$REV" == "" ];then
 DB_pkgname="${NAME}_${VER}"
else
 DB_pkgname="${NAME}_${VER}-${REV}"
fi
DB_nameonly="${NAME}"
DB_version="${VER}"
DB_revision="${REV}"
DB_path="easyos/${DISTRO_BINARY_COMPAT}/${DISTRO_COMPAT_VERSION}" #ex: easyos/oe/pyro
DB_size="$(busybox du -s -k ${PATH1}/${DIR} | cut -f 1)"
DB_fullfilename="${DB_pkgname}_${ARCH}.sfs"
DB_dependencies="easy_${DISTRO_VERSION}"
DB_compileddistro="${DISTRO_BINARY_COMPAT}"
DB_compiledrelease="${DISTRO_COMPAT_VERSION}"
#DB_repo="distro.ibiblio.org/easyos/amd64/packages/sfs"

#181106 what if there is already a .control/${NAME}.specs?
DB_description_long=''
if [ -s ${PATH1}/${DIR}/.control/${NAME}.specs ];then
 DB_arch="$(grep '^DB_arch=' ${PATH1}/${DIR}/.control/${NAME}.specs | cut -f 2 -d "'")"
 DB_dependencies="$(grep '^DB_dependencies=' ${PATH1}/${DIR}/.control/${NAME}.specs | cut -f 2 -d "'")"
 DB_description="$(grep '^DB_description=' ${PATH1}/${DIR}/.control/${NAME}.specs | cut -f 2 -d "'")"
 DB_description_long="$(grep '^DB_description_long=' ${PATH1}/${DIR}/.control/${NAME}.specs | cut -f 2 -d "'")"
 DB_compileddistro="$(grep '^DB_compileddistro=' ${PATH1}/${DIR}/.control/${NAME}.specs | cut -f 2 -d "'")"
 DB_compiledrelease="$(grep '^DB_compiledrelease=' ${PATH1}/${DIR}/.control/${NAME}.specs | cut -f 2 -d "'")"
 DB_path="$(grep '^DB_path=' ${PATH1}/${DIR}/.control/${NAME}.specs | cut -f 2 -d "'")"
fi

###gui###
#bring up a gui to finalize <name>.specs file
#need to decide how this is going to run
# -- individual apps on main desktop
# -- in their own xephyr container, individual apps or as a desktop
echo "#prelimiary values, generated by dir2sfs:
DB_pkgname='$DB_pkgname'
DB_nameonly='$DB_nameonly'
DB_version='$DB_version'
DB_revision='$DB_revision'
DB_arch='$DB_arch'
DB_categories='$DB_categories'
DB_size='$DB_size'
DB_fullfilename='$DB_fullfilename'
DB_dependencies='$DB_dependencies'
DB_description='$DB_description'
DB_description_long='$DB_description_long'
DB_compileddistro='$DB_compileddistro'
DB_compiledrelease='$DB_compiledrelease'
DB_path='$DB_path'
DB_apps='$DB_apps'
DB_icons='$DB_icons'
DB_install='$DB_install'
DB_hash='$DB_hash'" > /tmp/sfsget/new-db-entry-fields0 #read by sfsspec
###############
/usr/local/sfsget/sfsspec
###############
[ $? -ne 0 ] && err_exit_func 7 "/usr/local/sfsget/sfsspec failed. Aborting."
#created by sfsspec...
. /tmp/sfsget/new-db-entry-fields


###update .desktop files###
CNTapp=1
for aAPP in `echo -n "$DB_apps" | tr ',' ' '`
do
 aIMAGE="$(echo -n "$DB_icons" | cut -f ${CNTapp} -d ',')"   #ex: abiword
 aCAT="X-$(echo -n "$DB_categories" | cut -f ${CNTapp} -d ',' | tr ';' '-')" #ex: X-Utility-development
 
 #200601 normal icon...
 if [ -f ${PATH1}/${DIR}/usr/share/pixmaps/${aIMAGE}48.png ];then
  xIMAGE=${aIMAGE}48.png
 elif [ -f ${PATH1}/${DIR}/usr/share/pixmaps/${aIMAGE}.png ];then
  xIMAGE=${aIMAGE}.png
 else
  xIMAGE=Executable.xpm
 fi
 
 #190911 if aAPP=easy then create container with name "pyro" or "buster"...
 xAPP="$aAPP"
 [ "easy" == "$aAPP" ] && xAPP="${DISTRO_COMPAT_VERSION}"
 
 case "$xAPP" in #191009
  chromium) xNAME="Chromium web browser" ;;
  firefox|firefox-esr) xNAME="Firefox web browser" ;; #191029
  *) xNAME="$xAPP" ;;
 esac
 
 #200601 prior, created .desktop for running in a container, and if sfs is on main desktop
 # then /sbin/fixlayers in intrd modified the .desktop for running on main desktop.
 # however, for EasyPup compatibility, want .desktop default for main desktop...
 if [ -f ${PATH1}/${DIR}/usr/share/applications/${xAPP}.desktop ];then
  cp -a -f ${PATH1}/${DIR}/usr/share/applications/${xAPP}.desktop ${PATH1}/${DIR}/usr/share/applications/${xAPP}.ec-desktop
  sed -i -e "s%^Categories=.*%Categories=${aCAT}%" ${PATH1}/${DIR}/usr/share/applications/${xAPP}.ec-desktop
  sed -i -e "s%^Icon=.*%Icon=container48.png%" ${PATH1}/${DIR}/usr/share/applications/${xAPP}.ec-desktop
  sed -i -e "s%^Exec=.*%Exec=ec-chroot-${xAPP}%" ${PATH1}/${DIR}/usr/share/applications/${xAPP}.ec-desktop
  sed -i -e "s%^Name=.*%Name=${xNAME}%" ${PATH1}/${DIR}/usr/share/applications/${xAPP}.ec-desktop
  #default is for running on main desktop...
  sed -i -e "s%^Categories=.*%Categories=${aCAT}%" ${PATH1}/${DIR}/usr/share/applications/${xAPP}.desktop
  sed -i -e "s%^Icon=.*%Icon=${xIMAGE}%" ${PATH1}/${DIR}/usr/share/applications/${xAPP}.desktop
  sed -i -e "s%^Exec=.*%Exec=${xAPP}%" ${PATH1}/${DIR}/usr/share/applications/${xAPP}.desktop
  sed -i -e "s%^Name=.*%Name=${xNAME}%" ${PATH1}/${DIR}/usr/share/applications/${xAPP}.desktop
 else
  #create it...
  echo "[Desktop Entry]
Type=Application
Name=${xNAME}
Comment=${DB_description}
Exec=ec-chroot-${xAPP}
Icon=container48.png
Terminal=false
Categories=${aCAT}" > ${PATH1}/${DIR}/usr/share/applications/${xAPP}.ec-desktop
  #create it...
  echo "[Desktop Entry]
Type=Application
Name=${xNAME}
Comment=${DB_description}
Exec=${xAPP}
Icon=${xIMAGE}
Terminal=false
Categories=${aCAT}" > ${PATH1}/${DIR}/usr/share/applications/${xAPP}.desktop
 fi
 
 
 CNTapp=$(($CNTapp+1))
done

###create /.control###
mkdir -p ${PATH1}/${DIR}/.control
cat /tmp/sfsget/new-db-entry-fields > ${PATH1}/${DIR}/.control/${DB_nameonly}.specs

###configuration###
#need '${DB_nameonly}.configuration', after installation
# will go into host /usr/local/easy_containers/templates/${DB_nameonly}/configuration
#some overrides...
echo -n "" > /tmp/sfsget/security-overrides
case "$DB_install" in
 desk)
  echo -e "EC_XSOCKET='unix'\nEC_XSERVER='xephyr'\nEC_NS_UNSHARE_IPC='true'" >> /tmp/sfsget/security-overrides
 ;;
esac
#############
/usr/local/sfsget/ec-security ${DB_nameonly}
#############
#...writes to /tmp/sfsget/ec-configuration
[ $? -ne 0 ] && err_exit_func 8 "/usr/local/sfsget/ec-security failed. Aborting."
cat /tmp/sfsget/ec-configuration > ${PATH1}/${DIR}/.control/${DB_nameonly}.configuration


###desk###
mkdir -p ${PATH1}/${DIR}/usr/bin
mkdir -p ${PATH1}/${DIR}/usr/sbin
mkdir -p ${PATH1}/${DIR}/.control
if [ "$DB_install" == "desk" ];then
 #there will be one icon on desktop, label same as name of container.
 #the way EC works, /.control/ec-run will look for /.control/$DB_nameonly after the chroot
 #model this after /,control/desk in the pre-created 'desk' container...
 #note, refer also script 3buildeasydistro
 #this is for a puppy-compatible distro...
 case "$DB_path" in
  *easyos*|*puppy*)
   [ -e ${PATH1}/${DIR}/.control/${DB_nameonly} ] && rm -f ${PATH1}/${DIR}/.control/${DB_nameonly}
   cat << 'EOF1' >> ${PATH1}/${DIR}/.control/${DB_nameonly}
#!/bin/sh
#this script generated by /usr/local/sfsget/dir2sfs
#PuppyPin backup...
if [ -f /root/Choices/ROX-Filer/PuppyPinBACKUP ];then
 touch /root/Choices/ROX-Filer/PuppyPin
 if [ "$(grep 'defaultbrowser' /root/Choices/ROX-Filer/PuppyPin)" == "" ];then
  cp -a -f /root/Choices/ROX-Filer/PuppyPinBACKUP /root/Choices/ROX-Filer/PuppyPin
 fi
fi
cp -a -f /root/Choices/ROX-Filer/PuppyPin /root/Choices/ROX-Filer/PuppyPinBACKUP

#181115 xenialpup puppy package manager needs this...
FREEM="$(df -m / | tail -n 1 | tr -s ' ' | cut -f 4 -d ' ')"
echo -n "${FREEM}" > /tmp/pup_event_sizefreem

#unexpected change of screen dimensions may cause desktop drive icons to be displaced...
SCRNXY="`xwininfo -root | grep -o '\-geometry .*' | tr -s ' ' | cut -f 2 -d ' ' | cut -f 1 -d '+'`"
SCRN_X="`echo -n "$SCRNXY" | cut -f 1 -d 'x'`"
SCRN_Y="`echo -n "$SCRNXY" | cut -f 2 -d 'x'`"
echo -n "$SCRN_X" > /tmp/pup_event_frontend_scrn_x #read by frontend_change.
echo -n "$SCRN_Y" > /tmp/pup_event_frontend_scrn_y #read by frontend_change.
if [ -f /var/local/pup_event_frontend_scrn_x ];then
 oldSCRNX="$(cat /var/local/pup_event_frontend_scrn_x)"
 oldSCRNY="$(cat /var/local/pup_event_frontend_scrn_y)"
 [ "$SCRN_X" != "$oldSCRNX" ] && echo "ICONWIPE" > /var/local/pup_event_icon_change_flag
 [ "$SCRN_Y" != "$oldSCRNY" ] && echo "ICONWIPE" > /var/local/pup_event_icon_change_flag
 #.../var/local/pup_event_icon_change_flag is read by /sbin/clean_desk_icons (see below).
fi
echo -n "$SCRN_X" > /var/local/pup_event_frontend_scrn_x
echo -n "$SCRN_Y" > /var/local/pup_event_frontend_scrn_y

#make sure /usr/local/bin in path:
[ "$(echo -n "$PATH" | grep '/usr/local/bin')" == "" ] && export PATH="/usr/local/bin:${PATH}"

/usr/bin/desk-delayedrun & #this will launch tray applets.

#relocates right-side icons to actual right-side of screen...
/usr/sbin/fixPuppyPin /root/Choices/ROX-Filer/PuppyPin

CR='
'

#start rox...
#181013 don't know how, rox picks up wallpaper etc from host rox (but corrupted)...
#181105 weird, but have to start rox, start jwm, kill then restart rox...
cp -f /root/Choices/ROX-Filer/globicons /root/.config/rox.sourceforge.net/ROX-Filer/
cp -f /root/Choices/ROX-Filer/PuppyPin /root/.config/rox.sourceforge.net/ROX-Filer/
echo '<?xml version="1.0"?>
<pinboard>
</pinboard>' > /root/Choices/ROX-Filer/PuppyPin0
rox -p /root/Choices/ROX-Filer/PuppyPin0

#start jwm...
jwm &
sleep 4 #0.5 sometimes not enough. 181217 increase from 2. 190202 increase from 2.5. 190806 now 4.
killall ROX-Filer
#put text on background...
if [ -x /usr/sbin/popup ];then
 popup "level=bottom placement=center background=#06ee9b|<b><big><big>Containerized desk ${CR}Press Alt-F6 to flip</big></big></b>"
elif [ -x /usr/bin/gtkdialog-splash ];then
 gtkdialog-splash -close never -fontsize large -text "Containerized desk${CR}Press Alt-F6 to flip" &
fi
rox -n -p /root/Choices/ROX-Filer/PuppyPin
###end###
EOF1
   chmod 755 ${PATH1}/${DIR}/.control/${DB_nameonly}
   chown spot:spot ${PATH1}/${DIR}/.control/${DB_nameonly}
   #181117 the run-once scripts...
   [ -f ${PATH1}/${DIR}/.control/${DB_nameonly}.run-once-base ] && rm -f ${PATH1}/${DIR}/.control/${DB_nameonly}.run-once-base
   [ -f ${PATH1}/${DIR}/.control/${DB_nameonly}.run-once-top ] && rm -f ${PATH1}/${DIR}/.control/${DB_nameonly}.run-once-top
   #the .run-once-base is called when sfs is in layer, not on top...
   #scenario, xenialpup sfs on bottom, an app sfs on top, just fix xenialpup enough to work...
   #this will be called from /.control/ec-run...
   cat << 'EOFrob' >> ${PATH1}/${DIR}/.control/${DB_nameonly}.run-once-base
#!/bin/sh
 #this script generated by /usr/local/sfsget/dir2sfs
 #181115 xenialpup has not precreated the pixbuf cache...
 GDKPIXBUFDIR="$(find /usr/lib* -type d -name 'gdk-pixbuf-2.0' | head -n 1)"
 if [ "${GDKPIXBUFDIR}" ];then
  if [ ! -e ${GDKPIXBUFDIR}/2.10.0/loaders.cache ];then
   gdk-pixbuf-query-loaders --update-cache
  fi
 fi
 #181115 xenialpup already has the icon-theme.cache files, but just in case...
 find /usr/share/icons -mindepth 1 -maxdepth 1 -type d |
 while read ALINE
 do
  [ "${ALINE}" == "" ] && continue
  [ ! -d "${ALINE}"/16x16 ] && continue
  gtk-update-icon-cache -f "${ALINE}"
 done
 #181105 check these...
 #fc-cache -s #regenerate fontconfig caches (in /var/cache/fontconfig).
 if [ -d /usr/share/glib-2.0/schemas ];then
  if [ ! -f /usr/share/glib-2.0/schemas/gschemas.compiled ];then
   if [ -e /usr/bin/glib-compile-schemas ];then
    /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
   fi
  fi
 fi
EOFrob
   chmod 755 ${PATH1}/${DIR}/.control/${DB_nameonly}.run-once-base
   chown spot:spot ${PATH1}/${DIR}/.control/${DB_nameonly}.run-once-base
   
   #the .run-once-top is called if sfs is on top layer...
   cat << 'EOFrot' >> ${PATH1}/${DIR}/.control/${DB_nameonly}.run-once-top
#!/bin/sh
 #this script generated by /usr/local/sfsget/dir2sfs
 #take out the pre-created "www", "console", "desk" container desktop icons...
 sed -i '/ec\-chroot\-/d' /root/Choices/ROX-Filer/PuppyPin
 #take out these desktop icons also...
 sed -i '/Connect/d' /root/Choices/ROX-Filer/PuppyPin
 sed -i '/EasyShare/d' /root/Choices/ROX-Filer/PuppyPin
 #missing, probably good to create it...
 touch /root/.packages/user-installed-packages
 #we need equivalent to /usr/sbin/delayedrun (which starts from /root/.xinitrc), which starts the tray applets.
 #see 'desk-delayedrun' below.
 #181115 xenialpup is launching the tray applets...
 # seems a bit weird, need to delete this...
 [ -d /root/.config/autostart ] && rm -rf /root/.config/autostart/*
 #181105 need to cutout some entries from /etc/xdg/templates/_root_.jwmrc ...
 sed -i '/puppy\-setup\.menu/d' /etc/xdg/templates/_root_.jwmrc
 sed -i '/"Shutdown"/,/^\s*$/{d}' /etc/xdg/templates/_root_.jwmrc #removed entire paragraph.
 sed -i '/label="Exit"/d' /etc/xdg/templates/_root_.jwmrc #181116 xenialpup.
 [ -f /root/.jwm/jwmrc-personal ] && sed -i '/defaultscreenshot/d' /root/.jwm/jwmrc-personal #181116 up-arrow launches screeny.
 [ -f /root/.jwm/jwmrc-personal2 ] && sed -i '/defaultscreenshot/d' /root/.jwm/jwmrc-personal2 #181116 up-arrow launches screeny.
 #have to run fixmenus, but firstly, delete some .desktop files...
 for aDT in BootManagerSTAR easy-STAR ec-STAR childproof cups Desktop-STAR easydd firewall-STAR floppy-STAR Internet-STAR loginmanager MoManagerSTAR MouseSTAR Multiple-Sound-Card-Wizard Partview Pdisk-STAR Pmount-STAR printer-STAR PuddSTAR qsync QuickSetup remove-builtin Set-STAR TopSTAR video-STAR
 do
  rm -f /usr/share/applications/${aDT/STAR/*}.desktop 2>/dev/null
 done
 #and some apps...
 for aDT in bluepup GPartedSTAR gpptp hardinfo pschedule Puppy-Event-Manager PupScanSTAR Pup-SysInfo pupXSTAR xinput_calibrator wcpufreq
 do
  rm -f /usr/share/applications/${aDT/STAR/*}.desktop 2>/dev/null
 done
 #181105 and for some old pups...
 for aDT in Floppy_Formatter Format-floppy-disk Gpptp GrubSTAR legacy_grub_2013 mtp_detect pbackup pgprs-connect PupsaveSTAR Remaster-STAR Resize-personal-storage-file
 do
  rm -f /usr/share/applications/${aDT/STAR/*}.desktop 2>/dev/null
 done
 fixmenus
 #181105 reshape the backround image...
 /usr/sbin/background_reshape
 #...ok for old pups, easyos and quirky need extra code...
 RET1=$?
 if [ $RET1 -eq 11 ];then
  if [ -s /tmp/qwallpaper_reshaped ];then
   NEWIMAGE="`cat /tmp/qwallpaper_reshaped`"
   if [ -f $NEWIMAGE ];then #sanity check.
    #truncated image created, update rox...
    iPTN="s%>.*</backdrop>%>${NEWIMAGE}</backdrop>%"
    sed -i -e "$iPTN" /root/Choices/ROX-Filer/PuppyPin
   fi
  fi
 fi
 #181115 xenialpup needs fix for desktop install icon...
 if [ -f /usr/sbin/dotpup ];then
  echo '#!/bin/sh
exec /usr/local/petget/pkg_chooser.sh' > /usr/sbin/dotpup
  chmod 755 /usr/sbin/dotpup
 fi
EOFrot
   chmod 755 ${PATH1}/${DIR}/.control/${DB_nameonly}.run-once-top
   chown spot:spot ${PATH1}/${DIR}/.control/${DB_nameonly}.run-once-top
   
   #and the delayedrun script...
   [ -f ${PATH1}/${DIR}/usr/bin/desk-delayedrun ] && mv -f ${PATH1}/${DIR}/usr/bin/desk-delayedrun ${PATH1}/${DIR}/usr/bin/desk-delayedrunORIG
   cat << 'EOF2' >> ${PATH1}/${DIR}/usr/bin/desk-delayedrun
#!/bin/sh
#this script generated by /usr/local/sfsget/dir2sfs
#give enough time for jwm to start...
sleep 4.5 #must be >2  181217 increase from 3. 190202 increase from 3.5. 190806 increased from 4.
if ! pidof jwm >/dev/null ;then sleep 2; fi
if ! pidof jwm >/dev/null ;then sleep 4; fi

if [ -x /root/Startup/pnmixer_tray ];then
 if ! pidof pnmixer >/dev/null; then
  /root/Startup/pnmixer_tray &
 fi
elif [ -x /root/Startup/retrovol_tray ];then
 if ! pidof retrovol >/dev/null; then
  /root/Startup/retrovol_tray &
 fi
elif [ -x /usr/bin/retrovol ];then #181115
 retrovol -hide &
fi
EOF2
   chmod 755 ${PATH1}/${DIR}/usr/bin/desk-delayedrun
   chown spot:spot ${PATH1}/${DIR}/usr/bin/desk-delayedrun
  ;;
 esac
 #container launch script...
 if [ "$DB_nameonly" == "easy" ];then #190911
  echo "#!/bin/sh
empty -f ec-chroot ${DISTRO_COMPAT_VERSION} easy" >  ${PATH1}/${DIR}/usr/sbin/ec-chroot-${DISTRO_COMPAT_VERSION}
  chmod 755 ${PATH1}/${DIR}/usr/sbin/ec-chroot-${DISTRO_COMPAT_VERSION}
 else
  echo "#!/bin/sh
empty -f ec-chroot ${DB_nameonly}" >  ${PATH1}/${DIR}/usr/sbin/ec-chroot-${DB_nameonly}
  chmod 755 ${PATH1}/${DIR}/usr/sbin/ec-chroot-${DB_nameonly}
 fi
fi

###terminal###
if [ "$DB_install" == "terminal" ];then
 [ -e ${PATH1}/${DIR}/usr/bin/${DB_nameonly} ] && mv -f ${PATH1}/${DIR}/usr/bin/${DB_nameonly} ${PATH1}/${DIR}/usr/bin/${DB_nameonly}.bin
 #container launch script...
 echo "#!/bin/sh
empty -f ec-chroot ${DB_nameonly} sakura" >  ${PATH1}/${DIR}/usr/sbin/ec-chroot-${DB_nameonly}
 chmod 755 ${PATH1}/${DIR}/usr/sbin/ec-chroot-${DB_nameonly}
fi

###individual###
if [ "$DB_install" == "individual" ];then
 for aAPP in `echo "$DB_apps" | tr ',' ' '`
 do
  aEXEC="$(grep '^Exec=' ${PATH1}/${DIR}/usr/share/applications/${aAPP}.desktop | cut -f 2 -d '=')"
  #container launch script...
  if [ -e ${PATH1}/${DIR}/usr/bin/${aAPP} ];then
   echo "#!/bin/sh
empty -f ec-chroot ${aAPP}" >  ${PATH1}/${DIR}/usr/sbin/ec-chroot-${aAPP}
   chmod 755 ${PATH1}/${DIR}/usr/sbin/ec-chroot-${aAPP}
  else
   echo "#!/bin/sh
empty -f ec-chroot ${aAPP} ${aEXEC}" >  ${PATH1}/${DIR}/usr/sbin/ec-chroot-${aAPP}
   chmod 755 ${PATH1}/${DIR}/usr/sbin/ec-chroot-${aAPP}
  fi
 done
fi

###mksquashfs###
sync
cp -f ${PATH1}/${DIR}/usr/share/pixmaps/${DB_icons/,*/}48.png ${DB_nameonly}.png
cp -f ${PATH1}/${DIR}/.control/${DB_nameonly}.specs ${DIR}.specs
mksquashfs ${PATH1}/${DIR} ${DIR}.sfs
sync
DB_hash="$(sha256sum ${DIR}.sfs | cut -f 1 -d ' ')"
sed -i -e "s%^DB_hash=.*%DB_hash='${DB_hash}'%" ${DIR}.specs
echo "Created:
 ${DIR}.sfs
 ${DIR}.specs
 ${DB_nameonly}.png"

###end###
