#!/bin/ash
#(c) copyright Barry Kauler 2008, puppylinux.com
#2008 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v404 first release. rewritten from scratch, aiming for simplicity and speed.
#v405 fix for alsa.
#v406 load apm.ko if no acpi.
#v408 explicitly load battery, ac, thermal, fan modules.
#v409 gparted create ext3 part. failed, fixed by making /etc/mtab a symlink.
#v411 fix mouse detection logic.
#v411 can use old network wizard (from 403, based on 400)
#v411 return of 'puppyserialdetect', for usb/ps2 mouse detection only. now 'puppyinputdetect'.
#v411 extra 'sleep 1' as rerwin reports usb mouse may need it some hardware.
#v411 choosemousefunc().
#v412 /etc/DISTRO_SPECS, renamed pup_xxx.sfs, pup_save.2fs etc.
#v412 bugfix usb-storage wait loop.
#w001 ubuntu module-init-tools not configured to support gzipped modules.
#w004 LANG=C, faster. /bin/ash, even faster.
#w463 rerwin: special mwave modem handling.
#w463 mouse scrollwheel fix.
#w468 full-hd-install, write to PUPSTATE after remount r/w.
#w478 moved depmod code up above call to rc.update.
#w481 fix crappy depmod, 'out of memory' in first boot 64MB RAM (no swap).
#v423 k2.6.29.6 dmesg has warning that ehci-hcd should load before uhci-hcd and ohci-hid.
#v424 fix LAYERFS variable.
#v424 fix secondary uevent replay. now finds ssb devices.
#v431 secondary uevent replay, fix for old kernels < 2.6.24
#v432 make alsa-card-number generation multithread-safe; start modprobe protection/serialization daemon
#100126 move syslogd, klogd to /etc/init.d/00sys_logger
#100127 anticipate bug gunzip modules from initrd.
#100131 bugfix detect multiple cd/dvd drives.
#100214 one config of 2.6.33 has base floppy and ps/2 mouse support not builtin.
#100216 jemimah's Pwireless2 now supported. 100217 slight modification.
#100227 integrate /usr/local/bin/defaultconnect for setup tool choice.
#100228 rerwin: make alsa-card-number generation multithread-safe; start modprobe protection/serialization daemon
#100304 modify pwireless choice. 100306 added Simple Network Setup.
#100316 perform f.s. check if improper shutdown. 100319 unset TZ, always load /dev/shm.
#100513 test to prevent msg on stderr.
#100611 UDEV_LOG=2 to prevent non-critical o/p to screen at bootup and shutdown.
#100628 shinobar: launch rc.network if eth0 is usable.
#100703 load fbcon module: needed by some xorg drivers when exit to console (ex: nouveau).
#100814 record cumulative tx/rx, see also network_tray and rc.shutdown.
#100831 fix KERNMAJ for olpc people, have 'uname -r' returning version with '_'.
#100903 rerwin: acc tx/rx added pupdial support.
#100908 rerwin: -7.5-fix modem patch.
#100911 fsckme.err filename changed to fsckme.flg. see also initNEW, init (initramfs), rc.shutdown.
#101007 Network detection improvements from shinobar.
#101012 have restored 'depmod-FULL' name to 'depmod'.
#101107 rerwin: Reset alsa card numbers, but retain wizard card assignments in alsa.conf.
#101119 /sbin/pup_event_backend_modprobe has been rewritten, repercussions here.
#101202 hack, remove some firmware tarballs. ***TEMPORARY HACK***
#101210 new path /tmp/pup_event_backend for files logged by /sbin/pup_event_backend_*.
#101216 mkdir /tmp/simple_network_setup.
#110113 make sure /dev/mixer removed, see test in /etc/init.d/10alsa.
#110126 fix ATADRIVES variable for full-hd installation.
#110302 wasn't getting the right uevents for my 3g modem, use 'udevadm trigger'.
#110304 remove nodes in /dev/snd -- reports that these may be wrong after a reboot.
#110405 fix 'free' applet output format different in later versions of busybox.
#110426 move done/failed/backgrounded 2 places to right (also in init script).
#110430 fixups for fido (non-root user). 110503 removed.
#110502 fix creation of owner:group for device nodes. 110503 fix permission fsckme.flg.
#110511 2nd uevent replay: now using udevadm for usb, ssb and pcmcia.
#110513 rcrsn51: udevadm usb replay fix, properly detect scanners etc.
#110712 fixes for kernel with usb core (including usb-storage) drivers builtin.
#110722 get rid of stray modules.* files, may occur when mix usage of the two depmods.
#110814 01micko: ensure unique hostname.
#110814 kernel with inbuilt usb drivers, usbfs was not mounting on /proc/bus/usb.
#110823 mavrothal: Check if it is an OLPC XO and add the devices.
#120124 tracing problem of b43 firmware not loading, peebee posted udev trace, determined ssb uevent replay missing, found test for <2.6.24 kernel broken for 3.x.
#120216 slight mods to make sss translation easy (see /usr/share/sss/script_strings).
#120217 improvement for full hd install, non-english.
#120301 need to load console font earlier.
#120301 a problem if initrd has kernel default font, switching here changes all o/p from initrd to partial garbage.
#120313 fix 120301 font garbage. 120331 clear requires redirection to /dev/console.
#120409 no longer deleting /tmp/* in rc.shutdown... (note, init script in initrd.gz wipes it)
#120423 pemasu: /dev/mouse was missing in full install.
#120503 if kernel mounts a f.s. on /dev, removes my skeleton /dev
#120505 remove network reconnect flag if it exists.
#120704 introduce /etc/rc.d/BOOTCONSTRAINED. written by 3builddistro.
#120706 removed some sync's, think they are bad idea in this script.
#120709 BOOT_UDEVDCHILDREN from /etc/rc.d/BOOTCONSTRAINED
#120716 log size of ram, read by /sbin/initNEW for f.s. check.
#120717 log ext2,3,4 f.s. that have reached maximal mount count.
#130201 quieten the 'modinfo ehci_hcd'.
#130311 use modinfo to test if fbcon builtin.
#130318 support kmod (see also 3builddistro, rc.update).
#130501 support setting up X in PUPMODE=5. further fixup.
#130504 moved up, i think fbcon needs to be loaded before intel, nouveau modules load.
#130509 quicksetup creates pupdesk.flg with auto-selected driver stored in it. xorgwizard-cli reads this.
#130513 remove 130509, code now in init script in initrd.
#130513 init in initrd creates /root/.xorgwizard-reenter, need to ask kbd/locale even though auto-X at 1st boot...
#130618 3builddistro sets 'DEVTMPFSFLG' variable.
#130629 need directory /tmp/pup_event_ipc, to support new pup_event IPC.
#130630 bring back tmpfs on /tmp for full HD installation.

#unset TZ #100319 busybox hwclock gives priority to this (rather than /etc/localtime) and 'init' has set it wrong.
#...comment-out for now. note, TZ now set in rc.country.
ORIGLANG="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`" #120217
ORIGLANG1="${ORIGLANG%_*}" #ex: en
export LANG=C

. /etc/rc.d/functions4puppy4
. /etc/DISTRO_SPECS
. /etc/rc.d/BOOTCONSTRAINED #120704 has BOOT_DISABLESWAP, BOOT_ATIME, BOOT_DIRTYWRITE.

status_func() {
 if [ $1 -eq 0 ];then
  /bin/echo -n -e "\\033[74G\\033[1;32m" >/dev/console #green [done] msg. 110426: change 72 to 74.
  /bin/echo -n "done" >/dev/console #done
  /bin/echo -e "\\033[0;39m" >/dev/console
 else
  /bin/echo -n -e "\\033[72G\\033[1;31m" >/dev/console #red [failed]. 110426: change 70 to 72.
  /bin/echo -n "failed" >/dev/console #failed
  /bin/echo -e "\\033[0;39m" >/dev/console
  STATUS=0
 fi
 return $1 #return with same status as entry param.
}

loadswap_func() { #w481 made into a function.
 echo "LOAD SWAP"
 #load a swap partition...
 for ONESWAP in `fdisk -l | grep ' Linux swap' | cut -f 1 -d ' ' | tr '\n' ' '`
 do
  echo -n "Loading swap partition $ONESWAP..." >/dev/console #loading swap partition
  swapon $ONESWAP
  status_func $?
  [ $? -eq 0 ] && SWAPON="yes"
 done
 #if no go, try for a swap file...
 if [ "$SWAPON" != "yes" ];then
  SWAPFILE="/pupswap.swp"
  if [ -f /initrd${PUP_HOME}${SWAPFILE} ];then
   echo -n "Loading swap file ${SWAPFILE}..." >/dev/console #loading swap file
   swapon /initrd${PUP_HOME}${SWAPFILE}
   status_func $?
   [ $? -eq 0 ] && SWAPON="yes"
  fi
  if [ -f $SWAPFILE ];then
   echo -n "Loading swap file ${SWAPFILE}..." >/dev/console
   swapon $SWAPFILE
   status_func $?
   [ $? -eq 0 ] && SWAPON="yes"
  fi
 fi
}

#if have just done a switch_root, output a 'done' message...
[ -d /initrd ] && status_func 0 #note, /initrd does not exist when a full-hd installation.

. /etc/rc.d/MODULESCONFIG #modules loading configuration.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/X11R7/bin
[ $loglevel ] && LOGLEVEL=$loglevel #boot param.
[ $pmedia ] && PMEDIA=$pmedia #boot parameter, broad category of boot media. ex: cd.
[ $pdev1 ] && PDEV1=$pdev1    #boot parameter, partition have booted off. ex: hda3

#120301 a problem if initrd has kernel default font, switching here changes all o/p from initrd to partial garbage.
#workaround, clear the screen...
if [ -d /initrd ];then #120313 fix...
 PLANG1=`grep '^PLANG=' /etc/rc.d/PUPSTATE | cut -f 2 -d '=' | cut -f 1 -d '_'`
 [ ! "$PLANG1" ] && PLANG1='en'
 [ "$ORIGLANG1" != "$PLANG1" ] && clear > /dev/console #120331 redirection is required.
fi
load_consolefont #120301 if this was done in initrd then it would carry through, and this would not be needed.
#...this is done in rc.country, but needs to be move above all the o/p to /dev/console.

STATUS=0

##############MAKE FILESYSTEM USABLE################
#echo "MAKE FILESYSTEM USABLE"
echo -n "Making the filesystem usable..." >/dev/console #making filesystem usable. need this redirection!
busybox mount -t proc none /proc ;STATUS=$((STATUS+$?))

if [ ! -d /initrd ];then #w468
 if [ "$BOOT_ATIME" ];then #120704 see /etc/rc.d/BOOTCONSTRAINED, variable set in 3builddistro.
  busybox mount -o remount,rw,${BOOT_ATIME} / #have set this to 'relatime'.
 else
  busybox mount -o remount,rw /
 fi
 #120409 no longer deleting /tmp/* in rc.shutdown... (note, init script in initrd.gz wipes it)
 rm -rf /tmp/*
 rm -rf /tmp/.[0-9a-zA-Z]*
 echo 'PUPMODE=2' > /etc/rc.d/PUPSTATE
 if [ "$ORIGLANG1" != "en" ];then #120217
  echo "OUTPUT_CHARSET=UTF-8
export OUTPUT_CHARSET" >> /etc/rc.d/PUPSTATE
 fi
fi
. /etc/rc.d/PUPSTATE #variables created at bootup by init script in initrd.

if [ "$BOOT_DIRTYWRITE" ];then #120704 see /etc/rc.d/BOOTCONSTRAINED, variable set in 3builddistro.
 #i have set this as 1500 which is 15 seconds (default is 5 seconds).
 echo $BOOT_DIRTYWRITE > /proc/sys/vm/dirty_writeback_centisecs #refer: http://www.lesswatts.org/tips/disks.php
fi

#v409 mount/umount scripts no longer write to /etc/mtab, as gparted failed to create a
#ext3 partition -- dunno why. Instead, now have /etc/mtab a symlink to /proc/mounts...
rm -f /etc/mtab
ln -s /proc/mounts /etc/mtab

#redirect all output to a log file (must do after remount rw)...
[ ! "$LOGLEVEL" ] && exec 1>/tmp/bootsysinit.log 2>&1

mkdir -p /dev/pts #120503 if kernel mounts a f.s. on /dev, removes my skeleton /dev
busybox mount /dev/pts ;STATUS=$((STATUS+$?))
mkdir /sys 2>/dev/null
busybox mount -t sysfs none /sys ;STATUS=$((STATUS+$?))

##v2.20 some apps need shm (shared memory) (ex: xfdiff)... 100319 do this always...
FREERAM=`free | grep -o 'Mem: .*' | tr -s ' ' | cut -f 4 -d ' '` #w481 110405
QTRFREERAM=`expr $FREERAM \/ 4`
mkdir -p /dev/shm #120503 if kernel mounts a f.s. on /dev, removes my skeleton /dev
mount -t tmpfs -o size=${QTRFREERAM}k shmfs /dev/shm ;STATUS=$((STATUS+$?))

if [ ! -d /initrd ];then
 #130630 restore tmpfs on /tmp...
 #120717 this is not so good on raspi with only 256MB RAM, but saves flash writes and faster...
 mount -t tmpfs -o size=${QTRFREERAM}k tmpfs /tmp ;STATUS=$((STATUS+$?))
 #120716 /sbin/init needs to know ramdisk size, before /sys mounted...
 [ ! -f /var/local/ram_size_bytes ] && [ -e /sys/block/ram0/size ] && cat /sys/block/ram0/size > /var/local/ram_size_bytes
 #120717 log maximal mount counts, potentially rc.shutdown can then not delete /fsckme.flg...
 #dmesg | grep -o 'EXT[2,3,4]-fs.*running e2fsck is recommended' > /tmp/dmesg_e2fsck_warnings1
 dmesg | grep -o 'EXT[2,3,4]-fs.*maximal mount count reached' > /tmp/dmesg_e2fsck_warnings1
 #example lines:
 #EXT3-fs (sda9): warning: mounting fs with errors, running e2fsck is recommended
 #EXT3-fs (sda10): warning: maximal mount count reached, running e2fsck is recommended
fi

#w478 moved this code above call to rc.update...
KERNVER="`uname -r`"
#KERNMAJ=`echo -n "$KERNVER" | cut -f 3 -d '.' | cut -f 1 -d '-' | cut -f 1 -d '_'` #v431 100831. 120124 remove.
#w469 may need to run 'depmod'...
#live-cd may have been built without the modules.* files.
#initrd init script copied modules to /lib/modules/<kernver>/initrd/.
if [ ! -f /lib/modules/${KERNVER}/modules.dep ];then
 NEEDDEPMOD="yes" #w469 files may not be there to save space.
 [ -d /lib/modules/${KERNVER}/initrd ] && NEEDGUNZIP="yes"
else
 if [ -d /lib/modules/${KERNVER}/initrd ];then
  #100127 remove leading '/' from '/initrd/' for comaptibility with depmod-FULL...
  if [ "`cat /lib/modules/${KERNVER}/modules.dep | cut -f 1 -d ':' | grep  'initrd/'`" = "" ];then
   NEEDDEPMOD="yes" #w469 files may not be there to save space.
   NEEDGUNZIP="yes"
  fi
 fi
fi
if [ "$NEEDGUNZIP" = "yes" ];then
 #w001 ubuntu module-init-tools not configured to support gzipped modules...
 #for ONEMOD in `find /lib/modules/${KERNVER}/initrd/* -type f -name *.gz | tr '\n' ' '`
 #do
 # gunzip $ONEMOD
 #done
 gunzip -f -r /lib/modules/${KERNVER}/initrd #w482 shinobar.
fi

#w481 now building with busybox depmod, in module-init-tools have depmod-FULL, so comment out...
##w481 workaround for crappy depmod. First boot 64MB ram no swap, only 14660 free ram, due to
##tmpfs top layer (about 26MB). depmod gives 'out of memory'. so delay until after swap loaded...
#if [ "$NEEDDEPMOD" = "yes" ];then
# #have enabled depmod in latest busybox, that may be more efficient...
# if [ "`busybox | grep 'depmod'`" != "" ];then
#  echo -n ' depmod' >/dev/console
#  busybox depmod
# else
#  if [ $FREERAM -gt 25000 ];then
#   echo -n ' depmod' >/dev/console
#   depmod -a
#  else
#   #try and load a swapfile early...
#   echo >/dev/console
#   loadswap_func
#   if [ "$SWAPON" = "yes" ];then
#    echo -n 'Running depmod...' >/dev/console
#    depmod -a
#   fi
#  fi
# fi
#fi
#101012 have restored depmod-FULL name to 'depmod'...
if [ "$NEEDDEPMOD" = "yes" ];then
 echo -n ' depmod' >/dev/console
 if which kmod;then #130418
  #do not use busybox depmod, ever.
  depmod #should be linked to kmod.
 else
  #110722 get rid of stray modules.* files, may occur when mix usage of the two depmods...
  mkdir -p /tmp/rc_sysinit_dump
  mv -f /lib/modules/${KERNVER}/modules.* /tmp/rc_sysinit_dump
  [ -f /tmp/rc_sysinit_dump/modules.builtin ] && mv -f /tmp/rc_sysinit_dump/modules.builtin /lib/modules/${KERNVER}/
  [ -f /tmp/rc_sysinit_dump/modules.order ] && mv -f /tmp/rc_sysinit_dump/modules.order /lib/modules/${KERNVER}/
  rm -f /tmp/rc_sysinit_dump/modules.*
  if [ "`busybox | grep 'depmod'`" != "" ];then
   busybox depmod #use busybox depmod if available.
  else
   depmod
  fi
 fi
fi


[ $layerfs ] && LAYERFS=$layerfs #boot param.
[ ! $LAYERFS ] && LAYERFS=aufs #aufs or unionfs
[ "`modinfo aufs 2>/dev/null`" = "" ] && LAYERFS=unionfs #precaution.

#110430 fixups for fido. 110503 removed (fixed in rc.shutdown)
#HOMEUSER="`grep '^tty1' /etc/inittab | tr -s ' ' | cut -f 3 -d ' '`" #root or fido.
#if [ "$HOMEUSER" != "root" ];then
# #/usr/sbin/root2user has already done this, but somehow gets changed back to root...
# busybox chown ${HOMEUSER}:users /root #contents already changed.
# busybox chmod g+s /root
#fi

status_func $STATUS

#######################VERSION UPDATE##########################
echo "VERSION UPDATE"
#force update when new version of ${DISTRO_NAME}...
echo -n "Updating..." >/dev/console #updating

/etc/rc.d/rc.update $PUPMODE
#sync
status_func $STATUS

#110814 01micko: http://www.murga-linux.com/puppy/viewtopic.php?p=550932#550932
if [ "`cat /etc/hostname`" = "puppypc" ];then
 echo -n "Updating unique hostname..." >/dev/console #hostname
 echo "puppypc${RANDOM}" > /etc/hostname
 PUPHOSTNAME="`cat /etc/hostname`"
 HOSTSFILEFIRST="`grep -w 'puppypc' /etc/hosts|sed 's% puppypc%%'`"
 HOSTSFILEREST="`grep -v 'puppypc' /etc/hosts`"
 echo "$HOSTSFILEFIRST $PUPHOSTNAME" > /etc/hosts
 echo "$HOSTSFILEREST" >> /etc/hosts
 status_func 0
fi

#Ensure kernel-specific firmware.dep file present.
[ ! -e /etc/modules/firmware.dep.$KERNVER ] \
 && mv -f /etc/modules/firmware.dep /etc/modules/firmware.dep.$KERNVER

#101202 hack, remove some firmware tarballs, no longer used...
#dgcmodem: i have removed the /etc/init.d/Dgcmodem script from firmware tarball (so don't delete it).
#refer: /usr/sbin/pupdial_init_hotpluggable.
for ONEFIRM in cdcacm hso ipwireless nozomi usbserial
do
 if [ -f /lib/modules/all-firmware/${ONEFIRM}.tar.gz ];then
  rm -f /lib/modules/all-firmware/${ONEFIRM}.tar.gz
  ofPATTERN='/^'"${ONEFIRM}"':/d'
  sed -i -e "$ofPATTERN" /etc/modules/firmware.dep.$KERNVER
 fi
done

#################LOAD KERNEL MODULES################
echo "LOAD KERNEL MODULES"
echo -n "Loading kernel modules..." >/dev/console

MODALIASES="`ls /sys/bus/*/devices/*/modalias`"

#101119 new /sbin/pup_event_backend_modprobe, these must be deleted every boot...
rm -f /etc/modprobe.d/alsa_card*.conf 2>/dev/null
touch /etc/modules/firmware.dep.inst.${KERNVER} #make sure exists.
rm -f /dev/ttyUSB* 2>/dev/null #101210 may have been left there if modem plugged in at shutdown.
rm -f /dev/mixer 2>/dev/null #110113 make sure removed, see test in /etc/init.d/10alsa.
rm -f /dev/snd/* #110304 after a reboot, some of these may be wrong.
mkdir /tmp/rc_sysinit #101210 for logging into.
mkdir /tmp/pup_event_backend #101210 for logging into, see /sbin/pup_event_backend_modprobe.
mkdir /tmp/simple_network_setup #101216
mkdir -p /tmp/pup_event_ipc #130629 for new pup_event IPC.

#101119 no longer using /tmp/pup_event_modprobe.conf, put blacklist into /etc/modprobe.d/...
rm -f /etc/modprobe.d/blacklist*.conf
BLACKLISTVARS="`echo "$SKIPLIST" | tr '\-' '_' | tr ' ' '\n' | sed -e 's/^/blacklist /' | grep -v ' $'`"
echo "$BLACKLISTVARS" > /etc/modprobe.d/blacklist.conf

if [ ! -d /initrd ];then #v423 full hd install.
 #100214 one config of 2.6.33 has base floppy and ps/2 mouse support not builtin. have this in 'init' also...
 [ "`modinfo floppy 2>/dev/null`" != "" ] && modprobe floppy > /dev/null 2>&1
 [ "`modinfo psmouse 2>/dev/null`" != "" ] && modprobe psmouse
 #v423 have this in 'init', probably need here too...
 if [ "`echo "$MODALIASES" | grep 'bc0Csc03i10'`" != "" ];then
  modprobe yenta-socket #101119
  [ $? -eq 0 ] && sleep 2
 fi
 #v423 k2.6.29.6 dmesg has warning that ehci-hcd should load before uhci-hcd and ohci-hid
 # this is contrary to my understanding all these years, but oh well, let's do it...
 ELSPCI="`elspci -l`" #jesses great little utility.
 [ "`echo "$ELSPCI" | grep '0C0320'`" != "" ] && modprobe ehci-hcd > /dev/null 2>&1 #110712 maybe builtin to kernel.
 #...perhaps a udev rule could have done this?
fi

#130504 moved up, i think fbcon needs to be loaded before intel, nouveau modules load...
#101119 i really only want to load this for a kms-enabled video...
KMS=no
[ "`cat /proc/cmdline | grep 'modeset=1'`" != "" ] && KMS=yes
[ "`cat /etc/modprobe.d/* | grep '^options .* modeset=1'`" != "" ] && KMS=yes
[ "$KMS" = "yes" ] && [ "`modinfo fbcon 2>/dev/null`" != "" ] && modprobe fbcon #100703 needed by some xorg drivers when exit to console (ex: nouveau). 110712 maybe builtin. 130311 maybe builtin.

#130618 devtmpfs-enabled kernel, initrd may have loaded it on /dev, if DEVTMPFSFLG<>0.
DEVTMPFSFLG=0 #see 3builddistro.
if [ $DEVTMPFSFLG -ne 0 ];then
 #device nodes created by kernel in initrd are all 'root' group. fix (before starting udevd)...
 chmod 660 /dev/* 2>/dev/null
 chmod 666 /dev/null
 chmod 666 /dev/zero
 chgrp tty /dev/[pt]ty*
 chgrp  /dev/console
 chgrp video /dev/fb0
 chgrp floppy /dev/fd[0-9]
 chgrp disk /dev/[hs]d[a-z]*
 chgrp disk /dev/fuse
 chgrp disk /dev/mmcblk*
 chgrp cdrom /dev/sr[0-9]
fi

#my intention is for puppy to work with either of these...
if [ -f /sbin/udevd ];then
 #100611 UDEV_LOG=2 to prevent non-critical o/p to screen at bootup and shutdown...
 UDEVVER=`udevd --version`
 if [ $UDEVVER -gt 150 ];then
  #100611 v151 is recommended for kernel 2.6.27+.
  #110502 change 'never' to 'early', fixes device nodes created with correct owner:group...
  if [ "$BOOT_UDEVDCHILDREN" ];then #120709
   UDEV_LOG=2 /sbin/udevd --daemon --resolve-names=early --children-max=${BOOT_UDEVDCHILDREN} #BOOT_UDEVDCHILDREN=1 good idea?
  else
   UDEV_LOG=2 /sbin/udevd --daemon --resolve-names=early
  fi
 else
  UDEV_LOG=2 /sbin/udevd --daemon
 fi
else
 #note, this script is no longer in Puppy.
 pup_event_backend_d >/tmp/pup_event_backend_errors 2>&1 & #hotplug daemon. my homebrew replacement for udevd.
fi
sleep 0.1

#replay uevents from /sys...
for ONEMODALIAS in $MODALIASES
do
 ONEPATH="`dirname $ONEMODALIAS`"
 if [ -e ${ONEPATH}/uevent ];then
  echo add > ${ONEPATH}/uevent #generates an 'add' uevent.
  sleep 0.02
 fi
done
##wait until uevent queue is empty...
#if [ -f /sbin/udevd ];then
# /sbin/udevadm settle --timeout=30
#else
# while [ "`grep 'twosectimeout' /tmp/pup_event_backend_s`" = "" ];do #wait until uevents processed.
#  sleep 0.5
# done
#fi

if [ ! -d /initrd ];then #full hd installation.
 modprobe nls_cp437 > /dev/null 2>&1 #these needed by vfat/ntfs/ext2 f.s.'s. 110712 maybe builtin.
 modprobe nls_iso8859-1 > /dev/null 2>&1 # "
 modprobe fuse
 modprobe $LAYERFS #unionfs or aufs.
 modprobe squashfs
 #we can determine ide/sata drives at this point (drivers builtin to kernel)...
 #110126 define ATADRIVES as all internal ide/pata/sata drives (not usb), except optical...
 #110712 rewritten to handle kernel with usb driver built-in...
 ALLDRVS0="`find /sys/block -maxdepth 1 -name 'mmc*' -o -name 'sd*' -o -name 'sr*' | xargs -l readlink 2>/dev/null | grep -v '/usb[0-9]' | rev | cut -f 1 -d '/' | rev`" #all *except* usb!
 ALLDRVS="`echo "$ALLDRVS0" | tr '\n' ' '`" #all *except* usb!
 [ "$ALLDRVS" = " " ] && ALLDRVS=""
 ATADRIVES="`echo "$ALLDRVS0" | grep -v '^sr' | tr '\n' ' '`"
 [ "$ATADRIVES" = " " ] && ATADRIVES=""
 ATAOPTICALDRIVES="`echo "$ALLDRVS0" | grep '^sr' | tr '\n' ' '`"
 [ "$ATAOPTICALDRIVES" = " " ] && ATAOPTICALDRIVES=""
 if [ -e /proc/ide ];then #110126
  for ONEIDE in `ls -1 /proc/ide | grep '^hd' | tr '\n' ' '`
  do
   if [ "`cat /proc/ide/${ONEIDE}/media`" = "cdrom" ];then
    ATAOPTICALDRIVES="${ATAOPTICALDRIVES}${ONEIDE} "
   else
    ATADRIVES="${ATADRIVES}${ONEIDE} "
   fi
  done
 fi
 ATADRIVES0="`echo -n "$ATADRIVES" | tr ' ' '\n'`"
 ATAOPTICALDRIVES0="`echo -n "$ATAOPTICALDRIVES" | tr ' ' '\n'`"
 echo '#ATADRIVES is all internal ide/pata/sata drives, excluding optical, excluding usb...' >> /etc/rc.d/PUPSTATE
 echo "ATADRIVES='${ATADRIVES}'" >> /etc/rc.d/PUPSTATE
 echo '#ATAOPTICALDRIVES is list of non-usb optical drives...'  >> /etc/rc.d/PUPSTATE #110206
 echo "ATAOPTICALDRIVES='$ATAOPTICALDRIVES'"  >> /etc/rc.d/PUPSTATE
fi

[ ! -d /proc/acpi ] && modprobe apm #v406

#110823 mavrothal: Check if it is an OLPC XO and add the devices...
ISITANXO=`cat /sys/class/dmi/id/product_name`
if [ "$ISITANXO" = "XO" ] && [ $UDEVVER -gt 150 ] ; then
   udevadm trigger  --action=add --subsystem-match="input" --subsystem-match="sound"
   udevadm settle
fi

status_func 0

######################LOAD SWAP#####################
[ "$BOOT_DISABLESWAP" ] && SWAPON="$BOOT_DISABLESWAP" #120704 now ask in 3builddistro if want use swap file/partition. anything not "yes" means no.
[ "$SWAPON" != "yes" ] && loadswap_func

#################MISC. SYSTEM SETUP#################
echo "MISC. SYSTEM SETUP"
#100126 moved to /etc/init.d/00sys_logger...
#syslogd -m 0
#klogd

echo -e "${DISTRO_NAME} Linux\n`uname -s` `uname -r` [`uname -m` arch]\n\n" > /etc/issue
echo "1" > /proc/sys/net/ipv4/ip_dynaddr
hostname -F /etc/hostname

rm -f /mnt/home 2>/dev/null
if [ ! -d /initrd ];then
 PUP_HOME='/'
 echo "PUP_HOME='/'" >> /etc/rc.d/PUPSTATE
 ln -s / /mnt/home
else
 if [ "$PUP_HOME" ];then #see /etc/rc.d/PUPSTATE
  if [ "$PUP_HOME" = "/pup_ro1" -o "$PUP_HOME" = "/pup_rw" ];then
   #note, PUPMODE=6 will have PUP_HOME=/pup_rw.
   #in the case of the persistent storage being the partition itself, this will be mounted
   #on /initrd/pup_ro1 (tmpfs on pup_rw for restricted writes) or directly on /initrd/pup_rw
   #and we do not really want users to access it as it is a unionfs layer. Instead, they are
   #already accessing it as "/".
   ln -s / /mnt/home
  else
   ln -s /initrd${PUP_HOME} /mnt/home
  fi
 fi
fi

################WAIT MODULES LOADED##################
echo "WAIT MODULES LOADED"
#previous module loading may not have completed...
echo -n "Waiting for modules to complete loading..." >/dev/console
WAITCNT=0
PCIPCMCIA="`elspci -l | grep -o '060700'`"
if [ "$PCIPCMCIA" != "" ];then #this may be slow to respond.
 echo -n " pcmcia"  >/dev/console
 while [ $WAITCNT -lt 10 ];do
  [ "`lsmod | grep -E '^yenta_socket |^tcic |^i82092 |^i82365 |^pd6729 '`" != "" ] && break
  WAITCNT=`expr $WAITCNT + 1`
  sleep 1
  echo -n " $WAITCNT" >/dev/console
 done
fi
PCIUSB="`elspci -l | grep -o -E '0C0300|0C0310|0C0320'`"
#note, if initrd then usb-storage will have already loaded...
USBBUILTIN='no'
if [ "`modinfo ehci_hcd 2>/dev/null`" = "" ];then #110712 130201 shut this up.
 #kernel has usb core drivers (and usb-storage) builtin.
 USBBUILTIN='yes'
else
 if [ "$PCIUSB" != "" ];then #this may be slow to respond.
  echo -n " usb"  >/dev/console
  PCIUSBNUM=`echo "$PCIUSB" | sort -u | wc -l`
  while [ $WAITCNT -lt 10 ];do
   [ `lsmod | grep -o -E '^uhci_hcd|^ohci_hcd|^ehci_hcd' | wc -l` -ge $PCIUSBNUM ] && break
   WAITCNT=`expr $WAITCNT + 1`
   sleep 1
   echo -n " $WAITCNT" >/dev/console
  done
 fi
fi

#replay uevents from /sys (more interfaces as a consequence of above drivers loaded)...
#note, this also a workaround for kernel 'bug' where entries appear in /sys but no uevent
#gets generated (yes it happens in 2.6.25.x kernel!)

#[ -e /sys/bus/pcmcia/devices ] && NEWPCMCIA="`ls /sys/bus/pcmcia/devices/*/modalias | tr '\n' ' '`"
#[ -e /sys/bus/usb/devices ] && NEWUSB="`ls /sys/bus/usb/devices/*/modalias | tr '\n' ' '`"
#for ONEMODALIAS in ${NEWPCMCIA}${NEWUSB}
#do
# ONEPATH="`dirname $ONEMODALIAS`"
# if [ -e ${ONEPATH}/uevent ];then
#  echo add > ${ONEPATH}/uevent #generates an 'add' uevent.
#  sleep 0.02
# fi
#done
##if [ -f /sbin/udevd ];then
## /sbin/udevadm settle --timeout=30
##else
## while [ "`grep 'twosectimeout' /tmp/pup_event_backend_s`" = "" ];do #wait until uevents processed.
##  sleep 0.5
## done
##fi

#if [ $KERNMAJ -lt 24 ];then #v431
if vercmp $KERNVER lt '2.6.24';then #120124
 [ -e /sys/bus/pcmcia/devices ] && NEWPCMCIA="`ls /sys/bus/pcmcia/devices/*/modalias | tr '\n' ' '`"
 [ -e /sys/bus/usb/devices ] && NEWUSB="`ls /sys/bus/usb/devices/*/modalias | tr '\n' ' '`"
 for ONEMODALIAS in ${NEWPCMCIA}${NEWUSB}
 do
  ONEPATH="`dirname $ONEMODALIAS`"
  if [ -e ${ONEPATH}/uevent ];then
   echo add > ${ONEPATH}/uevent #generates an 'add' uevent.
   sleep 0.02
  fi
 done
else
 #110511 now using udevadm for usb, ssb and pcmcia... 110513 keep ssb as before...
 #v424 try it a different way, this also fixes ssb devices (such as b43.ko)...
# [ -e /sys/bus/pcmcia/devices ] && NEWPCMCIA="`ls /sys/bus/pcmcia/devices/*/uevent 2>/dev/null | tr '\n' ' '`"
# #110302 usb done differently below...
# #[ -e /sys/bus/usb/devices ] && NEWUSB="`ls /sys/bus/usb/devices/*/uevent 2>/dev/null | tr '\n' ' '`"
 [ -e /sys/bus/ssb/devices ] && NEWSSB="`ls /sys/bus/ssb/devices/*/uevent 2>/dev/null | tr '\n' ' '`"
 for ONEUEVENT in ${NEWSSB} #${NEWPCMCIA}${NEWUSB}${NEWSSB} #110511
 do
  ONEPATH="`dirname $ONEUEVENT`"
  #the check for 'modalias' file is maybe needed for old kernel?...
  [ ! -e ${ONEPATH}/modalias ] && [ "`grep 'MODALIAS' $ONEUEVENT`" = "" ] && continue
  echo add > $ONEUEVENT
  sleep 0.02
 done
# #110302 wasn't getting the right uevents for my 3g modem...
# udevadm trigger --action=add --subsystem-match=usb
# #TODO i think need this:

# udevadm trigger --action=add --subsystem-match="pcmcia*" --subsystem-match=usb --subsystem-match=ssb
 udevadm trigger --action=add --subsystem-match="pcmcia*" --subsystem-match="usb*" #--subsystem-match=ssb #110513

 #or, NEWPCMCIA="`ls /sys/class/pcmcia_socket/*/uevent 2>/dev/null | tr '\n' ' '`"
fi

if [ "$USBBUILTIN" = "no" ];then #110712
 #wait for usb-storage drives to become available...(already done if initrd)
 #note, pup_event_backend_d and udevd both log to this file when load modules
 # (the latter via udev handler script /sbin/pup_event_backend_modprobe)...
 #101119 /tmp/pup_event_backend/pup_event_module_devpath_log* now multiple files (see /sbin/pup_event_backend_modprobe)
 if [ "`cat /tmp/pup_event_backend/pup_event_module_devpath_log* | grep 'usb_storage'`" != "" ];then #see pup_event_backend_modprobe
  echo -n " usb-storage"  >/dev/console
  while [ $WAITCNT -lt 15 ];do
   [ "`dmesg | grep 'usb-storage: device scan complete'`" != "" ] && break
   sleep 1
   WAITCNT=`expr $WAITCNT + 1`
   echo -n " $WAITCNT" >/dev/console
  done
  #precaution, make sure have got them all... v412 need sort -u...
  USBSTORAGES=`/bin/dmesg | grep "usb-storage: device found at" | sort -u | wc -l`
  while [ $WAITCNT -lt 15 ];do
   AVAILABLEUSBSTORAGES=`/bin/dmesg | grep "usb-storage: device scan complete" | wc -l`
   [ $AVAILABLEUSBSTORAGES -ge $USBSTORAGES ] && break
   sleep 1
   WAITCNT=`expr $WAITCNT + 1`
   echo -n " $WAITCNT" >/dev/console
  done
 fi

 sleep 1 #v411 a bit extra. rerwin reports usb mouse detection problem some hardware.
         #(delay before usb mouse info appears in /proc/bus/input/devices)
fi

status_func 0

##############USER SELECTED MODULES##################
echo "USER SELECTED MODULES"
#the user can select extra modules to load in the BootManager...
if [ "$ADDLIST" != "" ];then #variable is in /etc/rc.d/MODULESCONFIG
 echo -n "Loading user-selected modules..." >/dev/console
 for MODULE in $ADDLIST
 do
  MPARAMS=""
  if [ `echo -n "$MODULE" | tr ':' ' ' | wc -w` -gt 1 ];then
   MPARAMS="`echo -n "$MODULE" | cut -f 2-9 -d ':' | tr ':' ' '`"
   MODULE="`echo -n "$MODULE" | cut -f 1 -d ':'`"
  fi
  echo -n " $MODULE" >/dev/console
  echo "Loading module $MODULE $MPARAMS"
  firmware_tarball_func #install firmware tarball. see functions4puppy4.
  #for mwave.ko (modem), do not load module, just the firmware, which has script that correctly loads mwave.ko.
  [ "$MODULE" != "mwave" ] && modprobe $MODULE $MPARAMS
 done
 status_func 0
fi


###################SETUP SERVICES################
echo "SETUP SERVICES"
echo -n "Setting up services (network, printing, etc.)..." >/dev/console
if [ -h /dev/modem ];then
 DEVM="`readlink /dev/modem`"
 case $DEVM in
  modem) #error, circular link.
   rm -f /dev/modem
   DEVM=""
  ;;
  /dev/*) #wrong format.
   DEVM="`echo -n "$DEVM" | cut -f 3,4 -d '/'`"
   ln -snf $DEVM /dev/modem
  ;;
 esac
 case $DEVM in
  ttyS[0-9]) #apparently setserial can crash with other modems.
   setserial -v -b /dev/modem auto_irq skip_test autoconfig
  ;;
 esac
fi

#had hoped to retire this, but HardInfo, Pupscan need it... 110814 fix test...
[ -e /proc/bus/usb ] && busybox mount -t usbfs none /proc/bus/usb

#v408 only needed for 2.6.21.7, as 2.6.25.x loads these automatically...
#v408 kirk: Check for laptop battery...
modprobe battery
if [ -d /proc/acpi/battery ]; then
 results="`find /proc/acpi/battery -mindepth 1 -type d`"
 if [ ! -z "$results" ]; then
  modprobe ac
  modprobe thermal
  modprobe fan
 else
  rmmod battery
 fi
fi

#100814 100903 record cumulative tx/rx, see also network_tray and rc.shutdown...
UPDATE_MONTH="`date +%b`"
CURRENT_MONTH="`cat /var/local/sns/current_month`" 
if [ "$UPDATE_MONTH" != "$CURRENT_MONTH" ];then 
 echo "$UPDATE_MONTH" > /var/local/sns/current_month 
 for ONECOUNT in sns/r sns/t pupdial/isp1/r pupdial/isp1/t pupdial/isp2/r pupdial/isp2/t;do 
  echo -n 0 > /var/local/${ONECOUNT}x_bytes_month 
 done 
fi 

#120505 remove this so that /usr/bin/xwin does not try another auto network connect (ref: /usr/sbin/hostname-set)...
[ -f /tmp/simple_network_setup/network_default_reconnect_required_flag ] && rm -f /tmp/simple_network_setup/network_default_reconnect_required_flag

#120505 ***NOTE: this code block extracted to /usr/sbin/network_default_connect. in future could delete it here and call that script.***
#100227 choose default network tool...
DEFAULTCONNECT="`cat /usr/local/bin/defaultconnect | tail -n 1 | tr -s " " | cut -f 2 -d " "`"
[ "`grep 'gprs' /usr/local/bin/defaultconnect`" != "" ] && DEFAULTCONNECT='pgprs-connect'
[ "$DEFAULTCONNECT" = "gkdial" ] && DEFAULTCONNECT="pupdial" #for older pups.
NETCHOICE="$DEFAULTCONNECT"	#101007 shinobar
case $DEFAULTCONNECT in
 Pwireless2)
  NETCHOICE='Pwireless2'
 ;;
 net-setup.sh)
  NETCHOICE='net-setup.sh'
 ;;
 net_wiz_classic)
  NETCHOICE='net_wiz_classic'
 ;;
 sns)
  NETCHOICE='sns'
 ;;
 connectwizard) #try determine which tool was used to setup networking... 101007
  if [ -s /etc/simple_network_setup/connections ];then #100306
   NETCHOICE='sns'
  else
   CHECKOLDWIZ="`ls -1 /etc/*[0-9]mode 2>/dev/null`" #ex: eth0mode, wlan0mode.
   if [ "$CHECKOLDWIZ" != "" -a -d /usr/local/net_setup ];then
    NETCHOICE='net_wiz_classic'
   else
    CHECKNEWWIZ="`ls -1 /etc/network-wizard/network/interfaces 2>/dev/null`"
    if [ "$CHECKNEWWIZ" != "" ];then
     NETCHOICE='net-setup.sh'
    else
     [ -f /usr/local/Pwireless2/interface ] && NETCHOICE='Pwireless2' #100304
    fi
   fi
  fi
 ;;
esac
[ -f /etc/init.d/Pwireless2 ] && chmod 644 /etc/init.d/Pwireless2 #prevent jemimah's script from running. 100304 100513
case $NETCHOICE in
 Pwireless2)
  #this only sets up interface 'lo'...
  /etc/rc.d/rc.network_basic
  #jemimah's script is in /etc/init.d/Pwireless2
  chmod 755 /etc/init.d/Pwireless2 #make executable so it will run.
  #i want to run it right now, as a separate process (rc.services will ignore it)...
  /etc/init.d/Pwireless2 start &
 ;;
 net-setup.sh)
  /etc/rc.d/rc.network &
 ;;
 net_wiz_classic)
  #note, old wizard is located in /usr/local/net_setup.
  /usr/local/net_setup/etc/rc.d/rc.network &
 ;;
 sns) #100306
  /etc/rc.d/rc.network_basic #this only sets up interface 'lo'.
  /usr/local/simple_network_setup/rc.network &
 ;;
 connectwizard) #101007 shinobar
  #100628 shinobar: launch rc.network if eth0 is usable
  RCNETWORK=/etc/rc.d/rc.network_basic #this only sets up interface 'lo'.
  # eth0 usable?
  if /sbin/ifconfig eth0 &>/dev/null ;then
    if [ -x /etc/rc.d/rc.network ]; then
      RCNETWORK=/etc/rc.d/rc.network
    elif [ -e /etc/init.d/Pwireless2 ]; then
      chmod 755 /etc/init.d/Pwireless2
    fi
  fi
  $RCNETWORK &
 ;;
 *) #101007 shinobar
   /etc/rc.d/rc.network_basic &
 ;;
esac

/etc/rc.d/rc.services & #run scripts in /etc/rc.d/init.d

echo -e "\\033[64G\\033[1;33m[backgrounded]\\033[0;39m" >/dev/console #column 62, yellow. 110426: 64

############RECOGNISE MEDIA DEVICES################
echo "RECOGNISE MEDIA DEVICES"
STATUS=0
echo -n "Recognising media devices..." >/dev/console
#recognise optical drives...
echo -n ' optical' >/dev/console
OPTCNT=1;CDTYPE="";DVDTYPE="";CDBURNERTYPE=""
OPTICALS="`grep '^drive name:' /proc/sys/dev/cdrom/info | grep -o -E 'sr.*|hd.*' | tr '\t' ' '`"
[ -L /dev/cdrom ] && CDTYPE="`readlink /dev/cdrom | cut -f 3 -d '/'`"
[ -L /dev/dvd ] && DVDTYPE="`readlink /dev/dvd | cut -f 3 -d '/'`"
[ -f /etc/cdburnerdevice ] && CDBURNERTYPE="`cat /etc/cdburnerdevice`"
[ "`echo "$OPTICALS" | grep "$CDTYPE"`" = "" ] && CDTYPE="" #no longer exists.
[ "`echo "$OPTICALS" | grep "$DVDTYPE"`" = "" ] && DVDTYPE="" #no longer exists.
[ "`echo "$OPTICALS" | grep "$CDBURNERTYPE"`" = "" ] && CDBURNERTYPE="" #no longer exists.
for ONEOPTICAL in $OPTICALS
do
 ONENUM="`echo -n "$ONEOPTICAL" | cut -c 3`"
 [ "$CDTYPE" = "" ] && CDTYPE="$ONEOPTICAL"
 [ "$DVDTYPE" = "" ] && [ "`grep '^Can read DVD' /proc/sys/dev/cdrom/info | head -n 1 | grep -o '[01].*' | sed -e 's/[^01]//g' | cut -c $OPTCNT`" = "1" ] && DVDTYPE="$ONEOPTICAL" #100131
 [ "$CDBURNERTYPE" = "" ] && [ "`grep '^Can write CD' /proc/sys/dev/cdrom/info | head -n 1 | grep -o '[01].*' | sed -e 's/[^01]//g' | cut -c $OPTCNT`" = "1" ] && CDBURNERTYPE="$ONEOPTICAL" #100131
 OPTCNT=`expr $OPTCNT + 1`
done
rm -f /dev/cdrom; rm -f /dev/dvd; rm -f /etc/cdburnerdevice
[ "$CDTYPE" ] && ln -sf /dev/$CDTYPE /dev/cdrom
[ "$DVDTYPE" ] && ln -sf /dev/$DVDTYPE /dev/dvd
[ "$CDBURNERTYPE" ] && echo -n "$CDBURNERTYPE" > /etc/cdburnerdevice
[ "$DVDTYPE" ] && hdparm -d1 /dev/$DVDTYPE >/dev/null 2>&1

echo -n ' input' >/dev/console #100131
if [ ! -s /etc/mousedevice ];then #120423 change -f to -s test.
 echo -n "input/mice" > /etc/mousedevice
 ln -snf input/mice /dev/mouse
else
 [ "`cat /etc/mousedevice`" = "psaux" ] && echo -n "input/mice" > /etc/mousedevice
fi
#120423 pemasu: /dev/mouse was missing in full install...
MOUSELINK="`cat /etc/mousedevice`" #(refer /usr/sbin/input-wizard for examples)
if [ ! -e /dev/$MOUSELINK ];then #precaution, in case mouse interface is gone (unlikely, being paranoid).
 echo -n "input/mice" > /etc/mousedevice
 ln -snf input/mice /dev/mouse
fi
[ ! -e /dev/mouse ] && ln -s $MOUSELINK /dev/mouse

#recognise keyboard...
#echo -n ' keyboard' >/dev/console
#130513 init in initrd creates /root/.xorgwizard-reenter, need to ask kbd/locale even though auto-X at 1st boot...
ASKME=''
[ $PUPMODE -eq 5 ] && [ -f /root/.xorgwizard-reenter ] && ASKME='ask'
/etc/rc.d/rc.country ${ASKME} ;STATUS=$((STATUS+$?)) #this asks for keyboard layout.

status_func $STATUS

############MISC DESKTOP STUFF##################
echo "MISC. DESKTOP STUFF"
if [ "$PUPMODE" = "77" ];then #77=multisession cd/dvd.
 if [ "`cat /root/Choices/ROX-Filer/PuppyPin | grep "savesession"`" = "" ];then
  echo '<icon x="768" y="128" label="save">/usr/sbin/savesession-dvd</icon>' >> /root/Choices/ROX-Filer/PuppyPin
  grep -v '/pinboard' /root/Choices/ROX-Filer/PuppyPin  > /tmp/PuppyPin-CPY
  #sync
  cp -f /tmp/PuppyPin-CPY /root/Choices/ROX-Filer/PuppyPin
  echo '</pinboard>' >> /root/Choices/ROX-Filer/PuppyPin
 fi
fi

#save button on desktop when booted from flash drive...
if [ $PUPMODE -eq 3 -o $PUPMODE -eq 7 -o $PUPMODE -eq 13 ];then #pup_rw is tmpfs.
 if [ "`cat /root/Choices/ROX-Filer/PuppyPin | grep "save2flash"`" = "" ];then
  echo '<icon x="768" y="128" label="save">/usr/sbin/save2flash</icon>' >> /root/Choices/ROX-Filer/PuppyPin
  grep -v '/pinboard' /root/Choices/ROX-Filer/PuppyPin  > /tmp/PuppyPin-CPY
  #sync
  cp -f /tmp/PuppyPin-CPY /root/Choices/ROX-Filer/PuppyPin
  echo '</pinboard>' >> /root/Choices/ROX-Filer/PuppyPin
 fi
fi


###################PERSONAL BOOT SCRIPT######################
echo "PERSONAL BOOT SCRIPT"
#personal boot script here...
if [ ! -f /etc/rc.d/rc.local ];then
 echo '#this file called from rc.sysinit' > /etc/rc.d/rc.local
 echo '#you can edit this file' >> /etc/rc.d/rc.local
 echo '#When firewall is installed, will append lines to this file...' >> /etc/rc.d/rc.local
fi
. /etc/rc.d/rc.local

#100316 improper shutdown check. see above, also rc.shutdown and /sbin/init...
if [ ! -d /initrd ];then #if no /initrd, full hd install.
 [ ! $PDEV1 ] && PDEV1="`df | grep ' /$' | grep '^/dev/' | cut -f 1 -d ' ' | cut -f 3 -d '/'`"
 [ ! $DEV1FS ] && DEV1FS="`mount | grep ' on / ' | grep '^/dev/' | cut -f 5 -d ' '`"
 if [ "$PDEV1" -a "$DEV1FS" ];then
  [ "`grep '^PDEV1' /etc/rc.d/PUPSTATE`" = "" ] && echo "PDEV1='$PDEV1'" >> /etc/rc.d/PUPSTATE
  [ "`grep '^DEV1FS' /etc/rc.d/PUPSTATE`" = "" ] && echo "DEV1FS='$DEV1FS'" >> /etc/rc.d/PUPSTATE
  #this file gets removed by rc.shutdown if a proper shutdown...
  echo -n "${PDEV1},${DEV1FS}," > /fsckme.flg #ex: sda7,ext3,
  busybox chmod 666 /fsckme.flg #110503 so non-root can delete it.
 fi
else
 if [ "$PUPSAVE" ];then
  echo -n "$PUPSAVE" > /initrd${PUP_HOME}/fsckme.flg #ex: sda7,ext3,/pupsave-10MAR2010.2fs
  busybox chmod 666 /initrd${PUP_HOME}/fsckme.flg #110503 so non-root can delete it.
 fi
fi

if [ "$BOOT_SCHEDULER" ];then #120704 see /etc/rc.d/BOOTCONSTRAINED, variable set in 3builddistro.
 #my kernel defaults to 'cfq', change to 'deadline'. refer: http://tombuntu.com/index.php/2008/09/04/four-tweaks-for-using-linux-with-solid-state-drives/
 if [ "$PDEV1" ];then
  PDEV="`echo -n "$PDEV1" | sed -e 's%[0-9]*$%%' -e 's%p$%%'`" #mmcblk0p2 becomes mmcblk0
  if [ "`cat /sys/block/${PDEV}/queue/scheduler | grep "$BOOT_SCHEDULER"`" != "" ];then #check available.
   if [ "`cat /sys/block/${PDEV}/queue/scheduler | cut -f 2 -d '[' | cut -f 1 -d ']' | grep "$BOOT_SCHEDULER"`" = "" ];then #check not already chosen.
    #sync
    echo $BOOT_SCHEDULER > /sys/block/${PDEV}/queue/scheduler
    echo 1 > /sys/block/${PDEV}/queue/iosched/fifo_batch
   fi
  fi
 fi
fi

#130513 remove...
##130501 pupdesk.flg gets created at bootup if not exist. If exists, then X desktop failed, so run Xorg Wizard...
##note, /usr/sbin/quicksetup deletes pupdesk.flg when click ok button.
#if [ $PUPMODE -eq 5 ];then
# if [ "$DISTRO_XORG_AUTO" = "yes" ];then #see /etc/DISTRO_SPECS
#  if [ -f /initrd/tmp/PCPARTSALL_ata -o -f /initrd/tmp/PCPARTSALL_usb ];then
#   for ONEPART in `grep -E 'vfat|ntfs|ext[2-4]|f2fs|reiserfs' /initrd/tmp/PCPARTSALL_* | tr '\n' ' '`
#   do
#    #ex: ONEPART=/dev/sda3|ntfs|37209088
#    pFLG=0
#    ADEV="`echo -n "$ONEPART" | cut -f 1 -d '|'`"
#    AFS="`echo -n "$ONEPART" | cut -f 2 -d '|'`"
#    pMNTPT="$(cat /proc/mounts | grep "${ADEV} " | cut -f 2 -d ' ')"
#    if [ "$pMNTPT" = "" ];then
#     ABASE="`basename $ADEV`"
#     mkdir -p /mnt/$ABASE
#     mount -t $AFS $ADEV /mnt/$ABASE
#     pFLG=1
#     pMNTPT="/mnt/${ABASE}"
#     sleep 1 #just in case /proc/mounts slow to update.
#    fi
#    RWFLAG="$(cat /proc/mounts | grep "${ADEV} " | grep ' rw,')"
#    if [ "$RWFLAG" ];then
#     if [ ! -f ${pMNTPT}/pupdesk.flg ];then
#      touch ${pMNTPT}/pupdesk.flg #130509 note, quicksetup will insert auto-driver into this.
#     else
#      #130509 quicksetup creates pupdesk.flg with auto-selected driver stored in it. xorgwizard-cli reads this.
#      cp -f ${pMNTPT}/pupdesk.flg /var/local/quicksetup-pupmode5-autodriver
#      touch /root/.xorgwizard-reenter #see /root/.profile, will cause xorgwizard to run.
#      /etc/rc.d/rc.country ask #text-mode dlg for keyboard and locale.
#     fi
#     sync
#    fi
#    [ $pFLG -eq 1 ] && umount $pMNTPT
#    [ "$RWFLAG" ] && break #should break on the first loop.
#   done
#  fi
# fi
#fi

#that's it. next stop is /etc/profile...
###END###
