#!/bin/sh
#(c) copyright 2007 Barry Kauler
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#launched from ~/.xinitrc, so X is running.
#note, see also /usr/sbin/savepuppyd, launched from /etc/rc.d/rc.local0.
#v3.91 BK oct,nov 2007, upated for Dingo.
#v3.94 BK 15 dec 2007, updated.
#v4.01 BK 19may2008: don't think loading usb-storage reqd here anymore.
#v4.01 BK 19may2008: note the new pup_eventd also launched from .xinitrc.
#w007 path fix for Xorg.
#w481 warning if modules cannot be loaded.
#100404 fix load applets for fbpanel. 100413 think needs some 'sleep 0.1'
#100504 background color for retrovol.
#100511 for ram-challenged pc, better to display welcome1stboot.png.
#100513 jwm tray now uses freememapplet_tray and blinky_tray. NOTE: future, move these to /root/Startup.
#100517 freememapplet_tray moved to /root/Startup.
#100522 'surfer' is a simple viewer (uses libgtkhtml).
#100608 if yaf-splash symlink to gtkdialog-splash, use '-fontsize large' parameter.
#100821 01micko: fixes for background color in tray apps, also launch first-run.sh.
#100917 have removed -font parameter from yaf-splash (see /usr/share/doc/yaf-splash-new.htm).
#101004 jemimah has disabled tray applet in retrovol, now using obmixer_tray.
#101007 sound alias modprobe info may have moved.
#101009 problem as tray applets only got launched after welcome msg exited.
#101017 Alsa modprobe file now /etc/modprobe.d/alsa.conf.
#101019 welcome1stboot is now a gtkdialog app...
#101112 alsa info in two different places.
#101120, 101121 fix sound test, rewritten /sbin/pup_event_backend_modprobe.
#101210 pup_event_alsa_cnt* are now in /tmp/pup_event_backend.
#110113 test /dev/mixer for sound ready, see /etc/init.d/10alsa.
#20110328 if mouse not working, popup dialog.
#110412 welcome1stboot now written in BaCon, in /usr/sbin.
#110516 only popup dlg for mouse not working, if DISTRO_XORG_AUTO='yes' (in /etc/DISTRO_SPECS)
#110807 take out pngoverlay code, doing it in woof 3builddistro.
#110925 fix, improvements, for flash player download.
#111010 create /tmp/delayedrun_finished_flag to flag when this script exited.
#111019 DISTRO_XORG_AUTO==yes and PUPMODE==5, run 'quickcountry'. mouse-checking moved to 'mousecheck'.
#111026 decided not to run 'mousecheck' from here. instead, a button in 'quicksetup'.
#111110 ping 64.233.169.103 address no longer responding. 01micko: use 8.8.8.8
#111112 delay a bit more before flashplayer popup.
#111113 fix for download url for flash player. 111117 fix.
#120110 added sudo line. see also /etc/sudoers -- set delayedrun does not require password, probably a security hole, need to look at it again later.
#120201 rodin.s: internationalized.
#120227 call external /usr/sbin/check_internet
#120329 shinobar: download tweaks.
#120401 added warning flash player may not work.
#120403 removed adobe flash download. no, have changed to older version.
#120620 removed adobe flash download. want quicksetup/welcome1stboot for any PUPMODE at first startup.
#120708 run 'quickarm' instead of 'quicksetup' on arm boards. see 3builddistro, /usr/sbin/quicksetup.
#120714 revert 120708.
#120718 modify initial sleep.
#120823 rerwin: skip quicksetup if /etc/personal_settings_popup_disabled file created by remasterpup2 (modified by rerwin).
#120823 rerwin: use distro-specific delayedrun if available.
#120830 quicksetup restarts X, welcome1stboot did not run.
#130709 precise pup now has two web browsers. 130711. 130719. 130723. 130725.

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #120110

export TEXTDOMAIN=delayedrun
export OUTPUT_CHARSET=UTF-8
. gettext.sh

#variables created at bootup by 'init' in initramfs...
. /etc/rc.d/PUPSTATE
. /etc/DISTRO_SPECS
[ -x ${0}-${DISTRO_FILE_PREFIX} ] && exec ${0}-${DISTRO_FILE_PREFIX} #120823 rerwin: use distro-specific delayedrun if available.
. /etc/rc.d/BOOTCONSTRAINED #120708

BGOPT=""
#100821 start...
WINDOWMANAGER="`cat /etc/windowmanager`"
case $WINDOWMANAGER in
 icewm*) #icewm or icewm-session.
  #micko for icewm, not sure why it's needed but it doesn't work without killing the ice tray. It's mainly so freememapplet_tray, network_tray and retrovol work.
  killall icewmtray
  icewmtray & 2>/dev/null 
  . $HOME/.icewm/theme
  THEMEDIR="/usr/share/icewm/themes"
  . ${THEMEDIR}/${Theme} 2>/dev/null
  NUMCOLOR=`echo $ColorNormalTaskBarApp|grep "rgb:"`
  if [[ $NUMCOLOR = "" ]];then BG=""
  else PRE_BG=`echo $ColorNormalTaskBarApp|cut -d ':' -f2|sed 's%/%%g'`
   BG="#${PRE_BG}"   #this is more generic and works with many themes.
  fi
  if [[ $Theme = "GoldenSand/default.theme" ]];then 
   BG="#C6AD73" #this is particular to 5.1, it can be removed for cleanliness
  fi
  if [ "$BG" = "" ];then 
   BGOPT=""
  else 
   BGOPT="-bg ${BG}"
  fi
 ;;
 *) 
  if [ -f /root/.jwm/jwm_colors ];then #100504
   . /root/.jwm/jwm_colors
    BGOPT="-bg ${MENU_BG}"
  fi
 ;;
esac
#10821 end.
export BGOPT #101013 for use in /root/Startup/retrovol_tray

KERNVER="`uname -r`" #w481

sleep 2 #1 #let the dust settle first. 120718 reduce 2 to 1. 121105 try 2, see also /sbin/pup_event_frontend_d.
#120718 slow cpus need more delay...
DELAYFACTOR=0
CPUMHZ=`grep -m 1 -i '^cpu MHz' /proc/cpuinfo | tr -d ' ' | cut -f 2 -d ':' | cut -f 1 -d '.'` #my laptop: 933.000
if [ $CPUMHZ ];then
 [ $CPUMHZ -gt 100 ] && DELAYFACTOR=`expr 900 \/ $CPUMHZ`
else #120718 raspi: 697.95  my laptop: 4789.47
 BOGOMIPS=`grep -m 1 -i '^bogomips' /proc/cpuinfo | tr -d ' ' | cut -f 2 -d ':' | cut -f 1 -d '.'`
 [ $BOGOMIPS ] && [ $BOGOMIPS -gt 200 ] && DELAYFACTOR=`expr 800 \/ $BOGOMIPS`
fi
[ $DELAYFACTOR -gt 0 ] && sleep $DELAYFACTOR

#this goes into the tray...
CURRPS="`ps`"
if [ "`which blinky_tray`" != "" ];then #100821
 blinky_tray ${BGOPT} & #created by Lior Tubi and MU.
 sleep 0.1
fi
#101004 jemimah has disabled tray applet in retrovol, now using obmixer_tray...
if [ ! -f /root/Startup/obmixer_tray ];then
 if [ ! -f /root/Startup/retrovol_tray ];then #101013
  if [ "`which retrovol`" != "" ];then
   if [ "`retrovol -help 2>&1 | grep '\-show'`" != "" ];then
    retrovol ${BGOPT} & #100119 tray volume control by pizzasgood. 100504
   else
    retrovol -hide ${BGOPT} & #100413 versions after 0.2. 100504
   fi
  fi
 fi
fi

#offer to popup a getting-started html page...
#120620 change logic so get these when first boot full install...
#the test for /etc/videomode is for when Xvesa has finished choosing a resolution...
if [ "`readlink /usr/bin/X`" = "Xorg" -o -e /etc/videomode ];then #w007
 if [ ! -f /var/local/delayedrun_firstboot_flag ];then
  if [ ! -f /tmp/delayedrun_firstboot_flag ];then #120830 if quicksetup restarts X, reenter here jumps this...
   touch /tmp/delayedrun_firstboot_flag
   if [ "$DISTRO_XORG_AUTO" = "yes" ];then #see /etc/DISTRO_SPECS 111019
    #quick setup window for country and language and X resolution...
    QUICKSETUP="quicksetup"
    [ "`readlink /usr/bin/X | grep 'Xorg'`" = "" ] && QUICKSETUP="quickcountry" #probably Xvesa, offer country settings only.
    [ ! -f /etc/personal_settings_popup_disabled ] && ${QUICKSETUP} #120823 rerwin: skip quicksetup if /etc/personal_settings_popup_disabled file created by remasterpup2 (modified by rerwin).
   fi
  fi
  
#  #130709 new precise pup... 130711...
#  if [ -e /usr/bin/opera ];then
#   #if [ -e /usr/lib/seamonkey ];then
#   if [ "`find /usr/lib -maxdepth 1 -type d -name 'seamonkey-1.1.*'`" != "" ];then
#    MAINBROWSER='Opera'
#    pupdialog --ok-label "$(gettext 'OK')" --backtitle "$(eval_gettext 'SeaMonkey and ${MAINBROWSER} web browsers')" --colors --background yellow --msgbox "$(eval_gettext 'This Puppy has two web browsers installed:') \ZbSeaMonkey\ZB and \Zb${MAINBROWSER}\ZB.\n\n$(eval_gettext 'The default is set to run ${MAINBROWSER}, however you can change that anytime via \ZbDefault Applications Chooser\ZB in the Setup menu.')\n\n$(gettext 'Both of these browsers are good for older slow and low-RAM PCs, try which one works best for you. Note, SeaMonkey is a very old version, but has the advantage of running better in old PCs compared with recent releases of SeaMonkey -- it also has a lot of addons included, such as add-blocker, flash-blocker, video-downloader, and user-agent-changer -- the first two are great if you have slow Internet access, the last enables SeaMonkey to pretend to be a different browser. The main downside of this old version of SeaMonkey is that it does not support HTML5.')\n\n$(gettext 'You are of course welcome to run the \ZbPuppy Package Manager\ZB and install a later SeaMonkey, Firefox or Chromium web browser.')" 0 0
#   fi
#  fi
  #130719... 130723... 130725...
  ALTBROWSER=''
  #the pinstall.sh script in qtweb and opera pets create separate desktop icon if there is another major browser...
  [ "`grep 'QtWeb' /root/Choices/ROX-Filer/PuppyPin`" != "" ] && ALTBROWSER='QtWeb'
  [ "`grep 'opera' /root/Choices/ROX-Filer/PuppyPin`" != "" ] && ALTBROWSER='opera'
  if [ "$ALTBROWSER" != "" ];then
   xALTBROWSER="`grep "$ALTBROWSER" /root/Choices/ROX-Filer/PuppyPin | grep -o 'label=.*' | cut -f 2 -d '"'`"
   FIRSTCHAR="`echo -n "$ALTBROWSER" | cut -c 1 | tr '[a-z]' '[A-Z]'`"
   RESTCHARS="`echo -n "$ALTBROWSER" | cut -c 2- | cut -f 1 -d '-'`"
   zALTBROWSER="${FIRSTCHAR}${RESTCHARS}" #ex: Opera
   MAINBROWSER="`grep '^exec' /usr/local/bin/defaultbrowser | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' '`"
   if [ "$MAINBROWSER" != "$ALTBROWSER" ];then
    case $MAINBROWSER in
     mozstart)  zMAINBROWSER='SeaMonkey' ;;
     seamonkey) zMAINBROWSER='SeaMonkey' ;;
     *)
      FIRSTCHAR="`echo -n "$MAINBROWSER" | cut -c 1 | tr '[a-z]' '[A-Z]'`"
      RESTCHARS="`echo -n "$MAINBROWSER" | cut -c 2- | cut -f 1 -d '-'`"
      zMAINBROWSER="${FIRSTCHAR}${RESTCHARS}"
     ;;
    esac
    pupdialog --ok-label "$(gettext 'OK')" --backtitle "$(gettext 'Web browser announcement')" --colors --background yellow --msgbox "$(gettext 'This build of Puppy has two web browsers:')
$(eval_gettext '\Zb${zMAINBROWSER}\ZB and \Zb${zALTBROWSER}\ZB')

$(eval_gettext 'The \Zbbrowse\ZB icon on the desktop will launch ${zMAINBROWSER}. There is a separate icon labeled \Zb${xALTBROWSER}\ZB for the ${zALTBROWSER} browser.')

$(eval_gettext '${zALTBROWSER} is a small and fast browser, that uses less system resources. It is recommended that you use this browser on very slow computers or with not much RAM. In fact, for any PC with less than 256MB, please choose ${zALTBROWSER}.')" 0 0
   fi
  fi

  if [ -c /dev/mixer ];then #110113 test for sound ready, see /etc/init.d/10alsa.
   aplay /usr/share/audio/2barks.au &
   #wavplay /usr/share/audio/2barks.wav &
   RETAPLAY=$?
   PIDAPLAY=$!
  fi
  welcome1stboot & #110412 now in /usr/sbin
  
  sleep 3 #101009
  #[ $RETAPLAY -eq 0 ] && kill $PIDAPLAY 2> /dev/null
  #[ "`pidof aplay`" != "" ] && killall aplay
  touch /var/local/delayedrun_firstboot_flag
 fi
fi

#v2.16 popup the BootManager if an extra SFS file added (once only)...
if [ ! $PUPMODE -eq 5 ];then
 if [ -d /initrd ];then #test it isn't full hd installation.
  . /etc/rc.d/BOOTCONFIG
  #if EXTRASFSLIST variable not there, then auto-load of SFS files (meaning, user
  # has probably never run the BootManager)...
  if [ "`grep '^EXTRASFSLIST' /etc/rc.d/BOOTCONFIG`" = "" ];then
   #see /initrd/init which writes these to BOOTCONFIG..
   if [ "$LASTUNIONRECORD" != "" ];then
    if [ "$LASTUNIONRECORD" != "$PREVUNIONRECORD" ];then
     bootmanager extrasfs quiet
    fi
   fi
  fi
 fi
fi

#101121 handle multiple sound cards...
if [ $PUPMODE -eq 5 ];then
 if [ -f /tmp/rc_d_rc_services_alsa_ok ];then #see /etc/rc.d/rc.services
  if [ `find /tmp/pup_event_backend -maxdepth 1 -type f -name 'pup_event_alsa_cnt*' | wc -l` -gt 1 ];then
   #run kirk's Multiple Sound Card Wizard...
   Multiple-Sound-Card-Wizard
  fi
 fi
fi

#v2.16 popup a warning if booting from usb flash...
if [ "$PMEDIA" = "usbflash" ];then
 yaf-splash -close never -fontsize large -bg yellow -timeout 3 -text "$(gettext 'WARNING! Do not unplug USB Flash drive!')" & #101009
fi

#v2.17 suggested by andrei...
if [ -d /root/Startup ];then
 for a in /root/Startup/*
 do
  [ -x "$a" ] && $a &
  sleep 0.2
 done
fi


touch /tmp/delayedrun_finished_flag #111010
###END###
