#!/bin/sh
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Author          : http://jidanni.org/
# Created On      : 2009
# Last Modified On: Sat May 24 05:33:27 2014
# Update Count    : 213

#I say make sure sysv-rc-conf for nodm is all off, and for rc.local is 2 3 4 5

HOSTNAME=`hostname`
case $HOSTNAME in
    jidanni3)
	#so beep works
	#counteract /etc/modprobe.d/eeepc.conf
	#but 5 is default anyway
        #http://permalink.gmane.org/gmane.linux.debian.user/379183
	echo 0 >| /sys/module/snd_hda_intel/parameters/power_save
	beep -l 1 #for some reason the first beep after boot is not heard, so "prime" it here

	#http://permalink.gmane.org/gmane.linux.debian.devel.eeepc/3117
	#for stuck temperature sensor
	case $(cat /sys/devices/virtual/hwmon/hwmon0/temp1_input) in
	    55000|46000|0)
		case `date +%m` in
		    *)t=20;;
		    #REST NEVER MIND
#		    12|01|02|03)t=20;;#21
		    12|01|02|03)t=21;;
		    04|11)t=21;;#23
		    05|10)t=22;;#30
		    06|09)t=23;;#33
		    07|08)t=24;;#34
		    *)echo 1>&2 WHAT MONTH IS THAT?; exit 33;;
		esac
		#		t=`expr $t + 2` #Taipei/Taichung
		echo ${t}000 > /tmp/.bogus_jidanni_temperature
		chmod 666 /tmp/.bogus_jidanni_temperature
		/etc/init.d/fancontrol start
		#	        /sys/devices/platform/eeepc/hwmon/hwmon1/fan1_input
		;;
	esac
	bell000= #for below
	;;
    #    jidanni5)
    #	f=~/$HOSTNAME.brightness
    #	cat $f >| /sys/class/backlight/acpi_video0/brightness;;
esac

#    modprobe r8192s_usb
#    echo "0bda 8171" > /sys/bus/usb/drivers/rtl819xU/new_id
#    #ifconfig wlan0 up

case $HOSTNAME in
    jidanni2)
	# /proc/acpi/ibm/brightness
	# /usr/share/doc/linux-doc-*/Documentation/laptops/thinkpad-acpi.txt.gz
	#		      which rfkill > /dev/null && rfkill list wlan|grep 'Wireless LAN' > /dev/null &&
	if test -e /sys/class/net/wlan0 #ifconfig wlan0 > /dev/null 2>&1
	then
	    iwconfig wlan0 txpower off
	fi
	# 	j=/var/run/jidanni_thinkpad_brightness k=/home/jidanni/.brightness_bug_485465_rc.local
	# 	! test -f $j && test -f $k && ! pidof xdm > /dev/null &&
	# 	rmmod thinkpad-acpi && modprobe thinkpad-acpi brightness_mode=2 &&
	# 	touch $j # http://bugs.debian.org/485465
	# 	;;

	# if test `date +%Y` -lt 2000
	# then
	#     if test -d /cf/updates
	#     then
	# 	set -- `ls -td /cf/*`
	# 	set -- `date -ur $1`
	# 	date -s "$*"
	# 	echo $0: set date for now to:
	# 	date
	#     else echo /cf/updates not mounted. Not messing with time. 1>&2
	#     fi
	# fi
	# f=~/.brightness
	# if test -f $f
	# then cat $f > /sys/devices/virtual/backlight/thinkpad_screen/brightness
	# fi
esac

# echo 0 >| /sys/devices/virtual/backlight/thinkpad_screen/brightness
# find /sys/devices/virtual/backlight/thinkpad_screen/*bright*|xargs more

! test -d /cf/updates && fsck -C -a /cf && mount /cf
while ! test -d /cf/updates
do
    echo -n 'Connect /cf flash card, let settle, and hit [skip]RET '
    read answer
    case "$answer" in
	skip) break;;
	*)    fsck -C -a /cf && mount /cf;;
    esac
done

aa=/var/cache/apt/archives\ /var/lib/apt/lists
set -- $(find $aa -maxdepth 0 -type d -perm 0)
if test $# -ne 2
then
    ls -ld $aa
    echo -n "$0: not 000 !!!!! Got that holmes? "; read
fi

err=0
for m in $aa
do
    fsck -C -a $m && mount $m || err=1
done
if test $err != 0
then echo "MOUNT $aa YOURSELF, DUDE, OK? "; read
fi

f=/cf/updates/hhh.txt
if test -s $f
then cp -av /cf/updates/hhh.txt /var/tmp/hhh.txt.`date +%u`
fi
set +xe
# cal=/tmp/krazy.calendar
# perl -pwle 's/\\t/\t/g' > $cal <<EOF
# /* TueLast\\tA Pian Daren, Zhongguang 21:00 */
# SatFirst\\tLan Xuan Kexue zazhi 09:00
# SunFirst\\tLan Xuan Kexue zazhi 09:00
# EOF
# set -- $(calendar -f $cal -A 5)
# if test $# -gt 0; then echo $@|mail -s $cal jidanni; fi
# rm $cal

if test -d /cf/updates/
then
    cd /cf/updates || exit 44
    cpiome_notice="$(find cpiome.* -newer ${HOSTNAME?}.logoff -print 2>/dev/null|tee /tmp/CPIOME_NOTICE)"
    cd
    if test "$cpiome_notice"
    then
	cpiome_notice="**** some cpiomes to unwrap ****:
$cpiome_notice"
	echo "$cpiome_notice"
	(sleep 44; echo "$cpiome_notice"|wall)&
    fi
fi

#set -x
on_ac_power; a=$?;
case $a in 1)
# case $HOSTNAME in
#     jidanni6) ;;
#     *)
# 	BATT_FILE=/sys/class/power_supply/BAT?/status
# 	if ! test -f $BATT_FILE
# 	then
# 	    echo No \$BATT_FILE $BATT_FILE! ! ! ! ! !
# 	    echo hit RET
# 	    read
# 	elif grep -x Discharging $BATT_FILE > /dev/null
# 	then
	    echo -n -e BATTERY is Discharging ! Fix and hit RET' '
	    read
#	fi
	;;
esac
#set +x

echo "============ $0:
Will execute \"${program=/etc/init.d/nodm start}\" in ${seconds=5} seconds.
  Hit some chars and RET to cancel.
  Hit RET to start right away... MAYBE..."
# Hit ^C to cancel doesn't work here
tt=6 kbd=0 x=1
case $HOSTNAME in jidanni[23])
	if lsusb -v|perl -nwle 'print unless /Logitech, Inc. Unifying Receiver/../keyboard/i'|grep -i keyboard
	then
	    echo $0: found the above keyboard...
	    keyboard_risk=1
	    #	if test -f /cf/$HOSTNAME-66OK; then kbd=1; fi
	    tf=/cf/$HOSTNAME-66OK
	    if test -f $tf
	    then
		kbd=1
		echo found $tf, remove it if want rainy day \"$tt\" challenge...
		echo DON\'T TYPE ANYTHING or else you better have that spare keyboard ready...
		sleep 4
	    fi
	    if test ${kbd?} -eq 0
	    then
		echo -e ${bell000-\\a}Hit ...${tt}...RET if really want to use possibly
		echo       broken built-in keyboard, as today is a DRY WEATHER day.
	    fi
	else
	    echo $0: No USB keyboards found, not running choices section.
	    case $HOSTNAME in jidanni3)
		    echo HEY PLUG IN KEYBOARD! and well, reboot, fool
		    set -x
		    read
		    ;;
	    esac
	    keyboard_risk=0
	fi
	;;
    *) keyboard_risk=0;;
esac
for ((; seconds > 0; seconds--))
do
    read -p $seconds.. -t 1
    case $? in 0) #I hit RET...
	    case $REPLY in
		'') kbd=0 x=1  ;; #...with no other chars
		*${tt?}*) kbd=1 x=${keyboard_risk?};; #Yes, I really want to use the built-in keyboard.
		*) x=0  ;; #...with other chars
	    esac
	    break
	    ;;
    esac
done
case ${keyboard_risk?} in
    1)
	#	f6=/usr/share/X11/xorg.conf.d/66-jidanni.conf
	case ${kbd?} in
	    0) #WET DAYS
		touch /tmp/.DISABLE_WET_KEYBOARD;;
	esac
	;;
esac
case ${x?} in
    0)
	echo $0: NOT running ${program?} because \"x=0\"
	sleep 3
	exit 0
	;;
esac
${program?}
exit 0 #else will mess up concurrent makefile sysv stuff
# Local Variables:
# compile-command: "sh -n rc.local"
# End:
