swiftpaw1

Linux SDL game controller database fixer and updater

Sep 2nd, 2018
2,672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.21 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Script written by Swiftpaw - 2018/09/02
  4.  
  5. # Define color functions
  6. echo_red() {
  7. echo -e "\e[1m\e[91m$1\e[0m"
  8. }
  9. echo_green() {
  10. echo -e "\e[92m$1\e[0m"
  11. }
  12. echo_greenbold() {
  13. echo -e "\e[1m\e[92m$1\e[0m"
  14. }
  15. echo_yellow() {
  16. echo -e "\e[93m$1\e[0m"
  17. }
  18.  
  19. # Define variables
  20. NEWFILEOK=0
  21. OLDFILEOK=0
  22.  
  23. # Verify script is running as normal user
  24. if ! ((EUID)); then
  25.         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."
  26.         exit 1
  27. fi
  28.  
  29. # Change to user's home directory
  30. echo_green "Changing directory to $HOME"
  31. cd $HOME
  32.  
  33. # Download newest database file
  34. echo_green "Downloading newest controller database file"
  35. wget -nv https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt -O ./gamecontrollerdb.txt-new
  36.  
  37. # Check if download was successful or not
  38. # Check if downloaded file exists.
  39. if [ -e ./gamecontrollerdb.txt-new ] ; then
  40.     # Check if downloaded file is greater than 180 lines.  If so, set NEWFILEOK=1, else set to 0.
  41.     if [ $(cat ./gamecontrollerdb.txt-new | wc -l) -ge 180 ] ; then
  42.         echo_green "New database download appears to be OK"
  43.         NEWFILEOK=1
  44.     else
  45.         echo_yellow "New database download file is under 180 lines, so not using it"
  46.         NEWFILEOK=0
  47.     fi
  48. else
  49. # If downloaded file doesn't exist,  don't use it by setting NEWFILEOK=0.
  50.     echo_yellow "New database download file not found, so download must have failed."
  51.     NEWFILEOK=0
  52. fi
  53.  
  54. # Check if old database file exists...
  55. if [ -e ./gamecontrollerdb.txt ] ; then
  56.     # ...and if it has enough lines.
  57.     if [ $(cat ./gamecontrollerdb.txt | wc -l) -gt 180 ] ; then
  58.         OLDFILEOK=1
  59.         echo_green "Old database file exists and looks okay"
  60.     else
  61.         OLDFILEOK=0
  62.         echo_yellow "Old database file exists, but doesn't look okay, has less than 180 lines"
  63.     fi
  64. else
  65.     OLDFILEOK=0
  66.     echo_green "No existing database file found"
  67. fi
  68.  
  69. # If old database file doesn't exist or is bad, and new database download failed, exit.
  70. if [ $OLDFILEOK -eq 0 ] && [ $NEWFILEOK -eq 0 ] ; then
  71.     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"
  72.     exit 1
  73. fi
  74.  
  75. # If both old and new files exist and are OK, make a backup of old file, then move the new one into its place.
  76. if [ $OLDFILEOK -eq 1 ] && [ $NEWFILEOK -eq 1 ] ; then
  77.     mv ./gamecontrollerdb.txt ./gamecontrollerdb.txt-backup
  78.     echo_green "Backup of gamecontrollerdb.txt saved to $HOME/gamecontrollerdb.txt-backup"
  79.     mv ./gamecontrollerdb.txt-new ./gamecontrollerdb.txt
  80.     echo_green "Newly downloaded game controller database file saved to $HOME/gamecontrollerdb.txt"
  81.     # Compare the old and new files and report if there is a difference
  82.     if [ $(diff ./gamecontrollerdb.txt ./gamecontrollerdb.txt-backup | wc -l) -eq 0 ] ; then
  83.         echo_yellow "New database file doesn't contain any updates"
  84.     else
  85.         echo_greenbold "New database file contained an update!"
  86.     fi
  87. fi
  88.  
  89. # If downloaded new database file is the only file (for new installs), just move it into place
  90. if [ $OLDFILEOK -eq 0 ] && [ $NEWFILEOK -eq 1 ] ; then
  91.         mv ./gamecontrollerdb.txt-new ./gamecontrollerdb.txt
  92.         echo_green "Newly downloaded game controller database file saved to $HOME/gamecontrollerdb.txt"
  93. fi
  94.  
  95. # Only take the Linux controller lines from the database file, and save them to .controller_config
  96. grep Linux gamecontrollerdb.txt | grep -v '# Linux' > .controller_config
  97.  
  98. # Add SDL_GAMECONTROLLERCONFIG variable text to .controller_config file and wrap the variable in quotation marks
  99. sed -i '1s/^/export SDL_GAMECONTROLLERCONFIG="/' .controller_config
  100. sed -i '$s/$/&"/' .controller_config
  101.  
  102. echo_green "Database variable source file created from $HOME/gamecontrollerdb.txt and placed into $HOME/.controller_config"
  103.  
  104. # Configure the user's profile to source the SDL_GAMECONTROLLERCONFIG variable from the .controller_config file
  105. if [ -e .bashrc ] ; then
  106.     if ! $(grep -q '. "$HOME/.controller_config"' .bashrc); then
  107.         echo '. "$HOME/.controller_config"' >> .bashrc
  108.         echo_green "Line to source .controller_config was added to your .bashrc file"
  109.     fi
  110. fi
  111.  
  112. if [ -e .bash_profile ] ; then
  113.         if ! $(grep -q '. "$HOME/.controller_config"' .bash_profile); then
  114.                 echo '. "$HOME/.controller_config"' >> .bash_profile
  115.         echo_green "Line to source .controller_config was added to your .bash_profile file"
  116.         fi
  117. fi
  118.  
  119. if [ -e .profile ] ; then
  120.         if ! $(grep -q '. "$HOME/.controller_config"' .profile); then
  121.                 echo '. "$HOME/.controller_config"' >> .profile
  122.         echo_green "Line to source .controller_config was added to your .profile file"
  123.         fi
  124. fi
  125.  
  126. # Print that script has finished, wait in case user used temporary terminal
  127. echo_greenbold "Script complete.  Please log out and back in."
  128. echo_greenbold "Waiting 30 seconds, or hit CTRL-C to exit."
  129. sleep 30
Advertisement
Add Comment
Please, Sign In to add comment