rclone_script.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/bin/bash
  2. # define colors for output
  3. NORMAL=$(tput sgr0)
  4. RED=$(tput setaf 1)
  5. GREEN=$(tput setaf 2)
  6. YELLOW=$(tput setaf 3)
  7. BLUE=$(tput setaf 4)
  8. UNDERLINE=$(tput smul)
  9. # include settings file
  10. config=~/scripts/rclone_script/rclone_script.ini
  11. source ${config}
  12. # parameters
  13. direction="$1"
  14. system="$2"
  15. emulator="$3"
  16. rom="$4"
  17. command="$5"
  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 debug ()
  28. {
  29. log "DEBUG" "direction: ${direction}"
  30. log "DEBUG" "system: ${system}"
  31. log "DEBUG" "emulator: ${emulator}"
  32. log "DEBUG" "rom: ${rom}"
  33. log "DEBUG" "command: ${command}"
  34. log "DEBUG" "remotebasedir: ${remotebasedir}"
  35. log "DEBUG" "rompath: ${rompath}"
  36. log "DEBUG" "romfilename: ${romfilename}"
  37. log "DEBUG" "romfilebase: ${romfilebase}"
  38. log "DEBUG" "romfileext: ${romfileext}"
  39. }
  40. function killOtherNotification ()
  41. {
  42. # get PID of other PNGVIEW process
  43. otherPID=$(pgrep --full pngview)
  44. if [ "${debug}" = "1" ]; then log "DEBUG" "Other PIDs: ${otherPID}"; fi
  45. if [ "${otherPID}" != "" ]
  46. then
  47. if [ "${debug}" = "1" ]; then log "DEBUG" "Kill other PNGVIEW ${otherPID}"; fi
  48. kill ${otherPID}
  49. fi
  50. }
  51. function showNotification ()
  52. {
  53. # Quit here, if Notifications are not to be shown and they are not forced
  54. if [ "${showNotifications}" == "FALSE" ] && [ "$6" != "forced" ]
  55. then
  56. return
  57. fi
  58. message="$1"
  59. if [ "$2" = "" ]
  60. then
  61. color="yelloW"
  62. else
  63. color="$2"
  64. fi
  65. if [ "$3" = "" ]
  66. then
  67. timeout="10000"
  68. else
  69. timeout="$3"
  70. fi
  71. if [ "$4" = "" ]
  72. then
  73. posx="10"
  74. else
  75. posx="$4"
  76. fi
  77. if [ "$5" = "" ]
  78. then
  79. posy="10"
  80. else
  81. posy="$5"
  82. fi
  83. # create PNG using IMAGEMAGICK
  84. convert -size 1500x32 xc:"rgba(0,0,0,0)" -type truecolormatte -gravity NorthWest \
  85. -pointsize 32 -font FreeMono -style italic \
  86. -fill ${color} -draw "text 0,0 '${message}'" \
  87. PNG32:- > ~/scripts/rclone_script/rclone_script-notification.png
  88. killOtherNotification
  89. # show PNG using PNGVIEW
  90. nohup pngview -b 0 -l 10000 ~/scripts/rclone_script/rclone_script-notification.png -x ${posx} -y ${posy} -t ${timeout} &>/dev/null &
  91. }
  92. function getROMFileName ()
  93. {
  94. rompath="${rom%/*}" # directory containing $rom
  95. romfilename="${rom##*/}" # filename of $rom, including extension
  96. romfilebase="${romfilename%%.*}" # filename of $rom, excluding extension
  97. romfileext="${romfilename#*.}" # extension of $rom
  98. }
  99. function prepareFilter ()
  100. {
  101. filter="${romfilebase//\[/\\[}"
  102. filter="${filter//\]/\\]}"
  103. }
  104. function getTypeOfRemote ()
  105. {
  106. # list all remotes and their type
  107. remotes=$(rclone listremotes -l)
  108. # get line wiht RETROPIE remote
  109. retval=$(grep -i "^retropie:" <<< ${remotes})
  110. remoteType="${retval#*:}"
  111. remoteType=$(echo ${remoteType} | xargs)
  112. }
  113. ##################
  114. # SYNC FUNCTIONS #
  115. ##################
  116. function downloadSaves ()
  117. {
  118. if [ "${syncOnStartStop}" == "FALSE" ]
  119. then
  120. showNotification "!!! Synchronization is currently disabled !!!" "red" "" "" "" "forced"
  121. return
  122. fi
  123. log "INFO" "Started ${romfilename} (${system})"
  124. log "INFO" "Downloading saves and states from ${remoteType}..."
  125. showNotification "Downloading saves and states from ${remoteType}..."
  126. # test for remote files
  127. remotefiles=$(rclone lsf retropie:${remotebasedir}/${system} --include "${filter}.*")
  128. retval=$?
  129. if [ "${retval}" = "0" ]
  130. then # no error with RCLONE
  131. if [ "${remotefiles}" = "" ]
  132. then # no remote files found
  133. log "INFO" "No remote files found"
  134. showNotification "Downloading saves and states from ${remoteType}... No remote saves found"
  135. else # remote files found
  136. log "INFO" "Found remote files"
  137. # download saves and states to corresponding ROM
  138. rclone copy retropie:${remotebasedir}/${system} ~/RetroPie/saves/${system} --include "${filter}.*" --update >> ${logfile}
  139. retval=$?
  140. if [ "${retval}" = "0" ]
  141. then
  142. log "INFO" "Done"
  143. showNotification "Downloading saves and states from ${remoteType}... Done" "green"
  144. else
  145. log "ERROR" "Saves could not be downloaded"
  146. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red" "" "" "" "forced"
  147. fi
  148. fi
  149. else # error with RCLONE
  150. log "ERROR" "Saves could not be downloaded"
  151. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red" "" "" "" "forced"
  152. fi
  153. }
  154. function uploadSaves ()
  155. {
  156. if [ "${syncOnStartStop}" == "FALSE" ]
  157. then
  158. showNotification "!!! Synchronization is currently disabled !!!" "red" "" "" "" "forced"
  159. return
  160. fi
  161. log "INFO" "Stopped ${romfilename} (${system})"
  162. log "INFO" "Uploading saves and states to ${remoteType}..."
  163. showNotification "Uploading saves and states to ${remoteType}..."
  164. localfiles=$(find ~/RetroPie/saves/${system} -type f -iname "${filter}.*")
  165. if [ "${localfiles}" = "" ]
  166. then # no local files found
  167. log "INFO" "No local saves found"
  168. showNotification "Uploading saves and states to ${remoteType}... No local saves found"
  169. else # local files found
  170. # upload saves and states to corresponding ROM
  171. rclone copy ~/RetroPie/saves/${system} retropie:${remotebasedir}/${system} --include "${filter}.*" --update >> ${logfile}
  172. retval=$?
  173. if [ "${retval}" = "0" ]
  174. then
  175. log "INFO" "Done"
  176. showNotification "Uploading saves and states to ${remoteType}... Done" "green"
  177. else
  178. log "ERROR" "Saves could not be uploaded"
  179. showNotification "Uploading saves and states to ${remoteType}... ERROR" "red" "" "" "" "forced"
  180. fi
  181. fi
  182. }
  183. ########
  184. # MAIN #
  185. ########
  186. if [ "${debug}" = "1" ]; then debug; fi
  187. if [ "${direction}" == "up" ] && [ "${system}" != "kodi" ]
  188. then
  189. getROMFileName
  190. prepareFilter
  191. getTypeOfRemote
  192. uploadSaves
  193. fi
  194. if [ "${direction}" == "down" ] && [ "${system}" != "kodi" ]
  195. then
  196. getROMFileName
  197. prepareFilter
  198. getTypeOfRemote
  199. downloadSaves
  200. fi