#!/bin/bash
USER_HOME=$(eval echo ~)
STEAM_USER_HOME="${USER_HOME}/Library/Application Support/Steam/"
STEAM_CONFIG_FILE="${STEAM_USER_HOME}config/config.vdf"
CAVE_CONFIG_FILE="${STEAM_USER_HOME}SteamApps/common/TheCave/Cave.app/Contents/Resources/Data/Config/SDLGamepad.config"
if [ ! -e "${STEAM_CONFIG_FILE}" ]; then
echo "Could not find Steam user config file."
exit 1
fi
if [ ! -e "${CAVE_CONFIG_FILE}" ]; then
echo "Could not find 'The Cave' SDL Gamepad config file."
exit 1
fi
clear
printf "Steam user data found: '%s'\n" "${STEAM_USER_HOME}"
printf "Steam config file found: 'config/config.vdf'\n"
printf "The Cave gamepad file found: 'SteamApps/common/TheCave/Cave.app/Contents/Resources/Data/Config/SDLGamepad.config'\n\n"
SDL_GAMECONTROLLER_STRING=`cat "${STEAM_CONFIG_FILE}" | grep '"SDL_GamepadBind"' | perl -nle 'm/"SDL_GamepadBind"\s*"(.*)"/; print $1'`
if [ -z "${SDL_GAMECONTROLLER_STRING}" ]; then
echo "Failed to extract controller key binding. Did you configure it in Steam's 'Big Picture' mode?"
exit 1
fi
OLD_IFS=$IFS
printf "Controller data:\n"
IFS=','
CONTROLLER_DATA=( $SDL_GAMECONTROLLER_STRING )
CONTROLLER_NAME=${CONTROLLER_DATA[1]}
CONTROLLER_GUID=${CONTROLLER_DATA[0]}
printf "\tName = %s\n\tGUID = %s\n" "${CONTROLLER_NAME}" "${CONTROLLER_GUID}"
IFS=':'
INDEX=0;
for i in {2..22}; do
BUTTON_DATA=( ${CONTROLLER_DATA[i]} )
if [ "${BUTTON_DATA[1]}" = "" ]; then
printf "\t%s --> %s\n" "${BUTTON_DATA[0]}" "UNBOUND"
elif [ "${BUTTON_DATA[1]:0:1}" = "b" ]; then
printf "\t%s --> button %s\n" "${BUTTON_DATA[0]}" "${BUTTON_DATA[1]:1}"
else
printf "\t%s --> axis %s\n" "${BUTTON_DATA[0]}" "${BUTTON_DATA[1]:1}"
fi
done
IFS=$OLD_IFS
printf "\n"
TEST_FOR_EXACT_EXISTENCE=`grep "${SDL_GAMECONTROLLER_STRING}" "${CAVE_CONFIG_FILE}"`
if [ -n "${TEST_FOR_EXACT_EXISTENCE}" ]; then
echo "This exact controller config string is already in the gamepad config file for The Cave."
exit 0
fi
TEST_FOR_GUID_EXISTENCE=`grep "${CONTROLLER_GUID}" "${CAVE_CONFIG_FILE}"`
if [ -n "${TEST_FOR_GUID_EXISTENCE}" ]; then
echo "This controller ID is already in the gamepad config file for The Cave but the name or button bindings have changed."
# Look for backup of origianl config file
if [ ! -f "${CAVE_CONFIG_FILE}.orig" ]; then
echo "There is no backup file to use. You must delete the existing string in the config file manually in order to use your new settings."
exit 1
fi
# Is this controller ID in the backup file too
TEST_FOR_GUID_EXISTENCE2=`grep "${CONTROLLER_GUID}" "${CAVE_CONFIG_FILE}.orig"`
if [ -n "${TEST_FOR_GUID_EXISTENCE2}" ]; then # (shit)
echo "The backup config file already has this controller ID too."
echo "You must manually remove the existing string for this controller in the config file in order to use your new settings."
exit 1
fi
# If user wants to use the new settings then just restore the backup
while true; do
read -p "Do you wish to replace the old name and bindings with the new ones? [y/n]: " yn
case $yn in
[Yy]* ) cp "${CAVE_CONFIG_FILE}.orig" "${CAVE_CONFIG_FILE}"; break;;
[Nn]* ) exit 1;;
* ) echo "Please answer yes or no.";;
esac
done
fi
printf "Backing up original The Cave config file to %s\n" "${CAVE_CONFIG_FILE}.orig"
cp "${CAVE_CONFIG_FILE}" "${CAVE_CONFIG_FILE}.orig"
printf "Adding controller SDL binding to config file ... "
printf "\n# Controller configuration extracted from Steam 'config.vdf' file\n%s\n\n" "${SDL_GAMECONTROLLER_STRING}" >> "${CAVE_CONFIG_FILE}"
printf "Done!\n"