#!/bin/sh
#create desktop icon(s) and update jwm menu.
#script easy-containers does this, when new container created, but want to do it from elsewhere.
#called from /usr/local/sfsget/sfsget, passed-in params ${DB_path} ${FULLPKGNAME}
# ex: puppy/t2/racy racy_5.5_i686
#190911 if creating "easy" container, another series of easyos, change desktop label.
#200601 .desktop is normal, .ec-desktop for running in container. see also /sbin/fixlayers in intrd, dir2sfs, sfsget

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

#load the database variables...
. /mnt/wkg/sfs/${1}/${2}.specs
EC_CREATE="${DB_nameonly}"

#190911 
xDISTRO_COMPAT_VERSION="$(basename ${1})" #ex: easyos/oe/pyro, extract pyro
if [ "$EC_CREATE" == "easy" ];then
 xLABEL="$xDISTRO_COMPAT_VERSION"
else
 xLABEL="$EC_CREATE"
fi

#the SFS file is mounted...
DEBS_PATH="/mnt/wkg/sfs/${1}/mntpt"

#copy out the .desktop files...
for aAPP in `echo -n "$DB_apps" | tr ',' ' '`
do
 if [ -f /mnt/wkg/sfs/${1}/mntpt/usr/share/applications/${aAPP}.ec-desktop ];then #200601
  cp -f /mnt/wkg/sfs/${1}/mntpt/usr/share/applications/${aAPP}.ec-desktop /usr/share/applications/ec-${aAPP}.desktop
 else
  cp -f /mnt/wkg/sfs/${1}/mntpt/usr/share/applications/${aAPP}.desktop /usr/share/applications/ec-${aAPP}.desktop
 fi
 #and the run-script...
 cp -a -f /mnt/wkg/sfs/${1}/mntpt/usr/sbin/ec-chroot-${aAPP} /usr/sbin/
done

#copy out the icons...
for aICON in `echo -n "$DB_icons" | tr ',' ' '`
do
 cp -f /mnt/wkg/sfs/${1}/mntpt/usr/share/pixmaps/ec-${aICON}48.png /usr/share/pixmaps/
done

#find a free space on desktop...
#  note, there is free_coord() in /usr/local/easy_containers/frontend_funcs, but just do it roughly here...
for aY in 128 224 320
do
 for aX in 672 736
 do
  aPTN="x=\"${aX:0:2}[0-9]\" y=\"${aY:0:2}[0-9]\""
  grep "$aPTN" /root/Choices/ROX-Filer/PuppyPin > /dev/null
  [ $? -ne 0 ] && break 2
 done
done
#...aX and aY are free coordinates.

#rox-filer, icon on desktop...
#globicons, note, got code from /etc/rc.d/functions4puppy4...
echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <SetIcon>
   <Path>/usr/sbin/ec-chroot-${EC_CREATE}</Path>
   <Icon>/usr/share/pixmaps/ec-${EC_CREATE}48.png</Icon>
  </SetIcon>
 </env:Body>
</env:Envelope>"  | rox -R
#now create an icon on desktop...
#  note, frontend_funcs has add_pinboard_func(), use code from it... 190911...
echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <PinboardAdd>
   <Path>/usr/sbin/ec-chroot-${EC_CREATE}</Path>
   <X>${aX}</X>
   <Y>${aY}</Y>
   <Label>${xLABEL}</Label>
   <Args></Args>
  </PinboardAdd>
 </env:Body>
</env:Envelope>"  | rox -R
  
#log, read by ec-fix-desktop... 190911...
echo "EC_LABEL=${xLABEL}
EC_PATH=/usr/sbin/ec-chroot-${EC_CREATE}
EC_ICON=/usr/share/pixmaps/ec-${EC_CREATE}48.png" > /mnt/wkg/containers/${EC_CREATE}/desktop
  
#now update the jwm menu...
cd /
fixmenus
jwm -reload
pupdialog --timeout 5 --background '#80FF80' --backtitle "$(gettext 'Creation completed:') ${EC_CREATE}" --msgbox "$(gettext 'Container created, menu entry created, and icon on the desktop')" 0 0

sync
###end###
