rclone_script.sh 10 KB

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