#!/bin/sh
#called from /root/.xinitrc when X starts.
#checks desktop icons for containerized apps.
#note, the pre-created 'www' and 'console' desktop containers: png files are created in 3buildeasydistro.
#181006 if erase .session folder, may need to recreate start-scripts.

#180506 this does not exist at first bootup...
[ ! -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons ] && exit

#code from easy-containers...
find_free_coords_func() {
 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.
}

add_puppypin_func() {
 find_free_coords_func
 sed -i -e "s%^</pinboard>%  <icon x=\"${aX}\" y=\"${aY}\" label=\"${EC_LABEL}\">${EC_PATH}</icon>\n</pinboard>%" /root/Choices/ROX-Filer/PuppyPin
}

add_globicon_func() {
 sed -i '/^<\/special-files>/d' /root/.config/rox.sourceforge.net/ROX-Filer/globicons
 echo "  <rule match=\"${EC_PATH}\">
    <icon>${EC_ICON}</icon>
  </rule>
</special-files>" >> /root/.config/rox.sourceforge.net/ROX-Filer/globicons
}

for aDESK in `ls -1 /mnt/wkg/containers/*/desktop 2>/dev/null`
do
 [ ! "$aDESK" ] && continue
 . ${aDESK}
 [ ! "$EC_PATH" ] && continue
 grep "$EC_PATH" /root/Choices/ROX-Filer/PuppyPin >/dev/null
 [ $? -ne 0 ] && add_puppypin_func
 grep "$EC_PATH" /root/.config/rox.sourceforge.net/ROX-Filer/globicons >/dev/null
 [ $? -ne 0 ] && add_globicon_func
 #181006 if erase .session folder, this file will need to be recreated:
 if [ ! -f /usr/sbin/ec-chroot-${EC_LABEL} ];then
  echo "#!/bin/sh
exec ec-chroot ${EC_LABEL}" > /usr/sbin/ec-chroot-${EC_LABEL}
  chmod 755 /usr/sbin/ec-chroot-${EC_LABEL}
 fi
 if [ ! -f "$EC_ICON" ];then #ref: easy-containers
  cp -f /mnt/wkg/containers/${EC_LABEL}/ec-${EC_LABEL}48.png "${EC_ICON}"
 fi
done

###END###
