1
0

rclone_script.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. logLevel=2
  13. # parameters
  14. direction="$1"
  15. system="$2"
  16. emulator="$3"
  17. rom="$4"
  18. command="$5"
  19. ####################
  20. # HELPER FUNCTIONS #
  21. ####################
  22. function log ()
  23. # Prints messages of different severeties to a logfile
  24. # Each message will look something like this:
  25. # <TIMESTAMP> <SEVERITY> <CALLING_FUNCTION> <MESSAGE>
  26. # needs a set variable $logLevel
  27. # -1 > No logging at all
  28. # 0 > prints ERRORS only
  29. # 1 > prints ERRORS and WARNINGS
  30. # 2 > prints ERRORS, WARNINGS and INFO
  31. # 3 > prints ERRORS, WARNINGS, INFO and DEBUGGING
  32. # needs a set variable $log pointing to a file
  33. # Usage
  34. # log 0 "This is an ERROR Message"
  35. # log 1 "This is a WARNING"
  36. # log 2 "This is just an INFO"
  37. # log 3 "This is a DEBUG message"
  38. {
  39. severity=$1
  40. message=$2
  41. if (( ${severity} <= ${logLevel} ))
  42. then
  43. case ${severity} in
  44. 0) level="ERROR" ;;
  45. 1) level="WARNING" ;;
  46. 2) level="INFO" ;;
  47. 3) level="DEBUG" ;;
  48. esac
  49. printf "$(date +%FT%T%:z):\t${level}\t${0##*/}\t${FUNCNAME[1]}\t${message}\n" >> ${logfile}
  50. fi
  51. }
  52. function killOtherNotification ()
  53. {
  54. # get PID of other PNGVIEW process
  55. otherPID=$(pgrep --full pngview)
  56. if [ "${debug}" = "1" ]; then log 3 "Other PIDs: ${otherPID}"; fi
  57. if [ "${otherPID}" != "" ]
  58. then
  59. if [ "${debug}" = "1" ]; then log 3 "Kill other PNGVIEW ${otherPID}"; fi
  60. kill ${otherPID}
  61. fi
  62. }
  63. function showNotification ()
  64. {
  65. # Quit here, if Notifications are not to be shown and they are not forced
  66. if [ "${showNotifications}" == "FALSE" ] && [ "$6" != "forced" ]
  67. then
  68. return
  69. fi
  70. message="$1"
  71. if [ "$2" = "" ]
  72. then
  73. color="yelloW"
  74. else
  75. color="$2"
  76. fi
  77. if [ "$3" = "" ]
  78. then
  79. timeout="10000"
  80. else
  81. timeout="$3"
  82. fi
  83. if [ "$4" = "" ]
  84. then
  85. posx="10"
  86. else
  87. posx="$4"
  88. fi
  89. if [ "$5" = "" ]
  90. then
  91. posy="10"
  92. else
  93. posy="$5"
  94. fi
  95. # create PNG using IMAGEMAGICK
  96. convert -size 1500x32 xc:"rgba(0,0,0,0)" -type truecolormatte -gravity NorthWest \
  97. -pointsize 32 -font FreeMono -style italic \
  98. -fill ${color} -draw "text 0,0 '${message}'" \
  99. PNG32:- > ~/scripts/rclone_script/rclone_script-notification.png
  100. killOtherNotification
  101. # show PNG using PNGVIEW
  102. nohup pngview -b 0 -l 10000 ~/scripts/rclone_script/rclone_script-notification.png -x ${posx} -y ${posy} -t ${timeout} &>/dev/null &
  103. }
  104. function getROMFileName ()
  105. {
  106. rompath="${rom%/*}" # directory containing $rom
  107. romfilename="${rom##*/}" # filename of $rom, including extension
  108. romfilebase="${romfilename%%.*}" # filename of $rom, excluding extension
  109. romfileext="${romfilename#*.}" # extension of $rom
  110. }
  111. function prepareFilter ()
  112. {
  113. filter="${romfilebase//\[/\\[}"
  114. filter="${filter//\]/\\]}"
  115. }
  116. function getTypeOfRemote ()
  117. {
  118. # list all remotes and their type
  119. remotes=$(rclone listremotes --long)
  120. # get line with RETROPIE remote
  121. retval=$(grep -i "^retropie:" <<< ${remotes})
  122. remoteType="${retval#*:}"
  123. remoteType=$(echo ${remoteType} | xargs)
  124. }
  125. function getAvailableConnection ()
  126. # checks if the device is connected to a LAN / WLAN and the Internet
  127. # RETURN
  128. # 0 > device seems to be connected to the Internet
  129. # 1 > device seems to be connected to a LAN / WLAN without internet access
  130. # 2 > device doesn't seem to be connected at all
  131. {
  132. gatewayIP=$(ip r | grep default | cut -d " " -f 3)
  133. if [ "${gatewayIP}" == "" ]
  134. then
  135. log 2 "Gateway could not be detected"
  136. return 2
  137. else
  138. log 2 "Gateway IP: ${gatewayIP}"
  139. fi
  140. ping -q -w 1 -c 1 ${gatewayIP} > /dev/null
  141. if [[ $? -eq 0 ]]
  142. then
  143. log 2 "Gateway PING successful"
  144. else
  145. log 2 "Gateway could not be PINGed"
  146. return 2
  147. fi
  148. ping -q -w 1 -c 1 "8.8.8.8" > /dev/null
  149. if [[ $? -eq 0 ]]
  150. then
  151. log 2 "8.8.8.8 PING successful"
  152. return 0
  153. else
  154. log 2 "8.8.8.8 could not be PINGed"
  155. return 1
  156. fi
  157. }
  158. ##################
  159. # SYNC FUNCTIONS #
  160. ##################
  161. function downloadSaves ()
  162. {
  163. if [ "${syncOnStartStop}" == "FALSE" ]
  164. then
  165. showNotification "!!! Synchronization is currently disabled !!!" "red" "" "" "" "forced"
  166. return
  167. fi
  168. log 2 "Started ${system}/${romfilename} "
  169. log 2 "Downloading saves and states for ${system}/${romfilename} from ${remoteType}..."
  170. showNotification "Downloading saves and states from ${remoteType}..."
  171. getAvailableConnection
  172. availableConnection=$?
  173. if [[ ${availableConnection} -gt ${neededConnection} ]]
  174. then
  175. log 0 "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
  176. case ${neededConnection} in
  177. 0) showNotification "Downloading saves and states from ${remoteType}... No Internet connection available" "red" "" "" "" "forced" ;;
  178. 1) showNotification "Downloading saves and states from ${remoteType}... No LAN / WLAN connection available" "red" "" "" "" "forced" ;;
  179. esac
  180. return
  181. fi
  182. # test for remote files
  183. remotefiles=$(rclone lsf retropie:${remotebasedir}/${system} --include "${filter}.*")
  184. retval=$?
  185. if [ "${retval}" = "0" ]
  186. then # no error with RCLONE
  187. if [ "${remotefiles}" = "" ]
  188. then # no remote files found
  189. log 2 "No remote files found"
  190. showNotification "Downloading saves and states from ${remoteType}... No remote files found"
  191. else # remote files found
  192. log 2 "Found remote files"
  193. # download saves and states to corresponding ROM
  194. rclone copy retropie:${remotebasedir}/${system} ~/RetroPie/saves/${system} --include "${filter}.*" --update >> ${logfile}
  195. retval=$?
  196. if [ "${retval}" = "0" ]
  197. then
  198. log 2 "Done"
  199. showNotification "Downloading saves and states from ${remoteType}... Done" "green"
  200. else
  201. log 2 "Saves and states could not be downloaded"
  202. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red" "" "" "" "forced"
  203. fi
  204. fi
  205. else # error with RCLONE
  206. log 0 "Saves and states could not be downloaded"
  207. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red" "" "" "" "forced"
  208. fi
  209. }
  210. function uploadSaves ()
  211. {
  212. if [ "${syncOnStartStop}" == "FALSE" ]
  213. then
  214. showNotification "!!! Synchronization is currently disabled !!!" "red" "" "" "" "forced"
  215. return
  216. fi
  217. log 2 "Stopped ${system}/${romfilename} "
  218. log 2 "Uploading saves and states for ${system}/${romfilename} to ${remoteType}..."
  219. showNotification "Uploading saves and states to ${remoteType}..."
  220. getAvailableConnection
  221. availableConnection=$?
  222. if [[ ${availableConnection} -gt ${neededConnection} ]]
  223. then
  224. log 0 "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
  225. case ${neededConnection} in
  226. 0) showNotification "Uploading saves and states to ${remoteType}... No Internet connection available" "red" "" "" "" "forced" ;;
  227. 1) showNotification "Uploading saves and states to ${remoteType}... No LAN / WLAN connection available" "red" "" "" "" "forced" ;;
  228. esac
  229. return
  230. fi
  231. localfiles=$(find ~/RetroPie/saves/${system} -type f -iname "${filter}.*")
  232. if [ "${localfiles}" = "" ]
  233. then # no local files found
  234. log 2 "No local saves and states found"
  235. showNotification "Uploading saves and states to ${remoteType}... No local files found"
  236. else # local files found
  237. # upload saves and states to corresponding ROM
  238. rclone copy ~/RetroPie/saves/${system} retropie:${remotebasedir}/${system} --include "${filter}.*" --update >> ${logfile}
  239. retval=$?
  240. if [ "${retval}" = "0" ]
  241. then
  242. log 2 "Done"
  243. showNotification "Uploading saves and states to ${remoteType}... Done" "green"
  244. else
  245. log 2 "saves and states could not be uploaded"
  246. showNotification "Uploading saves and states to ${remoteType}... ERROR" "red" "" "" "" "forced"
  247. fi
  248. fi
  249. }
  250. function deleteFileFromRemote ()
  251. # deletes a file from the remote
  252. # INPUT
  253. # $1 > relative filepath incl. name and extension to the local savepath
  254. # RETURN
  255. # 0 > file deteted successfully
  256. # 1 > connection not available
  257. # 2 > file could not be deleted
  258. {
  259. fileToDelete="$1"
  260. log 2 "File to delete: retropie:${remotebasedir}/${fileToDelete}"
  261. getAvailableConnection
  262. availableConnection=$?
  263. if [[ ${availableConnection} -gt ${neededConnection} ]]
  264. then
  265. log 0 "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
  266. return 1
  267. fi
  268. rclone delete "retropie:${remotebasedir}/${fileToDelete}" 2>&1 >> ${logfile}
  269. if [[ $? -eq 0 ]]
  270. then
  271. log 2 "File deleted successfully"
  272. return 0
  273. else
  274. log 0 "File could not be deleted. Error Code $?"
  275. return 1
  276. fi
  277. }
  278. ########
  279. # MAIN #
  280. ########
  281. #if [ "${debug}" = "1" ]; then debug; fi
  282. log 3 "direction: ${direction}"
  283. log 3 "system: ${system}"
  284. log 3 "emulator: ${emulator}"
  285. log 3 "rom: ${rom}"
  286. log 3 "command: ${command}"
  287. log 3 "remotebasedir: ${remotebasedir}"
  288. log 3 "rompath: ${rompath}"
  289. log 3 "romfilename: ${romfilename}"
  290. log 3 "romfilebase: ${romfilebase}"
  291. log 3 "romfileext: ${romfileext}"
  292. if [ "${direction}" == "up" ] && [ "${system}" != "kodi" ]
  293. then
  294. getROMFileName
  295. prepareFilter
  296. getTypeOfRemote
  297. uploadSaves
  298. fi
  299. if [ "${direction}" == "down" ] && [ "${system}" != "kodi" ]
  300. then
  301. getROMFileName
  302. prepareFilter
  303. getTypeOfRemote
  304. downloadSaves
  305. fi
  306. if [ "${direction}" == "delete" ]
  307. then
  308. deleteFileFromRemote "${2}"
  309. fi