1
0

rclone_script-install.sh 14 KB

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