1
0

rclone_script-menu.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 --long)
  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. case ${neededConnection} in
  51. 0) statusNeededConnection="Internet access" ;;
  52. 1) statusNeededConnection="LAN / WLAN" ;;
  53. esac
  54. }
  55. function saveConfig ()
  56. {
  57. echo "remotebasedir=${remotebasedir}" > ${config}
  58. echo "showNotifications=${showNotifications}" >> ${config}
  59. echo "syncOnStartStop=${syncOnStartStop}" >> ${config}
  60. echo "logfile=~/scripts/rclone_script/rclone_script.log" >> ${config}
  61. echo "neededConnection=${neededConnection}" >> ${config}
  62. echo "debug=0" >> ${config}
  63. }
  64. ##################
  65. # MENU FUNCTIONS #
  66. ##################
  67. # Show the main menu. Return here anytime another dialog is closed
  68. function main_menu ()
  69. {
  70. local choice="1"
  71. while true
  72. do
  73. getStatusOfParameters
  74. choice=$(dialog \
  75. --stdout \
  76. --colors \
  77. --backtitle "${backtitle}" \
  78. --title "main menu" \
  79. --default-item "${choice}" \
  80. --menu "\nWhat do you want to do?" 25 75 20 \
  81. 1 "Full synchronization of all savefiles and statefiles" \
  82. 2 "Toggle \"Synchronize saves on start / stop\" (currently ${statusSyncOnStartStop})" \
  83. 3 "Toggle \"Show notifications on sync\" (currently ${statusShowNotifications})" \
  84. 4 "Set needed Connection (currently \"${statusNeededConnection}\")" \
  85. "" ""\
  86. 9 "uninstall RCLONE_SCRIPT"
  87. )
  88. case "$choice" in
  89. 1) doFullSync ;;
  90. 2) toggleSyncOnStartStop ;;
  91. 3) toggleShowNotifications ;;
  92. 4) setNeededConnection ;;
  93. 9) ~/scripts/rclone_script/rclone_script-uninstall.sh ;;
  94. *) break ;;
  95. esac
  96. done
  97. }
  98. # Syncs all files in both directions, only transferring newer files
  99. function doFullSync ()
  100. {
  101. local tmpfile=~/scripts/rclone_script/tmp-sync.txt
  102. getTypeOfRemote
  103. printf "\nStarted full sync...\n\n" > ${tmpfile}
  104. log "INFO" "Started full sync..."
  105. # start sync process in background
  106. {
  107. # Download newer files from remote to local
  108. printf "Downloading newer files from retropie:${remotebasedir} (${remoteType}) to ~/RetroPie/saves/...\n"
  109. rclone copy retropie:${remotebasedir}/ ~/RetroPie/saves/ --update --skip-links --exclude "readme.txt" --verbose 2>&1
  110. # Upload newer files from local to remote
  111. printf "Uploading newer files from ~/RetroPie/saves/ to retropie:${remotebasedir} (${remoteType})...\n"
  112. rclone copy ~/RetroPie/saves/ retropie:${remotebasedir}/ --update --skip-links --exclude "readme.txt" --verbose 2>&1
  113. printf "Done\n"
  114. } >> ${tmpfile} & # capture output of background process
  115. dialog \
  116. --backtitle "${backtitle}" \
  117. --title "Doing full sync..." \
  118. --colors \
  119. --no-collapse \
  120. --cr-wrap \
  121. --tailbox ${tmpfile} 40 120
  122. wait
  123. cat ${tmpfile} >> ${logfile}
  124. rm ${tmpfile}
  125. log "INFO" "Finished full sync..."
  126. }
  127. function toggleSyncOnStartStop ()
  128. {
  129. if [ "${syncOnStartStop}" == "TRUE" ]
  130. then
  131. syncOnStartStop="FALSE"
  132. else
  133. syncOnStartStop="TRUE"
  134. fi
  135. saveConfig
  136. }
  137. function toggleShowNotifications ()
  138. {
  139. if [ "${showNotifications}" == "TRUE" ]
  140. then
  141. showNotifications="FALSE"
  142. else
  143. showNotifications="TRUE"
  144. fi
  145. saveConfig
  146. }
  147. function setNeededConnection ()
  148. {
  149. choice=$(dialog \
  150. --stdout \
  151. --colors \
  152. --no-collapse \
  153. --cr-wrap \
  154. --backtitle "${backtitle}" \
  155. --title "Needed connection" \
  156. --default-item "${neededConnection}" \
  157. --ok-label "Select" \
  158. --menu "\nPlease select which type of connection will be needed for your configured remote" 20 50 5 \
  159. 0 "Internet access" \
  160. 1 "LAN / WLAN connection only"
  161. )
  162. case ${choice} in
  163. 0) neededConnection=0 ;;
  164. 1) neededConnection=1 ;;
  165. *) return ;;
  166. esac
  167. saveConfig
  168. }
  169. ########
  170. # MAIN #
  171. ########
  172. main_menu