rclone_script-install.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. echo "${url}/${branch}/rclone_script-uninstall.sh"
  230. echo "https://raw.githubusercontent.com/Jandalf81/rclone_script/master/rclone_script-uninstall.sh"
  231. { # try
  232. retval=$(wget -q -N -P ~/scripts ${url}/${branch}/rclone_script-uninstall.sh 2>&1) &&
  233. retval=$(sudo chmod 755 ~/scripts/rclone_script-uninstall.sh 2>&1) &&
  234. printf "${GREEN}Done\n"
  235. } || { # catch
  236. printf "${RED}ERROR: ${retval}\n"
  237. exit
  238. }
  239. }
  240. testRUNCOMMAND ()
  241. {
  242. # test RUNCOMMAND-ONSTART
  243. printf "${NORMAL}Testing for RUNCOMMAND-ONSTART... "
  244. if [ -f /opt/retropie/configs/all/runcommand-onstart.sh ]
  245. then
  246. # file exists
  247. printf "${GREEN}Found\n"
  248. printf "${NORMAL} Testing RUNCOMMAND-ONSTART for call to RCLONE_SCRIPT... "
  249. # test call to RCLONE from RUNCOMMAND-ONSTART
  250. if grep -Fq "~/scripts/rclone_script.sh" /opt/retropie/configs/all/runcommand-onstart.sh
  251. then
  252. printf "${GREEN}Found\n"
  253. else
  254. printf "${YELLOW}Not found\n"
  255. printf "${NORMAL} Adding call to RCLONE_SCRIPT... "
  256. echo "~/scripts/rclone_script.sh \"down\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onstart.sh
  257. printf "${GREEN}Done\n"
  258. fi
  259. else
  260. # file does not exist
  261. printf "${YELLOW}Not found\n"
  262. printf "${NORMAL} Creating RUNCOMMAND-ONSTART... "
  263. echo "#!/bin/bash" > /opt/retropie/configs/all/runcommand-onstart.sh
  264. echo "~/scripts/rclone_script.sh \"down\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onstart.sh
  265. printf "${GREEN}Done\n"
  266. fi
  267. # test RUNCOMMAND-ONEND
  268. printf "${NORMAL}Testing for RUNCOMMAND-ONEND... "
  269. if [ -f /opt/retropie/configs/all/runcommand-onend.sh ]
  270. then
  271. # file exists
  272. printf "${GREEN}Found\n"
  273. printf "${NORMAL} Testing RUNCOMMAND-ONEND for call to RCLONE_SCRIPT... "
  274. # test call to RCLONE from RUNCOMMAND-ONEND
  275. if grep -Fq "~/scripts/rclone_script.sh" /opt/retropie/configs/all/runcommand-onend.sh
  276. then
  277. printf "${GREEN}Found\n"
  278. else
  279. printf "${YELLOW}Not found\n"
  280. printf "${NORMAL} Adding call to RCLONE_SCRIPT... "
  281. echo "~/scripts/rclone_script.sh \"up\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onend.sh
  282. printf "${GREEN}Done\n"
  283. fi
  284. else
  285. # file does not exist
  286. printf "${YELLOW}Not found\n"
  287. printf "${NORMAL} Creating RUNCOMMAND-ONSTART... "
  288. echo "#!/bin/bash" > /opt/retropie/configs/all/runcommand-onend.sh
  289. echo "~/scripts/rclone_script.sh \"up\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onend.sh
  290. printf "${GREEN}Done\n"
  291. fi
  292. }
  293. testLocalSaveDirectory ()
  294. {
  295. printf "${NORMAL}Testing local base save directory... "
  296. if [ -d ~/RetroPie/saves ]
  297. then
  298. printf "${GREEN}Found\n"
  299. else
  300. printf "${YELLOW}Not found\n"
  301. printf "${NORMAL} Creating local base save directory... "
  302. mkdir ~/RetroPie/saves
  303. printf "${GREEN}Done\n"
  304. fi
  305. printf "${NORMAL}Testing local system specific save directories... "
  306. # for each directory in ROMS directory...
  307. for directory in ~/RetroPie/roms/*
  308. do
  309. system="${directory##*/}"
  310. if [ ! -d ~/RetroPie/saves/${system} ]
  311. then
  312. mkdir ~/RetroPie/saves/${system}
  313. fi
  314. done
  315. printf "${GREEN}Done\n"
  316. }
  317. testRemoteSaveDirectory ()
  318. {
  319. read -p "${NORMAL}Please enter name of remote base save directory ([RetroArch]): " remotebasedir
  320. remotebasedir=${remotebasedir:-RetroArch}
  321. printf "${NORMAL}Testing remote base save directory (retropie:${remotebasedir})... "
  322. remotebasefound="FALSE"
  323. # list top level directories from remote
  324. directories=$(rclone lsf retropie:)
  325. # for each line from listing...
  326. while read directory
  327. do
  328. if [ "${directory}" = "${remotebasedir}/" ]
  329. then
  330. printf "${GREEN}Found\n"
  331. remotebasefound="TRUE"
  332. break
  333. fi
  334. done <<< "${directories}"
  335. if [ "$remotebasefound" = "FALSE" ]
  336. then
  337. printf "${YELLOW}Not found\n"
  338. printf "${NORMAL} Creating remote base save directory... "
  339. rclone mkdir retropie:${remotebasedir}
  340. printf "${GREEN}Done\n"
  341. fi
  342. # test and create system specific save directories
  343. printf "${NORMAL}Testing remote system specific save directories... "
  344. directories=$(rclone lsf retropie:${remotebasedir})
  345. # for each directory in ROMS directory...
  346. for directory in ~/RetroPie/roms/*
  347. do
  348. system="${directory##*/}"
  349. # use grep to search $SYSTEM in $DIRECTORIES
  350. retval=$(grep "${system}/" -nx <<< "${directories}")
  351. if [ "${retval}" = "" ]
  352. then
  353. # create system dir
  354. rclone mkdir retropie:${remotebasedir}/${system}
  355. fi
  356. done
  357. printf "${GREEN}Done\n"
  358. }
  359. setLocalSaveDirectoryPerSystem ()
  360. {
  361. # set local save directory per system
  362. printf "${NORMAL}Setting local save directory per system... "
  363. # for each directory...
  364. for directory in /opt/retropie/configs/*
  365. do
  366. system="${directory##*/}"
  367. # skip directory ALL
  368. if [ "${system}" = "all" ]
  369. then
  370. continue
  371. fi
  372. # test if there's a RETROARCH.CFG
  373. if [ -f "${directory}/retroarch.cfg" ]
  374. then
  375. # test file for SAVEFILE_DIRECTORY
  376. retval=$(grep -i "^savefile_directory = " ${directory}/retroarch.cfg)
  377. if [ ! "${retval}" = "" ]
  378. then
  379. # replace existing parameter
  380. sed -i "/^savefile_directory = /c\savefile_directory = \"~/RetroPie/saves/${system}\"" ${directory}/retroarch.cfg
  381. else
  382. # create new parameter above "#include..."
  383. 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
  384. fi
  385. # test file for SAVESTATE_DIRECTORY
  386. retval=$(grep -i "^savestate_directory = " ${directory}/retroarch.cfg)
  387. if [ ! "${retval}" = "" ]
  388. then
  389. # replace existing parameter
  390. sed -i "/^savestate_directory = /c\savestate_directory = \"~/RetroPie/saves/${system}\"" ${directory}/retroarch.cfg
  391. else
  392. # create new parameter above "#include..."
  393. 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
  394. fi
  395. fi
  396. done
  397. printf "${GREEN}Done\n"
  398. }
  399. saveConfiguration ()
  400. {
  401. printf "${NORMAL}Saving configuration of RCLONE_SCRIPT... "
  402. echo "remotebasedir=${remotebasedir}" > ~/scripts/rclone_script.ini
  403. echo "logfile=~/scripts/rclone_script.log" >> ~/scripts/rclone_script.ini
  404. echo "debug=0" >> ~/scripts/rclone_script.ini
  405. printf "${GREEN}Done\n"
  406. }
  407. getTypeOfRemote ()
  408. {
  409. # list all remotes and their type
  410. remotes=$(rclone listremotes -l)
  411. # get line wiht RETROPIE remote
  412. retval=$(grep -i "^retropie:" <<< ${remotes})
  413. remoteType="${retval#*:}"
  414. remoteType=$(echo ${remoteType} | xargs)
  415. }
  416. footer ()
  417. {
  418. printf "\n"
  419. printf "${GREEN}All done!\n"
  420. printf "${NORMAL}From now on, your saves and states will be\n"
  421. printf "${NORMAL}synchonised each time you start and stop a ROM.\n"
  422. printf "\n"
  423. printf "All systems will put their saves and states in\n"
  424. printf "\tLocal: \"${YELLOW}~/RetroPie/saves/<SYSTEM>${NORMAL}\"\n"
  425. printf "\tRemote: \"${YELLOW}retropie:${remotebasedir}/<SYSTEM> (${remoteType})${NORMAL}\"\n"
  426. printf "If you already have some saves in the ROM directories,\n"
  427. printf "you need to move them there manually!\n"
  428. printf "After moving your saves you should ${RED}reboot ${NORMAL}your RetroPie.\n"
  429. printf "\n"
  430. printf "Then, you should start a full sync via\n"
  431. printf "${YELLOW}RetroPie / RCLONE_SCRIPT FULL SYNC\n"
  432. printf "\n"
  433. printf "${NORMAL}Call \"${RED}~/scripts/rclone_script-uninstall.sh${NORMAL}\" to remove\n"
  434. printf "all or parts of this script\n"
  435. printf "\n"
  436. read -p "${NORMAL}Reboot RetroPie now? (y, [n]): " userInput
  437. userInput=${userInput:-n}
  438. if [ "${userInput}" = "y" ]; then
  439. sudo shutdown -r now
  440. fi
  441. }
  442. # main program
  443. header
  444. # test and install RCLONE
  445. testRCLONE
  446. # test and create RCLONE configuration
  447. #~/create_RCLONEconfig.sh # DEBUG
  448. testRCLONEconfiguration
  449. # test and install PNGVIEW
  450. testPNGVIEW
  451. # test and install IMAGEMAGICK
  452. testIMAGEMAGICK
  453. # install RCLONE_SCRIPT
  454. installRCLONE_SCRIPT
  455. # test and create RUNCOMMAND scripts
  456. testRUNCOMMAND
  457. # test and create local and remote save directories
  458. testLocalSaveDirectory
  459. testRemoteSaveDirectory
  460. setLocalSaveDirectoryPerSystem
  461. saveConfiguration
  462. getTypeOfRemote
  463. footer