rclone_script-install.sh 14 KB

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