rclone_script-install.sh 14 KB

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