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