#!/bin/sh
#running from usb, first startup of sm is extremely slow. popup this info msg.
#131216 Billtoo reports this script does not self-destruct. try further measures.
#140106 this is incredibly weird, Billtoo still reports popup stays comes back.
#140129 this is ridiculous, others report same problem (wish they would find out why it is happening!!!). have put code into rc.shutdown
#150108 popup persists because sm running as 'spot'. removed 140106, 140129.
#150108 handle /usr/bin/seamonkey.bin, created when sm setup to run as spot. refer: support/setup-spot
#150119 fix $HOMEDIR display.
#161231 slackware 14.2 pkg, /usr/lib64/seamonkey
#190116 internationalize, simplify popup msg, incr delay.
#190723 abort running sm if no internet in container.
#200516 sm 2.53.2 broken with profile update. new profile, copy files from old profile.
#200528 200516 code moved to /etc/rc.d/rc.update

export TEXTDOMAIN=seamonkeynew
export OUTPUT_CHARSET=UTF-8

# -icon "/usr/lib/seamonkey/chrome/icons/default/default48.png"

#150108 normally this will be 'seamonkey', running as spot it will be 'seamonkey.bin':
# refer: /usr/bin/seamonkey (seamonkeyQNEW in woof), /usr/sbin/run-as-spot, /usr/sbin/setup-spot (support/setup-spot in woof)
#NAMEEXE="$(echo -n ${0} | rev | cut -f 1 -d '/' | rev)"
#if [ "$NAMEEXE" == "seamonkey.bin" ];then
#no, would rather be more precise about it:
NAMEOFUSER="$(id -n -u)"
if [ "$NAMEOFUSER" == "spot" ];then
 HOMEDIR="/root/spot/.mozilla/seamonkey"
 NAMEEXE='seamonkey.bin'
else #called as seamonkey or mozilla...
 HOMEDIR="/root/.mozilla/seamonkey"
 NAMEEXE='seamonkey'
fi

MAGICDIR="$(ls -1 ${HOMEDIR} | grep 'default$' | tail -n 1)"
if [ ! -f ${HOMEDIR}/${MAGICDIR}/places.sqlite ];then
 
 #190723
 /usr/local/easy_containers/internet-container-abort seamonkey
 RETi=$?
 [ $RETi -eq 2 ] && exit 2 #no internet in container.
 
 M_1="$(gettext 'SeaMonkey is starting for the very first time!')"
 M_2="$(gettext 'It will take awhile, as SeaMonkey creates setup and cache files in this folder:')"
 M_3="$(gettext 'All future startups of SeaMonkey will be much faster.')"
 M_4="$(gettext 'Please wait...')"
 SMSPLASH="yaf-splash -bg orange -fg black -close never -fontsize medium -text \"${M_1}

${M_2}
 ${HOMEDIR}.

${M_3}

${M_4}\" &"
 
 echo "#!/bin/sh
#this script created by /usr/bin/seamonkeyQNEW, ref 3buildeasydistro in woof.
HOMEDIR=${HOMEDIR}
${SMSPLASH}
 YAFPID=\$!
 CNT=0
 while [ ! -f ${HOMEDIR}/${MAGICDIR}/places.sqlite ];do
  sleep 2
  CNT=\`expr \$CNT + 2\`
  [ \$CNT -gt 40 ] && break
 done
 sleep 4 #2 190116
 pupkill \$YAFPID
 if [ ${RETi} -eq 1 ];then #have internet, running in a container.
  if [ ! -x /usr/bin/seamonkey-test ];then
   echo '#!/bin/sh
   /usr/local/easy_containers/internet-container-abort seamonkey
   if [ \$? -ne 2 ];then
    if [ -f /usr/lib/seamonkey/seamonkey ];then
     exec /usr/lib/seamonkey/seamonkey \"\$@\"
    else
     exec /usr/lib64/seamonkey/seamonkey \"\$@\"
    fi
   fi' > /usr/bin/seamonkey-test
   chmod 755 /usr/bin/seamonkey-test
  fi
  ln -snf /usr/bin/seamonkey-test /usr/bin/${NAMEEXE}
 else #not running in a container.
  if [ -f /usr/lib/seamonkey/seamonkey ];then
   ln -snf ../lib/seamonkey/seamonkey /usr/bin/${NAMEEXE}
  else
   ln -snf ../lib64/seamonkey/seamonkey /usr/bin/${NAMEEXE}
  fi
 fi
" > /tmp/sm-startup-wait-script
 chmod 755 /tmp/sm-startup-wait-script
 /tmp/sm-startup-wait-script & 
fi

#note, above script restores symlink, for all subsequent startups.
if [ -f /usr/lib/seamonkey/seamonkey ];then
 exec /usr/lib/seamonkey/seamonkey "$@"
else
 exec /usr/lib64/seamonkey/seamonkey "$@"
fi
