rclone_script-install.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. # global variables
  10. url="https://raw.githubusercontent.com/Jandalf81/rclone_script"
  11. branch="beta"
  12. remotebasedir = ""
  13. header ()
  14. {
  15. # clear screen
  16. clear
  17. printf "${UNDERLINE}Install script for cloud sync via RCLONE\n\n"
  18. if [ "${branch}" = "beta" ]
  19. then
  20. printf "${RED}!!! BETA !!!\n\n"
  21. fi
  22. }
  23. testRCLONE ()
  24. {
  25. # testing for RCLONE binary
  26. printf "${NORMAL}Testing for RCLONE binary... "
  27. if [ -f /usr/bin/rclone ]
  28. then
  29. printf "${GREEN}Found\n"
  30. else
  31. printf "${YELLOW}Not found\n"
  32. installRCLONE
  33. fi
  34. }
  35. installRCLONE ()
  36. {
  37. printf "${NORMAL}Installing RCLONE...\n"
  38. # download current RCLONE
  39. printf "${NORMAL} Getting current RCLONE... "
  40. wget -q -P ~ https://downloads.rclone.org/rclone-current-linux-arm.zip
  41. printf "${GREEN}Done\n"
  42. # unzip RCLONE into HOME
  43. printf "${NORMAL} Unzipping RCLONE... "
  44. unzip -q ~/rclone-current-linux-arm.zip -d ~
  45. printf "${GREEN}Done\n"
  46. # move RCLONE
  47. printf "${NORMAL} Moving RCLONE to /usr/bin... "
  48. { # try
  49. cd ~/rclone-v* &&
  50. retval=$(sudo mv rclone /usr/bin 2>&1) &&
  51. retval=$(sudo chown root:root /usr/bin/rclone 2>&1) &&
  52. retval=$(sudo chmod 755 /usr/bin/rclone 2>&1) &&
  53. printf "${GREEN}Done\n"
  54. } || { # catch
  55. printf "${RED}ERROR: ${retval}\n"
  56. removeRCLONETempFiles
  57. exit
  58. }
  59. cd ~
  60. removeRCLONETempFiles
  61. }
  62. removeRCLONETempFiles ()
  63. {
  64. # remove temporary files
  65. printf "${NORMAL} Removing temporary files... "
  66. { #try
  67. retval=$(rm ~/rclone-current-linux-arm.zip 2>&1) &&
  68. retval=$(rm ~/rclone-v* -r 2>&1) &&
  69. printf "${GREEN}Done\n"
  70. } || { #catch
  71. printf "${RED}ERROR: ${retval}\n"
  72. }
  73. }
  74. testRCLONEconfiguration ()
  75. {
  76. # test for RCLONEs config file
  77. printf "${NORMAL}Testing for RETROPIE remote within RCLONE... "
  78. # list all remotes and their type
  79. remotes=$(rclone listremotes)
  80. # get line wiht RETROPIE remote
  81. retval=$(grep -i "^retropie:" <<< ${remotes})
  82. if [ "${retval}" = "retropie:" ]
  83. then
  84. printf "${GREEN}Found\n"
  85. else
  86. printf "${YELLOW}Not found\n"
  87. createRCLONEconfiguration
  88. fi
  89. }
  90. createRCLONEconfiguration ()
  91. {
  92. printf "${NORMAL} Please create a new remote within RCLONE now.\n"
  93. printf "${NORMAL} Name that remote \"${RED}retropie${NORMAL}\".\n"
  94. printf "${NORMAL} Opening RCLONE CONFIG now...\n"
  95. printf "\n"
  96. rclone config
  97. printf "\n"
  98. printf "${NORMAL}Continuing installation now\n"
  99. printf "\n"
  100. testRCLONEconfiguration
  101. }
  102. testPNGVIEW ()
  103. {
  104. # testing for PNGVIEW binary
  105. printf "${NORMAL}Testing for PNGVIEW binary... "
  106. if [ -f /usr/bin/pngview ]
  107. then
  108. printf "${GREEN}Found\n"
  109. else
  110. printf "${YELLOW}Not found\n"
  111. installPNGVIEW
  112. fi
  113. }
  114. installPNGVIEW ()
  115. {
  116. printf "${NORMAL}Installing PNGVIEW...\n"
  117. # download PNGVIEW
  118. printf "${NORMAL} Getting current PNGVIEW... "
  119. wget -q -P ~ https://github.com/AndrewFromMelbourne/raspidmx/archive/master.zip
  120. printf "${GREEN}Done\n"
  121. # unzip PNGVIEW
  122. printf "${NORMAL} Unzipping PNGVIEW... "
  123. unzip -q ~/master.zip -d ~
  124. printf "${GREEN}Done\n"
  125. # compile PNGVIEW
  126. printf "${NORMAL} Compiling PNGVIEW (may take a while)... "
  127. cd ~/raspidmx-master
  128. make > /dev/null
  129. printf "${GREEN}Done\n"
  130. # move PNGVIEW
  131. printf "${NORMAL} Moving PNGVIEW to /usr/bin... "
  132. { # try
  133. retval=$(sudo mv ~/raspidmx-master/pngview/pngview /usr/bin 2>&1) &&
  134. retval=$(sudo mv ~/raspidmx-master/lib/libraspidmx.so.1 /usr/lib 2>&1) &&
  135. retval=$(sudo chown root:root /usr/bin/pngview 2>&1) &&
  136. retval=$(sudo chmod 755 /usr/bin/pngview 2>&1) &&
  137. printf "${GREEN}Done\n"
  138. } || { # catch
  139. printf "${RED}ERROR: ${retval}\n"
  140. removePNGVIEWTempFiles
  141. exit
  142. }
  143. cd ~
  144. removePNGVIEWTempFiles
  145. }
  146. removePNGVIEWTempFiles ()
  147. {
  148. # remove temporary files
  149. printf "${NORMAL} Removing temporary files... "
  150. { #try
  151. retval=$(rm ~/master.zip 2>&1) &&
  152. retval=$(sudo rm -r ~/raspidmx-master 2>&1) &&
  153. printf "${GREEN}Done\n"
  154. } || { #catch
  155. printf "${RED}ERROR: ${retval}\n"
  156. }
  157. }
  158. testIMAGEMAGICK ()
  159. {
  160. # testing for IMAGEMAGICK binary
  161. printf "${NORMAL}Testing for IMAGEMAGICK binary... "
  162. if [ -f /usr/bin/convert ]
  163. then
  164. printf "${GREEN}Found\n"
  165. else
  166. printf "${YELLOW}Not found\n"
  167. installIMAGEMAGICK
  168. fi
  169. }
  170. installIMAGEMAGICK ()
  171. {
  172. # install IMAGEMAGICK
  173. printf "${NORMAL}Installing IMAGEMAGICK (may take a while)... "
  174. { # try
  175. retval=$(sudo apt-get update 2>&1) &&
  176. retval=$(sudo apt-get --yes install imagemagick 2>&1) &&
  177. printf "${GREEN}Done\n"
  178. } || { # catch
  179. printf "${RED}ERROR: ${retval}\n"
  180. exit
  181. }
  182. }
  183. installRCLONE_SCRIPT ()
  184. {
  185. # install RCLONE_SCRIPT
  186. printf "${NORMAL}Installing RCLONE_SCRIPT...\n"
  187. # test directory for RCLONE_SCRIPT
  188. printf "${NORMAL} Testing directory for RCLONE_SCRIPT... "
  189. if [ -d ~/scripts ]
  190. then
  191. printf "${GREEN}Found\n"
  192. else
  193. printf "${YELLOW}Not found\n"
  194. printf "{NORMAL} Creating directory for RCLONE_SCRIPT... "
  195. mkdir ~/scripts
  196. printf "${GREEN}Done\n"
  197. fi
  198. # download script
  199. printf "${NORMAL} Getting RCLONE_SCRIPT... "
  200. { # try
  201. retval=$(wget -q -N -P ~/scripts ${url}/${branch}/rclone_script.sh 2>&1) &&
  202. retval=$(sudo chmod 755 ~/scripts/rclone_script.sh 2>&1) &&
  203. printf "${GREEN}Done\n"
  204. } || { # catch
  205. printf "${RED}ERROR: ${retval}\n"
  206. exit
  207. }
  208. # download RCLONE_SCRIPT-FULLSYNC script
  209. printf "${NORMAL} Getting RCLONE_SCRIPT-FULLSYNC... "
  210. { # try
  211. retval=$(wget -q -N -P ~/RetroPie/retropiemenu ${url}/${branch}/rclone_script-fullsync.sh 2>&1) &&
  212. retval=$(sudo chmod 755 ~/RetroPie/retropiemenu/rclone_script-fullsync.sh 2>&1) &&
  213. printf "${GREEN}Done\n"
  214. } || { # catch
  215. printf "${RED}ERROR: ${retval}\n"
  216. exit
  217. }
  218. # test for RCLONE_SCRIPT-FULLSYNC menu item
  219. printf "${NORMAL} Testing for RCLONE_SCRIPT-FULLSYNC menu item... "
  220. if grep -Fq "<path>./rclone_script-fullsync.sh</path>" ~/.emulationstation/gamelists/retropie/gamelist.xml
  221. then
  222. printf "${GREEN}Found\n"
  223. else
  224. printf "${YELLOW}Not found\n"
  225. # create menu item
  226. printf "${NORMAL} Creating menu item for RCLONE_SCRIPT-FULLSYNC... "
  227. menuitem="\t<game>\n"
  228. sed -i "/<\/gameList>/c\\\\t<game>\n\t\t<path>.\/rclone_script-fullsync.sh<\/path>\n\t\t<name>RCLONE_SCRIPT full sync<\/name>\n\t\t<desc>Starts a synchronization of all save files<\/desc>\n\t\t<image></image>\n\t<\/game>\n<\/gameList>" ~/.emulationstation/gamelists/retropie/gamelist.xml
  229. printf "${GREEN}Done\n"
  230. fi
  231. # download uninstall script
  232. printf "${NORMAL} Getting UNINSTALL script... "
  233. { # try
  234. retval=$(wget -q -N -P ~/scripts ${url}/${branch}/rclone_script-uninstall.sh 2>&1) &&
  235. retval=$(sudo chmod 755 ~/scripts/rclone_script-uninstall.sh 2>&1) &&
  236. printf "${GREEN}Done\n"
  237. } || { # catch
  238. printf "${RED}ERROR: ${retval}\n"
  239. exit
  240. }
  241. }
  242. testRUNCOMMAND ()
  243. {
  244. # test RUNCOMMAND-ONSTART
  245. printf "${NORMAL}Testing for RUNCOMMAND-ONSTART... "
  246. if [ -f /opt/retropie/configs/all/runcommand-onstart.sh ]
  247. then
  248. # file exists
  249. printf "${GREEN}Found\n"
  250. printf "${NORMAL} Testing RUNCOMMAND-ONSTART for call to RCLONE_SCRIPT... "
  251. # test call to RCLONE from RUNCOMMAND-ONSTART
  252. if grep -Fq "~/scripts/rclone_script.sh" /opt/retropie/configs/all/runcommand-onstart.sh
  253. then
  254. printf "${GREEN}Found\n"
  255. else
  256. printf "${YELLOW}Not found\n"
  257. printf "${NORMAL} Adding call to RCLONE_SCRIPT... "
  258. echo "~/scripts/rclone_script.sh \"down\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onstart.sh
  259. printf "${GREEN}Done\n"
  260. fi
  261. else
  262. # file does not exist
  263. printf "${YELLOW}Not found\n"
  264. printf "${NORMAL} Creating RUNCOMMAND-ONSTART... "
  265. echo "#!/bin/bash" > /opt/retropie/configs/all/runcommand-onstart.sh
  266. echo "~/scripts/rclone_script.sh \"down\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onstart.sh
  267. printf "${GREEN}Done\n"
  268. fi
  269. # test RUNCOMMAND-ONEND
  270. printf "${NORMAL}Testing for RUNCOMMAND-ONEND... "
  271. if [ -f /opt/retropie/configs/all/runcommand-onend.sh ]
  272. then
  273. # file exists
  274. printf "${GREEN}Found\n"
  275. printf "${NORMAL} Testing RUNCOMMAND-ONEND for call to RCLONE_SCRIPT... "
  276. # test call to RCLONE from RUNCOMMAND-ONEND
  277. if grep -Fq "~/scripts/rclone_script.sh" /opt/retropie/configs/all/runcommand-onend.sh
  278. then
  279. printf "${GREEN}Found\n"
  280. else
  281. printf "${YELLOW}Not found\n"
  282. printf "${NORMAL} Adding call to RCLONE_SCRIPT... "
  283. echo "~/scripts/rclone_script.sh \"up\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onend.sh
  284. printf "${GREEN}Done\n"
  285. fi
  286. else
  287. # file does not exist
  288. printf "${YELLOW}Not found\n"
  289. printf "${NORMAL} Creating RUNCOMMAND-ONSTART... "
  290. echo "#!/bin/bash" > /opt/retropie/configs/all/runcommand-onend.sh
  291. echo "~/scripts/rclone_script.sh \"up\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onend.sh
  292. printf "${GREEN}Done\n"
  293. fi
  294. }
  295. testLocalSaveDirectory ()
  296. {
  297. printf "${NORMAL}Testing local base save directory... "
  298. if [ -d ~/RetroPie/saves ]
  299. then
  300. printf "${GREEN}Found\n"
  301. else
  302. printf "${YELLOW}Not found\n"
  303. printf "${NORMAL} Creating local base save directory... "
  304. mkdir ~/RetroPie/saves
  305. printf "${GREEN}Done\n"
  306. fi
  307. printf "${NORMAL}Testing local system specific save directories... "
  308. # for each directory in ROMS directory...
  309. for directory in ~/RetroPie/roms/*
  310. do
  311. system="${directory##*/}"
  312. if [ ! -d ~/RetroPie/saves/${system} ]
  313. then
  314. mkdir ~/RetroPie/saves/${system}
  315. fi
  316. done
  317. printf "${GREEN}Done\n"
  318. }
  319. testRemoteSaveDirectory ()
  320. {
  321. read -p "${NORMAL}Please enter name of remote base save directory ([RetroArch]): " remotebasedir
  322. remotebasedir=${remotebasedir:-RetroArch}
  323. printf "${NORMAL}Testing remote base save directory (retropie:${remotebasedir})... "
  324. remotebasefound="FALSE"
  325. # list top level directories from remote
  326. directories=$(rclone lsf retropie:)
  327. # for each line from listing...
  328. while read directory
  329. do
  330. if [ "${directory}" = "${remotebasedir}/" ]
  331. then
  332. printf "${GREEN}Found\n"
  333. remotebasefound="TRUE"
  334. break
  335. fi
  336. done <<< "${directories}"
  337. if [ "$remotebasefound" = "FALSE" ]
  338. then
  339. printf "${YELLOW}Not found\n"
  340. printf "${NORMAL} Creating remote base save directory... "
  341. rclone mkdir retropie:${remotebasedir}
  342. printf "${GREEN}Done\n"
  343. fi
  344. # test and create system specific save directories
  345. printf "${NORMAL}Testing remote system specific save directories... "
  346. directories=$(rclone lsf retropie:${remotebasedir})
  347. # for each directory in ROMS directory...
  348. for directory in ~/RetroPie/roms/*
  349. do
  350. system="${directory##*/}"
  351. # use grep to search $SYSTEM in $DIRECTORIES
  352. retval=$(grep "${system}/" -nx <<< "${directories}")
  353. if [ "${retval}" = "" ]
  354. then
  355. # create system dir
  356. rclone mkdir retropie:${remotebasedir}/${system}
  357. fi
  358. done
  359. printf "${GREEN}Done\n"
  360. }
  361. setLocalSaveDirectoryPerSystem ()
  362. {
  363. # set local save directory per system
  364. printf "${NORMAL}Setting local save directory per system... "
  365. # for each directory...
  366. for directory in /opt/retropie/configs/*
  367. do
  368. system="${directory##*/}"
  369. # skip directory ALL
  370. if [ "${system}" = "all" ]
  371. then
  372. continue
  373. fi
  374. # test if there's a RETROARCH.CFG
  375. if [ -f "${directory}/retroarch.cfg" ]
  376. then
  377. # test file for SAVEFILE_DIRECTORY
  378. retval=$(grep -i "^savefile_directory = " ${directory}/retroarch.cfg)
  379. if [ ! "${retval}" = "" ]
  380. then
  381. # replace existing parameter
  382. sed -i "/^savefile_directory = /c\savefile_directory = \"~/RetroPie/saves/${system}\"" ${directory}/retroarch.cfg
  383. else
  384. # create new parameter above "#include..."
  385. sed -i "/^#include \"\/opt\/retropie\/configs\/all\/retroarch.cfg\"/c\savefile_directory = \"~\/RetroPie\/saves\/${system}\"\n#include \"\/opt\/retropie\/configs\/all\/retroarch.cfg\"" ${directory}/retroarch.cfg
  386. fi
  387. # test file for SAVESTATE_DIRECTORY
  388. retval=$(grep -i "^savestate_directory = " ${directory}/retroarch.cfg)
  389. if [ ! "${retval}" = "" ]
  390. then
  391. # replace existing parameter
  392. sed -i "/^savestate_directory = /c\savestate_directory = \"~/RetroPie/saves/${system}\"" ${directory}/retroarch.cfg
  393. else
  394. # create new parameter above "#include..."
  395. sed -i "/^#include \"\/opt\/retropie\/configs\/all\/retroarch.cfg\"/c\savestate_directory = \"~\/RetroPie\/saves\/${system}\"\n#include \"\/opt\/retropie\/configs\/all\/retroarch.cfg\"" ${directory}/retroarch.cfg
  396. fi
  397. fi
  398. done
  399. printf "${GREEN}Done\n"
  400. }
  401. saveConfiguration ()
  402. {
  403. printf "${NORMAL}Saving configuration of RCLONE_SCRIPT... "
  404. echo "remotebasedir=${remotebasedir}" > ~/scripts/rclone_script.ini
  405. echo "logfile=~/scripts/rclone_script.log" >> ~/scripts/rclone_script.ini
  406. echo "debug=0" >> ~/scripts/rclone_script.ini
  407. printf "${GREEN}Done\n"
  408. }
  409. getTypeOfRemote ()
  410. {
  411. # list all remotes and their type
  412. remotes=$(rclone listremotes -l)
  413. # get line wiht RETROPIE remote
  414. retval=$(grep -i "^retropie:" <<< ${remotes})
  415. remoteType="${retval#*:}"
  416. remoteType=$(echo ${remoteType} | xargs)
  417. }
  418. footer ()
  419. {
  420. printf "\n"
  421. printf "${GREEN}All done!\n"
  422. printf "${NORMAL}From now on, your saves and states will be\n"
  423. printf "${NORMAL}synchonised each time you start and stop a ROM.\n"
  424. printf "\n"
  425. printf "All systems will put their saves and states in\n"
  426. printf "\tLocal: \"${YELLOW}~/RetroPie/saves/<SYSTEM>${NORMAL}\"\n"
  427. printf "\tRemote: \"${YELLOW}retropie:${remotebasedir}/<SYSTEM> (${remoteType})${NORMAL}\"\n"
  428. printf "If you already have some saves in the ROM directories,\n"
  429. printf "you need to move them there manually!\n"
  430. printf "After moving your saves you should ${RED}reboot ${NORMAL}your RetroPie.\n"
  431. printf "\n"
  432. printf "Then, you should start a full sync via\n"
  433. printf "${YELLOW}RetroPie / RCLONE_SCRIPT FULL SYNC\n"
  434. printf "\n"
  435. printf "${NORMAL}Call \"${RED}~/scripts/rclone_script-uninstall.sh${NORMAL}\" to remove\n"
  436. printf "all or parts of this script\n"
  437. printf "\n"
  438. read -p "${NORMAL}Reboot RetroPie now? (y, [n]): " userInput
  439. userInput=${userInput:-n}
  440. if [ "${userInput}" = "y" ]; then
  441. sudo shutdown -r now
  442. fi
  443. }
  444. # main program
  445. header
  446. # test and install RCLONE
  447. testRCLONE
  448. # test and create RCLONE configuration
  449. #~/create_RCLONEconfig.sh # DEBUG
  450. testRCLONEconfiguration
  451. # test and install PNGVIEW
  452. testPNGVIEW
  453. # test and install IMAGEMAGICK
  454. testIMAGEMAGICK
  455. # install RCLONE_SCRIPT
  456. installRCLONE_SCRIPT
  457. # test and create RUNCOMMAND scripts
  458. testRUNCOMMAND
  459. # test and create local and remote save directories
  460. testLocalSaveDirectory
  461. testRemoteSaveDirectory
  462. setLocalSaveDirectoryPerSystem
  463. saveConfiguration
  464. getTypeOfRemote
  465. footer