#!/bin/bash
#
# Before running this utility you need to populate the data!
# From a terminal run: jscal -c /dev/input/js0
# Follow on-screen instructions to calibrate controller
# From terminal type: jscal -p /dev/input/js0
# Copy the line it returns and paste it where you see the "jscal -s" lines below
# Do this for every controller you plan to use


zenity --info --text "GAME CONTROLLER PRESET\nThis utility lets you select a calibration for a game controller."

joy=$(zenity --list --text "Game Controller Input" --width "320" --height "240" --radiolist --column "Pick" --column "/dev/input/" TRUE "js0" FALSE "js1" FALSE "js2" FALSE "js3" FALSE "js4"); echo $joy

if [ $joy != "js0" ]; then
zenity --question --text "Shift controller to js0?"
q=$?
if [ $q = "0" ]; then
gksudo ln -s /dev/input/$joy /dev/input/js0
fi
fi

# Enter the Names of your Controllers where it says ">>PUT NAME HERE<<"
# Add or remove entries as needed in format:
# TRUE|FALSE # "Controller Name"

cal=$(zenity --list --text "Game Controller Preset" --width "425" --height "275" --radiolist --column "Pick" --column "#" --column "Controller" TRUE 1 ">>PUT NAME 1 HERE<<" FALSE 2 ">>PUT NAME 2 HERE<<" FALSE 3 ">>PUT NAME 3 HERE<<"); echo $ans

# For each option above have an IF entry below

if [ $cal = "1" ]; then
# CONTROLLER 1
jscal -s 6,1,0,125,125,16776704,14509582,1,1,128,128,15338701,11930101,1,1,87,87,6170742,4098126,1,0,120,120,5368545,5592235,1,0,0,0,536854528,536854528,1,0,0,0,536854528,536854528 /dev/input/$joy
fi
if [ $cal = "2" ]; then
# CONTROLLER 2
jscal -s 3,1,0,124,124,9256113,6100620,1,0,254,254,2113601,-2147483648,1,0,255,255,4793344,-2147483648 /dev/input/$joy

fi
if [ $cal = "3" ]; then
# CONTROLLER 3
jscal -s 5,1,0,-144,-128,2033540,1525155,1,0,255,255,2105312,-2147483648,1,0,255,255,2105312,-2147483648,1,0,0,0,536854528,536854528,1,0,0,0,536854528,536854528 /dev/input/$joy
fi
