1
0

rclone_script.sh 10 KB

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