#!/bin/true
# Final one-time adjustment required by different platforms.
# Sourced from rc.sysinit
# Note: this file is sourced, not run. Don't use 'exit', use 'return'.
#
# Copyright (C) James Budiono 2014, 2015, 2016, 2018, 2019
# License: GNU GPL Version 3 or later.
#

### configuration

### various platform adjustment helpers

# enable gpe suppression, wifi, webcam, mbpfan, sound and keyboard
mbp_setup() {
	# enable mbp-specific service
	chmod 755 /etc/init.d/99-disable-spurious-gpe /etc/init.d/15-mbpfan
	#... and run them early
	/etc/init.d/99-disable-spurious-gpe start
	# check if wl driver is needed
	[ "$(ifconfig | grep wlan)" = "" ] && chmod 755 /etc/init.d/30-BC-wl \
		&& /etc/init.d/30-BC-wl start

	# add kb-backlight control if it is detected
	lsmod | grep -q applesmc &&
>> /root/.sven/key_config cat << "EOF"
[237:0]
type=cmd
enable=1
description=keyboard backlight down
command=/usr/bin/mbp-kb-backlight.sh down
comment=

[238:0]
type=cmd
enable=1
description=keyboard backlight up
command=/usr/bin/mbp-kb-backlight.sh up
comment=
EOF

	# add Xorg volume control and arrow keys mapping
>> /etc/X11/app-defaults/Xmodmap cat << "EOF"
keycode 105 = KP_Left KP_4 KP_Left KP_4
keycode 106 = KP_Right KP_6 KP_Right KP_6
keycode 113 = XF86AudioMute NoSymbol XF86AudioMute
keycode 114 = XF86AudioLowerVolume NoSymbol XF86AudioLowerVolume
keycode 115 = XF86AudioRaiseVolume NoSymbol XF86AudioRaiseVolume
EOF

	# set analog sound card as default
>> /etc/modprobe.d/alsa-mbp.conf cat << "EOF"
options snd-hda-intel id=PCH index=0
options snd-hda-intel id=HDMI index=1
EOF
	# and fix sven commands
	cp /root/.sven/key_config /tmp
	sed -i "s/Mixer-Volume Up/amixer set Master 10%+/" /tmp/key_config
	sed -i "s/Mixer-Volume Down/amixer set Master 10%-/" /tmp/key_config
	sed -i "s/Mixer-Volume Mute/amixer set Master toggle/" /tmp/key_config
	sed -i "s/mixer.so/cmd/g" /tmp/key_config
	cp /tmp/key_config /root/.sven/key_config

	# Set screen dimensions the FD GUI can handle better
	mkdir -p /root/.zarfy/:0
>> /root/.zarfy/:0/outputs.conf cat << "EOF"
eDP1 3f 0 131 1680 1050 0 0 1
DP1 0 1 0 0 0 0 0 0
DP2 0 1 0 0 0 0 0 0
HDMI1 0 1 0 0 0 0 0 0
HDMI2 0 1 0 0 0 0 0 0
HDMI3 0 1 0 0 0 0 0 0
VIRTUAL1 0 1 0 0 0 0 0 0
EOF

	# TODO: for now spot and other users need to manually copy this configuration from root
	sed -i -e '/^Xft.dpi:/ d' /root/.Xresources
	echo "Xft.dpi: 96" >> /root/.Xresources

	# start webcam if module is available
	echo "facetimehd" >> /etc/modules
	modprobe facetimehd

	# fix sensors in pmcputemp
	patch_pmcputemp

	# Alert user to reboot for full functionality
	write_startup_mbp
}

patch_pmcputemp() {
	( cd /; patch -Np1 << "EOF"
From http://murga-linux.com/puppy/viewtopic.php?p=1027713#1027713
--- a/usr/bin/pmcputemp.sh
+++ b/usr/bin/pmcputemp.sh
@@ -46,7 +46,7 @@
 check_temp_data_exists() {
 	#echo "checking data" # COMMENT THIS IN REAL LIFE
 	# the rough order is Intel, AMD, whatever else
-	for a in `find /sys/devices/platform -type f -name 'temp*_input'|sort` \
+	for a in `find /sys/devices/platform/coretemp.0 -type f -name 'temp*_input'|sort` \
 			`find /sys/devices/pci* -type f -name 'temp*_input'|sort` \
 			`find /sys/devices/virtual -type f -name 'temp'|sort`
 	do find_func "$a"
EOF
	)
}

write_startup_mbp() {
	>> /root/Startup/mbp_info cat << "EOF"
#!/bin/ash
sleep 30 # in case of other startup files
export TEXTDOMAIN=fatdog
. gettext.sh
Xdialog --title "$(gettext 'Reboot is needed')" --infobox  \
	"$(gettext 'Reboot your MacBook for full sound and keyboard support')" \
	0 0 1000000

rm -f /root/Startup/mbp_info
EOF
	chmod 755 /root/Startup/mbp_info
}

# enable gpe suppression, wifi, webcam, mbpfan, sound and keyboard
mbp_mid_2012_13_setup() {

	# enable mbp-specific services
	chmod 755 /etc/init.d/99-disable-spurious-gpe /etc/init.d/15-mbpfan
	# check spurious interrupts
	/etc/init.d/99-disable-spurious-gpe start

	# On first boot b43 driver is active; on next boot switch over to wl driver because it's faster.
	# To go back to b43 run fatdog-service-manager.sh and: 1) stop BC-wl; 2) disable BC-wl; 3) reboot.
	chmod 755 /etc/init.d/30-BC-wl &&
		echo "blacklist b43" >> /etc/modprobe.d/wl.conf

	# add kb-backlight control if it is detected
	# TODO: for now spot and other users need to manually copy this configuration from root
	lsmod | grep -qm1 applesmc &&
>> /root/.sven/key_config cat << "EOF"
[237:0]
type=cmd
enable=1
description=Keyboard backlight down
command=mbp-kb-backlight.sh down
comment=

[238:0]
type=cmd
enable=1
description=Keyboard backlight up
command=mbp-kb-backlight.sh up
comment=
EOF

	# add Xorg arrow keys mapping
>> /etc/X11/app-defaults/Xmodmap cat << "EOF"
keycode 113 = KP_Left KP_4 KP_Left KP_4
keycode 114 = KP_Right KP_6 KP_Right KP_6
EOF

	#TODO top row keycodes 169 XF86Eject eject CD, 128 XF86LaunchA, 212 XF86LaunchB

	# start webcam if module is available
	echo "facetimehd" >> /etc/modules
	modprobe facetimehd

	# fix sensors in pmcputemp
	patch_pmcputemp

	# Alert user to reboot for full functionality
	write_startup_mbp
}

# set analog sound card as default
snd_hda_intel_setup() {
	local cmd

	case "$1" in
		'ao722')
cmd='>> /etc/modprobe.d/snd-hda-intel.conf cat << "EOF"
options snd-hda-intel id=SB index=0
options snd-hda-intel id=Generic index=1
EOF'
		;;
		'toshc75d')
cmd='>> /etc/modprobe.d/snd-hda-intel.conf cat << "EOF"
options snd-hda-intel id=Generic index=0
options snd-hda-intel id=HDMI index=1
EOF'
		;;
	esac

	# Alert user about the wrong sound card order
> /root/Startup/snd_hda_intel_info cat << EOF
#!/bin/ash
sleep 15 # in case of other startup files
export TEXTDOMAIN=fatdog
. gettext.sh
Xdialog --title "\$(gettext 'Request')" --yesno  \
"\$(gettext 'Wrong sound card order detected - HDMI is first, before the analog one.\n\n
Click Yes to fix it automatically and then reboot for the change to take effect.\n\n
Otherwise use Control Panel -> Sound -> Fatdog64 Set Default Sound Card.\n')" \
0 0

if [ \$? -eq 0 ]; then
$cmd

Xdialog --title "\$(gettext 'Info')" --infobox \
"\$(gettext 'The analog card has been set as default in\n
/etc/modprobe.d/snd-hda-intel.conf.')" 0 0 10000	
fi

rm -f /root/Startup/snd_hda_intel_info
EOF
	chmod 755 /root/Startup/snd_hda_intel_info
}

###### main ######
chmod -x /etc/rc.d/rc.platform # only run once

case "$(cat /sys/class/dmi/id/product_name)" in
	'MacBookPro9,2')	mbp_mid_2012_13_setup			;;	# (step)
	'MacBookPro'*)		mbp_setup						;;	# (mavrothal)
	'AO722')			snd_hda_intel_setup ao722		;;	# (JakeSFR)
	'Satellite C75D-A')	snd_hda_intel_setup toshc75d	;;	# (JakeSFR)
esac

# Try to make the font a reasonable size on high dpi displays.
case "$(cat /sys/class/dmi/id/product_name)" in
	'MacBookPro'*)		true							;;
	*)					/usr/bin/set-hidpi-font-size	;;
esac

# vim: ft=sh
