rclone_script.sh 10 KB

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