rclone_script-menu.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. NORMAL="\Zn"
  3. BLACK="\Z0"
  4. RED="\Z1"
  5. GREEN="\Z2"
  6. YELLOW="\Z3\Zb"
  7. BLUE="\Z4"
  8. MAGENTA="\Z5"
  9. CYAN="\Z6"
  10. WHITE="\Z7"
  11. BOLD="\Zb"
  12. REVERSE="\Zr"
  13. UNDERLINE="\Zu"
  14. # include settings file
  15. config=~/scripts/rclone_script/rclone_script.ini
  16. source ${config}
  17. backtitle="RCLONE_SCRIPT menu (https://github.com/Jandalf81/rclone_script)"
  18. ####################
  19. # HELPER FUNCTIONS #
  20. ####################
  21. function log ()
  22. {
  23. severity=$1
  24. message=$2
  25. printf "$(date +%FT%T%:z):\t${severity}:\t${message}\n" >> ${logfile}
  26. }
  27. function getTypeOfRemote ()
  28. {
  29. # list all remotes and their type
  30. remotes=$(rclone listremotes -l)
  31. # get line wiht RETROPIE remote
  32. retval=$(grep -i "^retropie:" <<< ${remotes})
  33. remoteType="${retval#*:}"
  34. remoteType=$(echo ${remoteType} | xargs)
  35. }
  36. function getStatusOfParameters ()
  37. {
  38. if [ "${syncOnStartStop}" == "TRUE" ]
  39. then
  40. statusSyncOnStartStop="${GREEN}ENABLED${NORMAL}"
  41. else
  42. statusSyncOnStartStop="${RED}DISABLED${NORMAL}"
  43. fi
  44. if [ "${showNotifications}" == "TRUE" ]
  45. then
  46. statusShowNotifications="${GREEN}ENABLED${NORMAL}"
  47. else
  48. statusShowNotifications="${RED}DISABLED${NORMAL}"
  49. fi
  50. }
  51. function saveConfig ()
  52. {
  53. echo "remotebasedir=${remotebasedir}" > ${config}
  54. echo "showNotifications=${showNotifications}" >> ${config}
  55. echo "syncOnStartStop=${syncOnStartStop}" >> ${config}
  56. echo "logfile=~/scripts/rclone_script/rclone_script.log" >> ${config}
  57. echo "debug=0" >> ${config}
  58. }
  59. ##################
  60. # MENU FUNCTIONS #
  61. ##################
  62. # Show the main menu. Return here anytime another dialog is closed
  63. function main_menu ()
  64. {
  65. local choice="1"
  66. while true
  67. do
  68. getStatusOfParameters
  69. choice=$(dialog \
  70. --stdout \
  71. --colors \
  72. --backtitle "${backtitle}" \
  73. --title "main menu" \
  74. --default-item "${choice}" \
  75. --menu "\nWhat do you want to do?" 25 75 20 \
  76. 1 "Full synchronization of all savefiles and statefiles" \
  77. 2 "Toggle \"Synchronize saves on start / stop\" (currently ${statusSyncOnStartStop})" \
  78. 3 "Toggle \"Show notifications on sync\" (currently ${statusShowNotifications})" \
  79. "" ""\
  80. 9 "uninstall RCLONE_SCRIPT"
  81. )
  82. case "$choice" in
  83. 1) doFullSync ;;
  84. 2) toggleSyncOnStartStop ;;
  85. 3) toggleShowNotifications ;;
  86. 9) ~/scripts/rclone_script/rclone_script-uninstall.sh ;;
  87. *) break ;;
  88. esac
  89. done
  90. }
  91. # Syncs all files in both directions, only transferring newer files
  92. function doFullSync ()
  93. {
  94. local tmpfile=~/scripts/rclone_script/tmp-sync.txt
  95. getTypeOfRemote
  96. printf "\nStarted full sync...\n\n" > ${tmpfile}
  97. log "INFO" "Started full sync..."
  98. # start sync process in background
  99. {
  100. # Download newer files from remote to local
  101. printf "Downloading newer files from retropie:${remotebasedir} (${remoteType}) to ~/RetroPie/saves/...\n"
  102. rclone copy retropie:${remotebasedir}/ ~/RetroPie/saves/ --update --verbose 2>&1
  103. # Upload newer files from local to remote
  104. printf "Uploading newer files from ~/RetroPie/saves/ to retropie:${remotebasedir} (${remoteType})...\n"
  105. rclone copy ~/RetroPie/saves/ retropie:${remotebasedir}/ --update --verbose 2>&1
  106. printf "Done\n"
  107. } >> ${tmpfile} & # capture output of background process
  108. dialog \
  109. --backtitle "${backtitle}" \
  110. --title "Doing full sync..." \
  111. --colors \
  112. --no-collapse \
  113. --cr-wrap \
  114. --tailbox ${tmpfile} 40 120
  115. wait
  116. cat ${tmpfile} >> ${logfile}
  117. rm ${tmpfile}
  118. log "INFO" "Finished full sync..."
  119. }
  120. function toggleSyncOnStartStop ()
  121. {
  122. if [ "${syncOnStartStop}" == "TRUE" ]
  123. then
  124. syncOnStartStop="FALSE"
  125. else
  126. syncOnStartStop="TRUE"
  127. fi
  128. saveConfig
  129. }
  130. function toggleShowNotifications ()
  131. {
  132. if [ "${showNotifications}" == "TRUE" ]
  133. then
  134. showNotifications="FALSE"
  135. else
  136. showNotifications="TRUE"
  137. fi
  138. saveConfig
  139. }
  140. ########
  141. # MAIN #
  142. ########
  143. main_menu