rclone_script.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. function getAvailableConnection ()
  114. # checks if the device is connected to a LAN / WLAN and the Internet
  115. # RETURN
  116. # 0 > device seems to be connected to the Internet
  117. # 1 > device seems to be connected to a LAN / WLAN without internet access
  118. # 2 > device doesn't seem to be connected at all
  119. {
  120. gatewayIP=$(ip r | grep default | cut -d " " -f 3)
  121. if [ "${gatewayIP}" == "" ]
  122. then
  123. log "INFO" "Gateway could not be detected"
  124. return 2
  125. else
  126. log "INFO" "Gateway IP: ${gatewayIP}"
  127. fi
  128. ping -q -w 1 -c 1 ${gatewayIP} > /dev/null
  129. if [[ $? -eq 0 ]]
  130. then
  131. log "INFO" "Gateway PING successful"
  132. else
  133. log "INFO" "Gateway could not be PINGed"
  134. return 2
  135. fi
  136. ping -q -w 1 -c 1 "www.google.com" > /dev/null
  137. if [[ $? -eq 0 ]]
  138. then
  139. log "INFO" "www.google.com PING successful"
  140. return 0
  141. else
  142. log "INFO" "www.google.com could not be PINGed"
  143. return 1
  144. fi
  145. }
  146. ##################
  147. # SYNC FUNCTIONS #
  148. ##################
  149. function downloadSaves ()
  150. {
  151. if [ "${syncOnStartStop}" == "FALSE" ]
  152. then
  153. showNotification "!!! Synchronization is currently disabled !!!" "red" "" "" "" "forced"
  154. return
  155. fi
  156. log "INFO" "Started ${system}/${romfilename} "
  157. log "INFO" "Downloading saves and states for ${system}/${romfilename} from ${remoteType}..."
  158. showNotification "Downloading saves and states from ${remoteType}..."
  159. getAvailableConnection
  160. availableConnection=$?
  161. if [[ ${availableConnection} -gt ${neededConnection} ]]
  162. then
  163. log "ERROR" "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
  164. case ${neededConnection} in
  165. 0) showNotification "Downloading saves and states from ${remoteType}... No Internet connection available" "red" "" "" "" "forced" ;;
  166. 1) showNotification "Downloading saves and states from ${remoteType}... No LAN / WLAN connection available" "red" "" "" "" "forced" ;;
  167. esac
  168. return
  169. fi
  170. # test for remote files
  171. remotefiles=$(rclone lsf retropie:${remotebasedir}/${system} --include "${filter}.*")
  172. retval=$?
  173. if [ "${retval}" = "0" ]
  174. then # no error with RCLONE
  175. if [ "${remotefiles}" = "" ]
  176. then # no remote files found
  177. log "INFO" "No remote files found"
  178. showNotification "Downloading saves and states from ${remoteType}... No remote files found"
  179. else # remote files found
  180. log "INFO" "Found remote files"
  181. # download saves and states to corresponding ROM
  182. rclone copy retropie:${remotebasedir}/${system} ~/RetroPie/saves/${system} --include "${filter}.*" --update >> ${logfile}
  183. retval=$?
  184. if [ "${retval}" = "0" ]
  185. then
  186. log "INFO" "Done"
  187. showNotification "Downloading saves and states from ${remoteType}... Done" "green"
  188. else
  189. log "ERROR" "Saves and states could not be downloaded"
  190. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red" "" "" "" "forced"
  191. fi
  192. fi
  193. else # error with RCLONE
  194. log "ERROR" "Saves and states could not be downloaded"
  195. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red" "" "" "" "forced"
  196. fi
  197. }
  198. function uploadSaves ()
  199. {
  200. if [ "${syncOnStartStop}" == "FALSE" ]
  201. then
  202. showNotification "!!! Synchronization is currently disabled !!!" "red" "" "" "" "forced"
  203. return
  204. fi
  205. log "INFO" "Stopped ${system}/${romfilename} "
  206. log "INFO" "Uploading saves and states for ${system}/${romfilename} to ${remoteType}..."
  207. showNotification "Uploading saves and states to ${remoteType}..."
  208. getAvailableConnection
  209. availableConnection=$?
  210. if [[ ${availableConnection} -gt ${neededConnection} ]]
  211. then
  212. log "ERROR" "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
  213. case ${neededConnection} in
  214. 0) showNotification "Uploading saves and states to ${remoteType}... No Internet connection available" "red" "" "" "" "forced" ;;
  215. 1) showNotification "Uploading saves and states to ${remoteType}... No LAN / WLAN connection available" "red" "" "" "" "forced" ;;
  216. esac
  217. return
  218. fi
  219. localfiles=$(find ~/RetroPie/saves/${system} -type f -iname "${filter}.*")
  220. if [ "${localfiles}" = "" ]
  221. then # no local files found
  222. log "INFO" "No local saves and states found"
  223. showNotification "Uploading saves and states to ${remoteType}... No local files found"
  224. else # local files found
  225. # upload saves and states to corresponding ROM
  226. rclone copy ~/RetroPie/saves/${system} retropie:${remotebasedir}/${system} --include "${filter}.*" --update >> ${logfile}
  227. retval=$?
  228. if [ "${retval}" = "0" ]
  229. then
  230. log "INFO" "Done"
  231. showNotification "Uploading saves and states to ${remoteType}... Done" "green"
  232. else
  233. log "ERROR" "saves and states could not be uploaded"
  234. showNotification "Uploading saves and states to ${remoteType}... ERROR" "red" "" "" "" "forced"
  235. fi
  236. fi
  237. }
  238. ########
  239. # MAIN #
  240. ########
  241. if [ "${debug}" = "1" ]; then debug; fi
  242. if [ "${direction}" == "up" ] && [ "${system}" != "kodi" ]
  243. then
  244. getROMFileName
  245. prepareFilter
  246. getTypeOfRemote
  247. uploadSaves
  248. fi
  249. if [ "${direction}" == "down" ] && [ "${system}" != "kodi" ]
  250. then
  251. getROMFileName
  252. prepareFilter
  253. getTypeOfRemote
  254. downloadSaves
  255. fi