#!/bin/sh
#call from 3builddistro, current dir is inside 'sandbox3'.
#also want to call from LoginManager (/usr/sbin/loginmanager) from running puppy
# (3buildistro will copy this script to sandbox3/rootfs-complete/usr/sbin).
#pass in space-delimited names of apps to run as spot, each with false or true.
#ex: 'seamonkey=true' 'sylpheed=false'
#...=true means turn on spot, =false turn off. no parameter then do nothing.
#130601 first version, seamonkey only.
#130604 use code from /usr/sbin/run-as-spot, created by jamesbond (James Budiono).
#130604 passed-in params to have actual names of executables.
#130720 add support for QtWeb. 130721 fix. 130722 fix.
#130723 add opera option to run as spot. see also 3builddistro, loginmanager.
#140215 add chromium.
#150108 script launched by /usr/bin/seamonkey.bin cannot relink /usr/bin/seamonkey.bin, do it here.
#161231 slackware 14.2 seamonkey pkg is in /usr/lib64
#170921 fix seamonkey path when in /usr/lib.

SPOTAPPS="$@"
RUNNINGPUP=0
[ "`echo "$0" | grep 'support/'`" = "" ] && RUNNINGPUP=1 #running in woof.
PREFIXDIR=''
[ $RUNNINGPUP -eq 0 ] && PREFIXDIR='rootfs-complete'

touch ${PREFIXDIR}/root/.spot-status

#161231
if [ -f ../DISTRO_SPECS ];then
. ../DISTRO_SPECS #running in woofQ
else
. /etc/DISTRO_SPECS
fi
xARCHDIR=""
if [ "$DISTRO_ARCHDIR_SYMLINKS" = "no" ];then
 if [ "$DISTRO_ARCHDIR" == "lib64" ];then
  xARCHDIR="64"
 else
  xARCHDIR="/${DISTRO_ARCHDIR}"
 fi
fi

#130604 jamesbond, mods by BK...
RUNASSPOT='#script to run $APP as spot...
ARGS=""
[ $1 ] && while [ "$1" ]; do ARGS="$ARGS \"$1\""; shift; done
if [ $(id -u) -eq 0 ]; then
 [ $XAUTHORITY ] && cp $XAUTHORITY /root/spot/.Xauthority 2>/dev/null
 touch /root/spot/.Xauthority
 #following line is mostly there to catch any root:root files that may have got copied in...
 chown -R spot:spot /root/spot &
 export XAUTHORITY=/root/spot/.Xauthority  
 export XDG_CONFIG_HOME=/root/spot/.config
 export XDG_CACHE_HOME=/root/spot/.cache
 export XDG_DATA_HOME=/root/spot/.local/share
 exec su spot -s /bin/sh -c "\"$APP\" $ARGS"
else #precaution
 exec "$APP" "$ARGS"
fi'

seamonkey_true() {
 #function to run seamonkey as spot.
 [ ! -e ${ONEAPPpath}/${ONEAPPname}.bin ] && cp -a -f ${ONEAPPpath}/${ONEAPPname} ${ONEAPPpath}/${ONEAPPname}.bin
 rm -f ${ONEAPPpath}/${ONEAPPname} #in case it is a symlink.
 echo "#!/bin/sh
 APP='${ONEAPPpath}/${ONEAPPname}.bin'" > ${ONEAPPpath}/${ONEAPPname}
 
 #150108 script launched by /usr/bin/seamonkey.bin cannot relink /usr/bin/seamonkey.bin, do it here:
 # refer: woof rootfs-complete/usr/bin/seamonkeyQNEW
 #170921 fix path...
 if [ -f /usr/lib${xARCHDIR}/seamonkey/seamonkey ];then
  echo "[ -f /root/spot/.mozilla/seamonkey/a2bvafqx.default/places.sqlite ] && ln -snf /usr/lib${xARCHDIR}/seamonkey/seamonkey /usr/bin/seamonkey.bin" >> ${ONEAPPpath}/${ONEAPPname}
 else
  echo "[ -f /root/spot/.mozilla/seamonkey/a2bvafqx.default/places.sqlite ] && ln -snf /usr/lib/seamonkey/seamonkey /usr/bin/seamonkey.bin" >> ${ONEAPPpath}/${ONEAPPname}
 fi
 
 echo "$RUNASSPOT" >> ${ONEAPPpath}/${ONEAPPname}
 chmod 755 ${ONEAPPpath}/${ONEAPPname}

 if [ -e ${PREFIXDIR}/root/.mozilla ];then
  if [ ! -e ${PREFIXDIR}/root/spot/.mozilla/seamonkey/a2bvafqx.default ];then #check not already setup.
   if [ "$PREFIXDIR" ];then #woof. my SM PETs have skeleton .mozilla.
    cp -a ${PREFIXDIR}/root/.mozilla ${PREFIXDIR}/root/spot/
    DEFAULTDIR="`find ${PREFIXDIR}/root/spot/.mozilla/seamonkey -mindepth 1 -maxdepth 1 -name '*.default'`"
   else #running puppy...
    xDEFAULTDIR="`find /root/.mozilla/seamonkey -mindepth 1 -maxdepth 1 -name '*.default' | head -n 1`"
    if [ "$xDEFAULTDIR" ];then
     BASENAME="`basename $xDEFAULTDIR`" #ex: 3o1ptd5r.default
     mkdir -p /root/spot/.mozilla/seamonkey/${BASENAME}
     rm -rf /root/spot/.mozilla/seamonkey/${BASENAME}/* 2>/dev/null #precaution.
     echo "[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=${BASENAME}" > /root/spot/.mozilla/seamonkey/profiles.ini
     cp -a -f /root/.mozilla/seamonkey/${BASENAME}/chrome /root/spot/.mozilla/seamonkey/${BASENAME}/
     cp -a -f /root/.mozilla/seamonkey/${BASENAME}/localstore.rdf /root/spot/.mozilla/seamonkey/${BASENAME}/
     cp -a -f /root/.mozilla/seamonkey/${BASENAME}/mimeTypes.rdf /root/spot/.mozilla/seamonkey/${BASENAME}/
     cp -a -f /root/.mozilla/seamonkey/${BASENAME}/prefs.js /root/spot/.mozilla/seamonkey/${BASENAME}/
     DEFAULTDIR="/root/spot/.mozilla/seamonkey/${BASENAME}"
    fi
   fi
   if [ "$DEFAULTDIR" ];then #precaution.
    DIRNAME="`dirname $DEFAULTDIR`"
    #needs to have a different name for .default dir from main one...
    mv -f $DEFAULTDIR $DIRNAME/a2bvafqx.default
    sed -i -e 's%^Path=.*defaul.*%Path=a2bvafqx.default%' $DIRNAME/profiles.ini
    #setup Downloads dir... 130722 no need for this anymore, already exists...
    mkdir -p ${PREFIXDIR}/root/spot/Downloads
    sed -i -e 's%/root/my-documents%/root/spot/Downloads%' $DIRNAME/a2bvafqx.default/prefs.js
    sed -i -e 's%"/usr/share/doc"%"/root/spot/Downloads"%' $DIRNAME/a2bvafqx.default/prefs.js
   fi
  fi
 fi
 return 0
}

seamonkey_false() {
 #function to run seamonkey as root.
 if [ -e ${ONEAPPpath}/${ONEAPPname}.bin ];then
  cp -a -f --remove-destination ${ONEAPPpath}/${ONEAPPname}.bin ${ONEAPPpath}/${ONEAPPname} #restore original.
  rm -f ${ONEAPPpath}/${ONEAPPname}.bin
 else #precaution. error.
  ln -snf /usr/lib${xARCHDIR}/seamonkey/seamonkey ${PREFIXDIR}/usr/bin/seamonkey
 fi
 #a paranoid precaution...
 xDEFAULTDIR="`find ${PREFIXDIR}/root/.mozilla/seamonkey -mindepth 1 -maxdepth 1 -name '*.default' | head -n 1`"
 if [ "$xDEFAULTDIR" ];then
  BASENAME="`basename $xDEFAULTDIR`" #ex: 3o1ptd5r.default
  rm -rf ${PREFIXDIR}/root/spot/.mozilla/seamonkey/${BASENAME} 2>/dev/null
 fi
 return 0
}

generic_func() {
 #setup the app to run as spot or root...
 case $ONEAPPvalue in
  true)
   [ ! -e ${ONEAPPpath}/${ONEAPPname}.bin ] && cp -a -f ${ONEAPPpath}/${ONEAPPname} ${ONEAPPpath}/${ONEAPPname}.bin
   rm -f ${ONEAPPpath}/${ONEAPPname} #in case it is a symlink.
   echo "#!/bin/sh
   APP='${ONEAPPpath}/${ONEAPPname}.bin'" > ${ONEAPPpath}/${ONEAPPname}
   echo "$RUNASSPOT" >> ${ONEAPPpath}/${ONEAPPname}
   chmod 755 ${ONEAPPpath}/${ONEAPPname}
  ;;
  false)
   if [ -e ${ONEAPPpath}/${ONEAPPname}.bin ];then
    cp -a -f --remove-destination ${ONEAPPpath}/${ONEAPPname}.bin ${ONEAPPpath}/${ONEAPPname} #restore original.
    rm -f ${ONEAPPpath}/${ONEAPPname}.bin
   fi
  ;;
 esac
 return 0
}

#130720
QtWeb_true() {
 #function to run QtWeb as spot.
 [ ! -e ${ONEAPPpath}/${ONEAPPname}.bin ] && cp -a -f ${ONEAPPpath}/${ONEAPPname} ${ONEAPPpath}/${ONEAPPname}.bin
 rm -f ${ONEAPPpath}/${ONEAPPname} #in case it is a symlink.
 echo "#!/bin/sh
 APP='${ONEAPPpath}/${ONEAPPname}.bin'" > ${ONEAPPpath}/${ONEAPPname}
 #QtWeb saves config settings in /tmp when running as spot...
 #jamesbond: fixed: http://bkhome.org/blog2/?viewDetailed=00319
# echo 'rm -rf /tmp/QtWeb.NET' >> ${ONEAPPpath}/${ONEAPPname}
# echo 'cp -a -f /root/spot/QtWeb.NET /tmp/' >> ${ONEAPPpath}/${ONEAPPname}
 echo "$RUNASSPOT" >> ${ONEAPPpath}/${ONEAPPname}
# echo 'rm -rf /root/spot/QtWeb.NET' >> ${ONEAPPpath}/${ONEAPPname}
# echo 'cp -a -f /tmp/QtWeb.NET /root/spot/' >> ${ONEAPPpath}/${ONEAPPname}
# #take out "exec" as want to return to the script...
# sed -i -e 's% exec % %' ${ONEAPPpath}/${ONEAPPname}
 chmod 755 ${ONEAPPpath}/${ONEAPPname}
 
 if [ -d ${PREFIXDIR}/root/.config/QtWeb.NET ];then #this is in the PET or created when run QtWeb as root.
  if [ ! -d ${PREFIXDIR}/root/spot/.config/QtWeb.NET ];then
   mkdir -p ${PREFIXDIR}/root/spot/.config/QtWeb.NET
   cp -a -f ${PREFIXDIR}/root/.config/QtWeb.NET/"QtWeb Internet Browser.conf" ${PREFIXDIR}/root/spot/.config/QtWeb.NET/
   sed -i -e 's%/root/Downloads%/root/spot/Downloads%' ${PREFIXDIR}/root/spot/.config/QtWeb.NET/"QtWeb Internet Browser.conf"
  fi
 else
  mkdir -p ${PREFIXDIR}/root/spot/.config/QtWeb.NET
 fi
 chown -R spot:spot ${PREFIXDIR}/root/spot/.config
 chmod 775 ${PREFIXDIR}/root/spot/Downloads #should already be.
 return 0
}

#130720
QtWeb_false() {
 if [ -e ${ONEAPPpath}/${ONEAPPname}.bin ];then
  cp -a -f --remove-destination ${ONEAPPpath}/${ONEAPPname}.bin ${ONEAPPpath}/${ONEAPPname} #restore original.
  rm -f ${ONEAPPpath}/${ONEAPPname}.bin
 fi
 return 0
}

#130723 opera...
opera_true() {
 #function to run Opera as spot.
 [ ! -e ${ONEAPPpath}/${ONEAPPname}.bin ] && cp -a -f ${ONEAPPpath}/${ONEAPPname} ${ONEAPPpath}/${ONEAPPname}.bin
 rm -f ${ONEAPPpath}/${ONEAPPname} #in case it is a symlink.
 echo "#!/bin/sh
 APP='${ONEAPPpath}/${ONEAPPname}.bin'" > ${ONEAPPpath}/${ONEAPPname}
 echo "$RUNASSPOT" >> ${ONEAPPpath}/${ONEAPPname}
 chmod 755 ${ONEAPPpath}/${ONEAPPname}
 
  if [ ! -d ${PREFIXDIR}/root/spot/.opera ];then
   if [ -d ${PREFIXDIR}/root/.opera ];then #this is in the PET or created when run Opera as root.
    cp -a -f ${PREFIXDIR}/root/.opera ${PREFIXDIR}/root/spot/
   else
    mkdir -p ${PREFIXDIR}/root/spot/.opera
    echo '
[State]
Accept License=0

[User Prefs]
Home URL=file://localhost/usr/share/doc/home.htm
Startup Type=2
Download Directory={Home}Downloads

[Fonts]
Normal=14,4,0,0,0,0,DejaVu Sans
PRE=14,4,0,0,0,0,Nimbus Mono L

' > ${PREFIXDIR}/root/spot/.opera/operaprefs.ini
  fi
 fi
 chown -R spot:spot ${PREFIXDIR}/root/spot/.opera
 chmod 775 ${PREFIXDIR}/root/spot/Downloads #should already be.
 return 0
}
opera_false() {
 if [ -e ${ONEAPPpath}/${ONEAPPname}.bin ];then
  cp -a -f --remove-destination ${ONEAPPpath}/${ONEAPPname}.bin ${ONEAPPpath}/${ONEAPPname} #restore original.
  rm -f ${ONEAPPpath}/${ONEAPPname}.bin
 fi
 return 0
}

#140215
chromium_true() {
   [ ! -e ${ONEAPPpath}/${ONEAPPname}.bin ] && cp -a -f ${ONEAPPpath}/${ONEAPPname} ${ONEAPPpath}/${ONEAPPname}.bin
   rm -f ${ONEAPPpath}/${ONEAPPname} #in case it is a symlink.
   echo "#!/bin/sh
   APP='${ONEAPPpath}/${ONEAPPname}.bin'" > ${ONEAPPpath}/${ONEAPPname}
   echo "$RUNASSPOT" >> ${ONEAPPpath}/${ONEAPPname}
   chmod 755 ${ONEAPPpath}/${ONEAPPname}
 #change path to profile...
 mkdir -p /root/spot/chromium/profile
 chown -R spot:spot /root/spot/chromium
 ctPTN='s%/file/portable/chromium/profile%/root/spot/chromium/profile%'
 sed -i -e "$ctPTN" ${ONEAPPpath}/${ONEAPPname}.bin
 sed -i -e "$ctPTN" /usr/local/bin/defaultbrowser
 sed -i -e "$ctPTN" /usr/local/bin/defaulthtmlviewer
 return 0
}
chromium_false() {
   if [ -e ${ONEAPPpath}/${ONEAPPname}.bin ];then
    cp -a -f --remove-destination ${ONEAPPpath}/${ONEAPPname}.bin ${ONEAPPpath}/${ONEAPPname} #restore original.
    rm -f ${ONEAPPpath}/${ONEAPPname}.bin
   fi
 #change path to profile...
 ctPTN='s%/root/spot/chromium/profile%/file/portable/chromium/profile%'
 sed -i -e "$ctPTN" ${ONEAPPpath}/${ONEAPPname}
 sed -i -e "$ctPTN" /usr/local/bin/defaultbrowser
 sed -i -e "$ctPTN" /usr/local/bin/defaulthtmlviewer
 return 0
}

for ONEAPP in $SPOTAPPS
do
 ONEAPPname="`echo -n "$ONEAPP" | cut -f 1 -d '='`"  #ex: seamonkey
 ONEAPPvalue="`echo -n "$ONEAPP" | cut -f 2 -d '='`" #ex: true
 ONEAPPspec="`find ${PREFIXDIR}/bin ${PREFIXDIR}/sbin ${PREFIXDIR}/usr/bin ${PREFIXDIR}/usr/sbin ${PREFIXDIR}/usr/local/bin -mindepth 1 -maxdepth 1 -name ${ONEAPPname} | head -n 1`"
 if [ ! "$ONEAPPspec" ];then
  #executable does not exist, so remove from list, if there...
  sed -i -e "/^${ONEAPPname}=/d" ${PREFIXDIR}/root/.spot-status
  continue
 fi
 ONEAPPpath="$(dirname "$ONEAPPspec")"
 
 case $ONEAPPname in
  seamonkey) ${ONEAPPname}_${ONEAPPvalue} ;;
  QtWeb)     ${ONEAPPname}_${ONEAPPvalue} ;; #130720
  opera)     ${ONEAPPname}_${ONEAPPvalue} ;; #130723
  chromium)  ${ONEAPPname}_${ONEAPPvalue} ;; #140215
  *)         generic_func  ;;
 esac
 if [ $? -ne 0 ];then
  sed -i -e "/^${ONEAPPname}=/d" ${PREFIXDIR}/root/.spot-status #remove.
  continue
 fi

 #record choice, for future runs of Login&Security Manager...
 if [ "$(grep "^${ONEAPPname}=" ${PREFIXDIR}/root/.spot-status)" != "" ];then
  rsPTN="s%^${ONEAPPname}=.*%${ONEAPPname}=${ONEAPPvalue}%"
  sed -i -e "$rsPTN" ${PREFIXDIR}/root/.spot-status
 else
  echo "${ONEAPPname}=${ONEAPPvalue}" >> ${PREFIXDIR}/root/.spot-status
 fi
 
done

chown -R spot:spot ${PREFIXDIR}/root/spot

###END###
