1
0

rclone_script.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/bin/bash
  2. # include settings file
  3. source ~/scripts/rclone_script.ini
  4. # parameters
  5. direction="$1"
  6. system="$2"
  7. emulator="$3"
  8. rom="$4"
  9. command="$5"
  10. log ()
  11. {
  12. severity=$1
  13. message=$2
  14. printf "$(date +%FT%T%:z):\t${severity}:\t${message}\n" >> ${logfile}
  15. }
  16. debug ()
  17. {
  18. log "DEBUG" "direction: ${direction}"
  19. log "DEBUG" "system: ${system}"
  20. log "DEBUG" "emulator: ${emulator}"
  21. log "DEBUG" "rom: ${rom}"
  22. log "DEBUG" "command: ${command}"
  23. log "DEBUG" "remotebasedir: ${remotebasedir}"
  24. log "DEBUG" "rompath: ${rompath}"
  25. log "DEBUG" "romfilename: ${romfilename}"
  26. log "DEBUG" "romfilebase: ${romfilebase}"
  27. log "DEBUG" "romfileext: ${romfileext}"
  28. log "DEBUG" ""
  29. }
  30. killOtherNotification ()
  31. {
  32. # get PID of other PNGVIEW process
  33. otherPID=$(pgrep --full pngview)
  34. if [ "${debug}" = "1" ]; then log "DEBUG" "Other PIDs: ${otherPID}"; fi
  35. if [ "${otherPID}" != "" ]
  36. then
  37. if [ "${debug}" = "1" ]; then log "DEBUG" "Kill other PNGVIEW ${otherPID}"; fi
  38. kill ${otherPID}
  39. fi
  40. }
  41. showNotification ()
  42. {
  43. message="$1"
  44. if [ "$2" = "" ]
  45. then
  46. color="yelloW"
  47. else
  48. color="$2"
  49. fi
  50. if [ "$3" = "" ]
  51. then
  52. timeout="10000"
  53. else
  54. timeout="$3"
  55. fi
  56. if [ "$4" = "" ]
  57. then
  58. posx="10"
  59. else
  60. posx="$4"
  61. fi
  62. if [ "$5" = "" ]
  63. then
  64. posy="10"
  65. else
  66. posy="$5"
  67. fi
  68. # create PNG using IMAGEMAGICK
  69. convert -size 1500x32 xc:"rgba(0,0,0,0)" -type truecolormatte -gravity NorthWest \
  70. -pointsize 32 -font FreeMono -style italic \
  71. -fill ${color} -draw "text 0,0 '${message}'" \
  72. PNG32:- > ~/scripts/rclone_script-notification.png
  73. killOtherNotification
  74. # show PNG using PNGVIEW
  75. nohup pngview -b 0 -l 10000 ~/scripts/rclone_script-notification.png -x ${posx} -y ${posy} -t ${timeout} &>/dev/null &
  76. }
  77. getROMFileName ()
  78. {
  79. rompath="${rom%/*}" # directory containing $rom
  80. romfilename="${rom##*/}" # filename of $rom, including extension
  81. romfilebase="${romfilename%%.*}" # filename of $rom, excluding extension
  82. romfileext="${romfilename#*.}" # extension of $rom
  83. }
  84. prepareFilter ()
  85. {
  86. filter="${romfilebase//\[/\\[}"
  87. filter="${filter//\]/\\]}"
  88. }
  89. getTypeOfRemote ()
  90. {
  91. # list all remotes and their type
  92. remotes=$(rclone listremotes -l)
  93. # get line wiht RETROPIE remote
  94. retval=$(grep -i "^retropie:" <<< ${remotes})
  95. remoteType="${retval#*:}"
  96. remoteType=$(echo ${remoteType} | xargs)
  97. }
  98. downloadSaves ()
  99. {
  100. log "INFO" "Started ${romfilename} (${system})"
  101. log "INFO" "Downloading saves and states from ${remoteType}..."
  102. showNotification "Downloading saves and states from ${remoteType}..."
  103. # test for remote files
  104. remotefiles=$(rclone lsf retropie:${remotebasedir}/${system} --include "${filter}.*")
  105. retval=$?
  106. if [ "${retval}" = "0" ]
  107. then # no error with RCLONE
  108. if [ "${remotefiles}" = "" ]
  109. then # no remote files found
  110. log "INFO" "No remote files found"
  111. showNotification "Downloading saves and states from ${remoteType}... No remote saves found"
  112. else # remote files found
  113. log "INFO" "Found remote files"
  114. # download saves and states to corresponding ROM
  115. rclone copy retropie:${remotebasedir}/${system} ~/RetroPie/saves/${system} --include "${filter}.*" --update
  116. retval=$?
  117. if [ "${retval}" = "0" ]
  118. then
  119. log "INFO" "Done"
  120. showNotification "Downloading saves and states from ${remoteType}... Done" "green"
  121. else
  122. log "ERROR" "Saves could not be downloaded"
  123. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red"
  124. fi
  125. fi
  126. else # error with RCLONE
  127. log "ERROR" "Saves could not be downloaded"
  128. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red"
  129. fi
  130. }
  131. uploadSaves ()
  132. {
  133. log "INFO" "Stopped ${romfilename} (${system})"
  134. log "INFO" "Uploading saves and states to ${remoteType}..."
  135. showNotification "Uploading saves and states to ${remoteType}..."
  136. localfiles=$(find ~/RetroPie/saves/${system} -type f -iname "${filter}.*")
  137. if [ "${localfiles}" = "" ]
  138. then # no local files found
  139. log "INFO" "No local saves found"
  140. showNotification "Uploading saves and states to ${remoteType}... No local saves found"
  141. else # local files found
  142. # upload saves and states to corresponding ROM
  143. rclone copy ~/RetroPie/saves/${system} retropie:${remotebasedir}/${system} --include "${filter}.*" --update
  144. retval=$?
  145. if [ "${retval}" = "0" ]
  146. then
  147. log "INFO" "Done"
  148. showNotification "Uploading saves and states to ${remoteType}... Done" "green"
  149. else
  150. log "ERROR" "Saves could not be uploaded"
  151. showNotification "Uploading saves and states to ${remoteType}... ERROR" "red"
  152. fi
  153. fi
  154. }
  155. getROMFileName
  156. prepareFilter
  157. getTypeOfRemote
  158. if [ "${debug}" = "1" ]; then debug; fi
  159. if [ "${direction}" == "up" ]
  160. then
  161. uploadSaves
  162. fi
  163. if [ "${direction}" == "down" ]
  164. then
  165. downloadSaves
  166. fi