#!/bin/sh

# The kernel caches recently enumerated directories, so lets pre-cache the Roms directories for a noticeable speed-boost in the GUI
find /mnt/mmc/Roms &
find /mnt/SDCARD/Roms &

# Restore the maximum CPU frequency
export HOME=/mnt/mmc/CFW
export SDL_NOMOUSE=1
cd $HOME
./main restorecpu

# The last shutdown was dirty (as in, triggered by the reset button)
if [ ! -f /mnt/mmc/CFW/shutdown.flag ]
then
	# Erase the resume command file
	rm /mnt/mmc/CFW/retroarch/command.sh
	rm /mnt/mmc/CFW/retroarch/gdbscript.txt
fi

# Erase the clean shutdown flag so we don't get stuck in one state
rm /mnt/mmc/CFW/shutdown.flag

# Execute all auto-start scripts
for STARTSCRIPT in /mnt/mmc/CFW/autostart/*.sh
do
	sh "$STARTSCRIPT"
done

## title-label
if [ -f /mnt/mmc/Mod/title.txt ]
then
	title=`cat /mnt/mmc/Mod/title.txt`
	find "/mnt/mmc/CFW/lang" -name "*.json" -type f | while read -r langfile
	do
		sed -i '/title-label/d' "$langfile"
		sed -i '/}/d' "$langfile"
		echo "	\"title-label\": \"%s - ${title}\"" >> "$langfile"
		echo "}" >> "$langfile"
		sync
	done
fi

## Mount Mod
mkdir -p /mnt/mmc/Roms/-THEMES
rm -rf /mnt/mmc/Roms/-THEMES/*
mount /mnt/mmc/Mod/themes /mnt/mmc/Roms/-THEMES
mount /mnt/mmc/Mod/overlay /mnt/mmc/CFW/retroarch/.retroarch/overlay
mount /mnt/mmc/Mod/cheats /mnt/mmc/CFW/retroarch/.retroarch/cheats
cp -f /mnt/mmc/CFW/skin/system/CORES.png /mnt/mmc/CFW/skin/system/-CORES.png
cp -f /mnt/mmc/CFW/skin/system/THEMES.png /mnt/mmc/CFW/skin/system/-THEMES.png
cp -f /mnt/mmc/CFW/skin/system/PDF.png /mnt/mmc/CFW/skin/system/-PDF.png

## Adaptive language
if [ ! -f /mnt/mmc/CFW/os/apps.flag ]
then
	if grep -q "简体中文" /mnt/mmc/CFW/language.flag; then
		echo zh_CN > /mnt/mmc/CFW/os/apps.flag
	else
		echo en_US > /mnt/mmc/CFW/os/apps.flag
	fi
else
	if grep -q "简体中文" /mnt/mmc/CFW/language.flag; then
		if grep -q "en_US" /mnt/mmc/CFW/os/apps.flag; then
			find "/mnt/mmc/Roms/APPS" -name "sw.sh" -type f | while read -r swsh
			do
				$swsh 1 &> /dev/null
			done
			find "/mnt/mmc/Roms/PORTS" -name "sw.sh" -type f | while read -r swsh
			do
				$swsh 1 &> /dev/null
			done
			cp -f /mnt/mmc/CFW/bin/mame.csv /mnt/mmc/CFW/config/mame.csv
			cp -f /mnt/mmc/CFW/retroarch/retroarch_cn /mnt/mmc/CFW/retroarch/retroarch
			echo zh_CN > /mnt/mmc/CFW/os/apps.flag
		fi
	else
		if grep -q "zh_CN" /mnt/mmc/CFW/os/apps.flag; then
			find "/mnt/mmc/Roms/APPS" -name "sw.sh" -type f | while read -r swsh
			do
				$swsh 2 &> /dev/null
			done
			find "/mnt/mmc/Roms/PORTS" -name "sw.sh" -type f | while read -r swsh
			do
				$swsh 2 &> /dev/null
			done
			cp -f /mnt/mmc/CFW/bin/mame_en.csv /mnt/mmc/CFW/config/mame.csv
			cp -f /mnt/mmc/CFW/retroarch/retroarch_en /mnt/mmc/CFW/retroarch/retroarch
			echo en_US > /mnt/mmc/CFW/os/apps.flag
		fi
	fi
fi

## Auto backup reset file
mkdir -p /mnt/mmc/Roms/APPS/reset
if [ ! -f /mnt/mmc/Roms/APPS/reset/reset.zip ]
then
	for file in /mnt/mmc/CFW/config/coremapping.json \
		/mnt/mmc/CFW/retroarch/.retroarch/retroarch.cfg \
		/mnt/mmc/CFW/retroarch/.retroarch/config/
	do
		zip -g -r /mnt/mmc/Roms/APPS/reset/reset.zip $file
	done
fi

# The operation loop
while true
do
	# Reset the rumble motor
	echo 0 > /sys/class/power_supply/battery/moto

	# Switch to the interactive governor (try to save battery when possible but try not to sacrifice performance too much)
	echo interactive > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

	# Wait for the kernel to populare the interactive sysfs
	sleep 0.1

	# Tweak the interactive target loads
	echo "50 504000:40 720000:40 840000:80 1008000:70 1104000:70 1200000:70 1296000:70 1392000:80 1488000:99" > /sys/devices/system/cpu/cpufreq/interactive/target_loads

	# We've got no pending RetroArch commands
	if [ ! -f /mnt/mmc/CFW/retroarch/command.sh ]
	then
		# Start Garlic
		export HOME=/mnt/mmc/CFW
		export SDL_NOMOUSE=1
		cd $HOME
		./main
	fi

	# Start RetroArch
	export HOME=/mnt/mmc/CFW/retroarch
	cd $HOME
	/mnt/mmc/CFW/retroarch/command.sh

	# Shutdown safely (if requested)
	if [ -f /mnt/mmc/CFW/shutdown.flag ]
	then
		# Sync files to disk and power off
		sync
		mount -o remount,ro /mnt/mmc
		mount -o remount,ro /mnt/SDCARD
		poweroff -f
	else
		# Erase the previous RetroArch command
		rm /mnt/mmc/CFW/retroarch/command.sh
		rm /mnt/mmc/CFW/retroarch/gdbscript.txt
	fi
done
