#!/bin/sh

if [ ! -e "/dev/input/o2x_js0" ]; then
	exit 1
fi
rmmod dummyjoy
echo "Rebuilding js devfs entries"
if [ -L "/dev/js0" ]; then
	rm /dev/js0
fi
if [ -L "/dev/js1" ]; then
	rm /dev/js1
fi
if [ -L "/dev/js2" ]; then
	rm /dev/js2
fi
if [ -L "/dev/js3" ]; then
	rm /dev/js3
fi
if [ -L "/dev/input/js0" ]; then
	rm /dev/input/js0
fi
if [ -L "/dev/input/js1" ]; then
	rm /dev/input/js1
fi
if [ -L "/dev/input/js2" ]; then
	rm /dev/input/js2
fi
if [ -L "/dev/input/js3" ]; then
	rm /dev/input/js3
fi
echo "Adding fake js0 devfs entry"
# This is too fool some programs like PocketSNES into using
# the 2nd gamepad control for player-2 controls since they
# don't offer flexible gamepad remapping like Notaz's emus.
# Dummyjoy is a simply kernel module that only acts like a joystick.
# So it should never register events and the real player 1 events
# will come from joy2xd's forcing of GPIO lines based on input/o2x_js0
# SIMPLE, NO? :)
# (Simply making a dummy js0 device node is not enough to fool some programs.)
# If two joys are connected, dummyjoy should show up as /dev/input/o2x_js2.

if [ ! -e "/dev/input/o2x_js1" ]; then
	# If only one joystick is connected, exit, as we
	# already removed all symlinks to it and thus no
	# dummy module needs to fill its gap
	exit 0
else
	# only necessary to load this once and only if there are 
	#	two or more joysticks.
	modprobe dummyjoy
fi

if [ -e "/dev/input/o2x_js1" ]; then
	if [ -e "/dev/input/o2x_js2" ]; then
		# Not the last device, must be real
		echo "Detected 2nd USB joy, symlinking to js1"
		ln -s /dev/input/o2x_js1 /dev/input/js1
		ln -s /dev/input/o2x_js1 /dev/js1
	else
		# Hit the last joy driver, this must be the dummyjoy
		echo "Symlinking dummy joy to js0"
		ln -s /dev/input/o2x_js1 /dev/input/js0
		ln -s /dev/input/o2x_js1 /dev/js0
	fi
else
	exit 0;
fi

if [ -e "/dev/input/o2x_js2" ]; then
	if [ -e "/dev/input/o2x_js3" ]; then
		# Not the last device, must be real
		echo "Detected 3rd USB joy, symlinking to js2"
		ln -s /dev/input/o2x_js2 /dev/input/js2
		ln -s /dev/input/o2x_js2 /dev/js2
	else
		# Hit the last joy driver, this must be the dummyjoy
		echo "Symlinking dummy joy to js0"
		ln -s /dev/input/o2x_js2 /dev/input/js0
		ln -s /dev/input/o2x_js2 /dev/js0
	fi
fi

if [ -e "/dev/input/o2x_js3" ]; then
	if [ -e "/dev/input/o2x_js4" ]; then
		# Not the last device, must be real
		echo "Detected 4th USB joy, symlinking to js3"
		ln -s /dev/input/o2x_js3 /dev/input/js3
		ln -s /dev/input/o2x_js3 /dev/js3
	else
		# Hit the last joy driver, this must be the dummyjoy
		echo "Symlinking dummy joy to js0"
		ln -s /dev/input/o2x_js3 /dev/input/js0
		ln -s /dev/input/o2x_js3 /dev/js0
	fi
fi

if [ -e "/dev/input/o2x_js4" ]; then
	if [ -e "/dev/input/o2x_js5" ]; then
		# Not the last device, must be real
		echo "WARNING: detected 5th USB joy, something is likely wrong."
		echo "Symlinking to js4"
		ln -s /dev/input/o2x_js4 /dev/input/js4
		ln -s /dev/input/o2x_js4 /dev/js4
	else
		# Hit the last joy driver, this must be the dummyjoy
		echo "Symlinking dummy joy to js0"
		ln -s /dev/input_o2x_js4 /dev/input/js0
		ln -s /dev/input/o2x_js4 /dev/js0
	fi
fi
exit 0
