1
0

rclone_script.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. # 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. # Builds patterns compatible with find and rclone
  114. function prepareSaveFilters ()
  115. {
  116. # Read in any extensions
  117. extensions=$(xmlstarlet sel -t -m "emulators/emulator[name='${emulator}']/saveFileExtensions" -v "ext" "${emu_settings}")
  118. # If no extensions were defined
  119. if [ -z "${extensions// }" ]
  120. then
  121. log 3 "Using default save file filter for emulator: ${emulator}"
  122. # Default to "<ROM_name>.*"
  123. localFilter="${romfilebase//\[/\\[}"
  124. localFilter="${localFilter//\]/\\]}.*"
  125. remoteFilter="${localFilter}"
  126. else
  127. # Otherwise, build custom filters
  128. log 2 "Custom save extentions defined for emulator: ${emulator}"
  129. i=0
  130. # Build the filters for the extensions
  131. while read ext; do
  132. if [ "${i}" -eq "0" ]
  133. then
  134. remoteFilter="{*.${ext}"
  135. localFilter+=("-iname" "*.${ext}")
  136. ((i++))
  137. else
  138. remoteFilter="${remoteFilter},*.${ext}"
  139. localFilter+=("-o" "-iname" "*.${ext}")
  140. fi
  141. done <<< ${extensions}
  142. remoteFilter="${remoteFilter}}"
  143. fi
  144. }
  145. function getTypeOfRemote ()
  146. {
  147. # list all remotes and their type
  148. remotes=$(rclone listremotes --long)
  149. # get line with RETROPIE remote
  150. retval=$(grep -i "^retropie:" <<< ${remotes})
  151. remoteType="${retval#*:}"
  152. remoteType=$(echo ${remoteType} | xargs)
  153. }
  154. function getAvailableConnection ()
  155. # checks if the device is connected to a LAN / WLAN and the Internet
  156. # RETURN
  157. # 0 > device seems to be connected to the Internet
  158. # 1 > device seems to be connected to a LAN / WLAN without internet access
  159. # 2 > device doesn't seem to be connected at all
  160. {
  161. gatewayIP=$(ip r | grep default | cut -d " " -f 3)
  162. if [ "${gatewayIP}" == "" ]
  163. then
  164. log 2 "Gateway could not be detected"
  165. return 2
  166. else
  167. log 2 "Gateway IP: ${gatewayIP}"
  168. fi
  169. ping -q -w 1 -c 1 ${gatewayIP} > /dev/null
  170. if [[ $? -eq 0 ]]
  171. then
  172. log 2 "Gateway PING successful"
  173. else
  174. log 2 "Gateway could not be PINGed"
  175. return 2
  176. fi
  177. ping -q -w 1 -c 1 "8.8.8.8" > /dev/null
  178. if [[ $? -eq 0 ]]
  179. then
  180. log 2 "8.8.8.8 PING successful"
  181. return 0
  182. else
  183. log 2 "8.8.8.8 could not be PINGed"
  184. return 1
  185. fi
  186. }
  187. ##################
  188. # SYNC FUNCTIONS #
  189. ##################
  190. function downloadSaves ()
  191. {
  192. if [ "${syncOnStartStop}" == "FALSE" ]
  193. then
  194. showNotification "!!! Synchronization is currently disabled !!!" "red" "" "" "" "forced"
  195. return
  196. fi
  197. log 2 "Started ${system}/${romfilename} "
  198. log 2 "Downloading saves and states for ${system}/${romfilename} from ${remoteType}..."
  199. showNotification "Downloading saves and states from ${remoteType}..."
  200. getAvailableConnection
  201. availableConnection=$?
  202. if [[ ${availableConnection} -gt ${neededConnection} ]]
  203. then
  204. log 0 "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
  205. case ${neededConnection} in
  206. 0) showNotification "Downloading saves and states from ${remoteType}... No Internet connection available" "red" "" "" "" "forced" ;;
  207. 1) showNotification "Downloading saves and states from ${remoteType}... No LAN / WLAN connection available" "red" "" "" "" "forced" ;;
  208. esac
  209. return
  210. fi
  211. # test for remote files
  212. remotefiles=$(rclone lsf retropie:${remotebasedir}/${system} --include "${remoteFilter}")
  213. retval=$?
  214. if [ "${retval}" = "0" ]
  215. then # no error with RCLONE
  216. if [ "${remotefiles}" = "" ]
  217. then # no remote files found
  218. log 2 "No remote files found"
  219. showNotification "Downloading saves and states from ${remoteType}... No remote files found"
  220. else # remote files found
  221. log 2 "Found remote files"
  222. # download saves and states to corresponding ROM
  223. rclone copy retropie:${remotebasedir}/${system} ~/RetroPie/saves/${system} --include "${remoteFilter}" --update >> ${logfile}
  224. retval=$?
  225. if [ "${retval}" = "0" ]
  226. then
  227. log 2 "Done"
  228. showNotification "Downloading saves and states from ${remoteType}... Done" "green"
  229. else
  230. log 2 "Saves and states could not be downloaded"
  231. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red" "" "" "" "forced"
  232. fi
  233. fi
  234. else # error with RCLONE
  235. log 0 "Saves and states could not be downloaded"
  236. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red" "" "" "" "forced"
  237. fi
  238. }
  239. function uploadSaves ()
  240. {
  241. if [ "${syncOnStartStop}" == "FALSE" ]
  242. then
  243. showNotification "!!! Synchronization is currently disabled !!!" "red" "" "" "" "forced"
  244. return
  245. fi
  246. log 2 "Stopped ${system}/${romfilename} "
  247. log 2 "Uploading saves and states for ${system}/${romfilename} to ${remoteType}..."
  248. showNotification "Uploading saves and states to ${remoteType}..."
  249. getAvailableConnection
  250. availableConnection=$?
  251. if [[ ${availableConnection} -gt ${neededConnection} ]]
  252. then
  253. log 0 "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
  254. case ${neededConnection} in
  255. 0) showNotification "Uploading saves and states to ${remoteType}... No Internet connection available" "red" "" "" "" "forced" ;;
  256. 1) showNotification "Uploading saves and states to ${remoteType}... No LAN / WLAN connection available" "red" "" "" "" "forced" ;;
  257. esac
  258. return
  259. fi
  260. localfiles=$(find ~/RetroPie/saves/${system} -type f "${localFilter[@]}")
  261. if [ "${localfiles}" = "" ]
  262. then # no local files found
  263. log 2 "No local saves and states found"
  264. showNotification "Uploading saves and states to ${remoteType}... No local files found"
  265. else # local files found
  266. # upload saves and states to corresponding ROM
  267. rclone copy ~/RetroPie/saves/${system} retropie:${remotebasedir}/${system} --include "${remoteFilter}" --update >> ${logfile}
  268. retval=$?
  269. if [ "${retval}" = "0" ]
  270. then
  271. log 2 "Done"
  272. showNotification "Uploading saves and states to ${remoteType}... Done" "green"
  273. else
  274. log 2 "saves and states could not be uploaded"
  275. showNotification "Uploading saves and states to ${remoteType}... ERROR" "red" "" "" "" "forced"
  276. fi
  277. fi
  278. }
  279. function deleteFileFromRemote ()
  280. # deletes a file from the remote
  281. # INPUT
  282. # $1 > relative filepath incl. name and extension to the local savepath
  283. # RETURN
  284. # 0 > file deteted successfully
  285. # 1 > connection not available
  286. # 2 > file could not be deleted
  287. {
  288. fileToDelete="$1"
  289. log 2 "File to delete: retropie:${remotebasedir}/${fileToDelete}"
  290. getAvailableConnection
  291. availableConnection=$?
  292. if [[ ${availableConnection} -gt ${neededConnection} ]]
  293. then
  294. log 0 "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
  295. return 1
  296. fi
  297. rclone delete "retropie:${remotebasedir}/${fileToDelete}" 2>&1 >> ${logfile}
  298. if [[ $? -eq 0 ]]
  299. then
  300. log 2 "File deleted successfully"
  301. return 0
  302. else
  303. log 0 "File could not be deleted. Error Code $?"
  304. return 1
  305. fi
  306. }
  307. ########
  308. # MAIN #
  309. ########
  310. #if [ "${debug}" = "1" ]; then debug; fi
  311. log 3 "direction: ${direction}"
  312. log 3 "system: ${system}"
  313. log 3 "emulator: ${emulator}"
  314. log 3 "rom: ${rom}"
  315. log 3 "command: ${command}"
  316. log 3 "remotebasedir: ${remotebasedir}"
  317. log 3 "rompath: ${rompath}"
  318. log 3 "romfilename: ${romfilename}"
  319. log 3 "romfilebase: ${romfilebase}"
  320. log 3 "romfileext: ${romfileext}"
  321. if [ "${direction}" == "up" ] && [ "${system}" != "kodi" ]
  322. then
  323. getROMFileName
  324. prepareSaveFilters
  325. getTypeOfRemote
  326. uploadSaves
  327. fi
  328. if [ "${direction}" == "down" ] && [ "${system}" != "kodi" ]
  329. then
  330. getROMFileName
  331. prepareSaveFilters
  332. getTypeOfRemote
  333. downloadSaves
  334. fi
  335. if [ "${direction}" == "delete" ]
  336. then
  337. deleteFileFromRemote "${2}"
  338. fi