Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Script written by Swiftpaw - 2018/09/02
- # Define color functions
- echo_red() {
- echo -e "\e[1m\e[91m$1\e[0m"
- }
- echo_green() {
- echo -e "\e[92m$1\e[0m"
- }
- echo_greenbold() {
- echo -e "\e[1m\e[92m$1\e[0m"
- }
- echo_yellow() {
- echo -e "\e[93m$1\e[0m"
- }
- # Define variables
- NEWFILEOK=0
- OLDFILEOK=0
- # Verify script is running as normal user
- if ! ((EUID)); then
- echo_red "Do not run script with root privilages. Script needs to modify a user's profile files. Changes to /etc/environment file are not possible for the multiple controller support which this script enables."
- exit 1
- fi
- # Change to user's home directory
- echo_green "Changing directory to $HOME"
- cd $HOME
- # Download newest database file
- echo_green "Downloading newest controller database file"
- wget -nv https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt -O ./gamecontrollerdb.txt-new
- # Check if download was successful or not
- # Check if downloaded file exists.
- if [ -e ./gamecontrollerdb.txt-new ] ; then
- # Check if downloaded file is greater than 180 lines. If so, set NEWFILEOK=1, else set to 0.
- if [ $(cat ./gamecontrollerdb.txt-new | wc -l) -ge 180 ] ; then
- echo_green "New database download appears to be OK"
- NEWFILEOK=1
- else
- echo_yellow "New database download file is under 180 lines, so not using it"
- NEWFILEOK=0
- fi
- else
- # If downloaded file doesn't exist, don't use it by setting NEWFILEOK=0.
- echo_yellow "New database download file not found, so download must have failed."
- NEWFILEOK=0
- fi
- # Check if old database file exists...
- if [ -e ./gamecontrollerdb.txt ] ; then
- # ...and if it has enough lines.
- if [ $(cat ./gamecontrollerdb.txt | wc -l) -gt 180 ] ; then
- OLDFILEOK=1
- echo_green "Old database file exists and looks okay"
- else
- OLDFILEOK=0
- echo_yellow "Old database file exists, but doesn't look okay, has less than 180 lines"
- fi
- else
- OLDFILEOK=0
- echo_green "No existing database file found"
- fi
- # If old database file doesn't exist or is bad, and new database download failed, exit.
- if [ $OLDFILEOK -eq 0 ] && [ $NEWFILEOK -eq 0 ] ; then
- echo_red "Download failure, and no $HOME/gamecontrollerdb.txt file found or file has less than 180 lines in it. Assuming this is a new database installation. Please find a working source for the game database file which was located at https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt and https://github.com/gabomdq/SDL_GameControllerDB/blob/master/gamecontrollerdb.txt"
- exit 1
- fi
- # If both old and new files exist and are OK, make a backup of old file, then move the new one into its place.
- if [ $OLDFILEOK -eq 1 ] && [ $NEWFILEOK -eq 1 ] ; then
- mv ./gamecontrollerdb.txt ./gamecontrollerdb.txt-backup
- echo_green "Backup of gamecontrollerdb.txt saved to $HOME/gamecontrollerdb.txt-backup"
- mv ./gamecontrollerdb.txt-new ./gamecontrollerdb.txt
- echo_green "Newly downloaded game controller database file saved to $HOME/gamecontrollerdb.txt"
- # Compare the old and new files and report if there is a difference
- if [ $(diff ./gamecontrollerdb.txt ./gamecontrollerdb.txt-backup | wc -l) -eq 0 ] ; then
- echo_yellow "New database file doesn't contain any updates"
- else
- echo_greenbold "New database file contained an update!"
- fi
- fi
- # If downloaded new database file is the only file (for new installs), just move it into place
- if [ $OLDFILEOK -eq 0 ] && [ $NEWFILEOK -eq 1 ] ; then
- mv ./gamecontrollerdb.txt-new ./gamecontrollerdb.txt
- echo_green "Newly downloaded game controller database file saved to $HOME/gamecontrollerdb.txt"
- fi
- # Only take the Linux controller lines from the database file, and save them to .controller_config
- grep Linux gamecontrollerdb.txt | grep -v '# Linux' > .controller_config
- # Add SDL_GAMECONTROLLERCONFIG variable text to .controller_config file and wrap the variable in quotation marks
- sed -i '1s/^/export SDL_GAMECONTROLLERCONFIG="/' .controller_config
- sed -i '$s/$/&"/' .controller_config
- echo_green "Database variable source file created from $HOME/gamecontrollerdb.txt and placed into $HOME/.controller_config"
- # Configure the user's profile to source the SDL_GAMECONTROLLERCONFIG variable from the .controller_config file
- if [ -e .bashrc ] ; then
- if ! $(grep -q '. "$HOME/.controller_config"' .bashrc); then
- echo '. "$HOME/.controller_config"' >> .bashrc
- echo_green "Line to source .controller_config was added to your .bashrc file"
- fi
- fi
- if [ -e .bash_profile ] ; then
- if ! $(grep -q '. "$HOME/.controller_config"' .bash_profile); then
- echo '. "$HOME/.controller_config"' >> .bash_profile
- echo_green "Line to source .controller_config was added to your .bash_profile file"
- fi
- fi
- if [ -e .profile ] ; then
- if ! $(grep -q '. "$HOME/.controller_config"' .profile); then
- echo '. "$HOME/.controller_config"' >> .profile
- echo_green "Line to source .controller_config was added to your .profile file"
- fi
- fi
- # Print that script has finished, wait in case user used temporary terminal
- echo_greenbold "Script complete. Please log out and back in."
- echo_greenbold "Waiting 30 seconds, or hit CTRL-C to exit."
- sleep 30
Advertisement
Add Comment
Please, Sign In to add comment