Guest User

argonone-setup-libreelec.sh

a guest
Mar 28th, 2020
213
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3.  
  4.  
  5. argon_create_file() {
  6.  
  7. if [ -f $1 ]; then
  8.  
  9. rm $1
  10.  
  11. fi
  12.  
  13. touch $1
  14.  
  15. chmod 666 $1
  16.  
  17. }
  18.  
  19.  
  20. daemonname="argononed"
  21.  
  22. powerbuttonscript=/storage/.config/$daemonname.py
  23.  
  24. shutdownscript="/storage/.config/"$daemonname"-poweroff.py"
  25.  
  26. daemonconfigfile=/storage/$daemonname.conf
  27.  
  28. configscript=/storage/argonone-config
  29.  
  30. removescript=/storage/argonone-uninstall
  31.  
  32. daemonfanservice=/storage/.config/system.d/$daemonname.service
  33.  
  34.  
  35. kodishutdownscript="/storage/.config/shutdown.sh"
  36.  
  37.  
  38.  
  39. mount -o remount,rw /flash
  40.  
  41.  
  42. echo "dtparam=i2c=on" >> /flash/config.txt
  43.  
  44. echo "enable_uart=1" >> /flash/config.txt
  45.  
  46.  
  47. mount -o remount,ro /flash
  48.  
  49.  
  50. if [ ! -f $daemonconfigfile ]; then
  51.  
  52. # Generate config file for fan speed
  53.  
  54. touch $daemonconfigfile
  55.  
  56. chmod 666 $daemonconfigfile
  57.  
  58. echo '#' >> $daemonconfigfile
  59.  
  60. echo '# Argon One Fan Configuration' >> $daemonconfigfile
  61.  
  62. echo '#' >> $daemonconfigfile
  63.  
  64. echo '# List below the temperature (Celsius) and fan speed (in percent) pairs' >> $daemonconfigfile
  65.  
  66. echo '# Use the following form:' >> $daemonconfigfile
  67.  
  68. echo '# min.temperature=speed' >> $daemonconfigfile
  69.  
  70. echo '#' >> $daemonconfigfile
  71.  
  72. echo '# Example:' >> $daemonconfigfile
  73.  
  74. echo '# 55=10' >> $daemonconfigfile
  75.  
  76. echo '# 60=55' >> $daemonconfigfile
  77.  
  78. echo '# 65=100' >> $daemonconfigfile
  79.  
  80. echo '#' >> $daemonconfigfile
  81.  
  82. echo '# Above example sets the fan speed to' >> $daemonconfigfile
  83.  
  84. echo '#' >> $daemonconfigfile
  85.  
  86. echo '# NOTE: Lines begining with # are ignored' >> $daemonconfigfile
  87.  
  88. echo '#' >> $daemonconfigfile
  89.  
  90. echo '# Type the following at the command line for changes to take effect:' >> $daemonconfigfile
  91.  
  92. echo '# systemctl restart '$daemonname'.service' >> $daemonconfigfile
  93.  
  94. echo '#' >> $daemonconfigfile
  95.  
  96. echo '# Start below:' >> $daemonconfigfile
  97.  
  98. echo '55=10' >> $daemonconfigfile
  99.  
  100. echo '60=55' >> $daemonconfigfile
  101.  
  102. echo '65=100' >> $daemonconfigfile
  103.  
  104. fi
  105.  
  106.  
  107. # Generate script that runs every shutdown event
  108.  
  109. argon_create_file $shutdownscript
  110.  
  111.  
  112. echo "#!/usr/bin/python" >> $shutdownscript
  113.  
  114. echo 'import sys' >> $shutdownscript
  115.  
  116. echo "sys.path.append('/storage/.kodi/addons/virtual.system-tools/lib')" >> $shutdownscript
  117.  
  118. echo 'import smbus' >> $shutdownscript
  119.  
  120. echo "sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')" >> $shutdownscript
  121.  
  122. echo 'import RPi.GPIO as GPIO' >> $shutdownscript
  123.  
  124. echo 'rev = GPIO.RPI_REVISION' >> $shutdownscript
  125.  
  126. echo 'if rev == 2 or rev == 3:' >> $shutdownscript
  127.  
  128. echo ' bus = smbus.SMBus(1)' >> $shutdownscript
  129.  
  130. echo 'else:' >> $shutdownscript
  131.  
  132. echo ' bus = smbus.SMBus(0)' >> $shutdownscript
  133.  
  134.  
  135. echo "try:" >> $shutdownscript
  136.  
  137. echo " bus.write_byte(0x1a,0)" >> $shutdownscript
  138.  
  139. echo " bus.write_byte(0x1a,0xFF)" >> $shutdownscript
  140.  
  141. echo "except:" >> $shutdownscript
  142.  
  143. echo " rev=0" >> $shutdownscript
  144.  
  145. chmod 755 $shutdownscript
  146.  
  147.  
  148. # Generate script to monitor shutdown button
  149.  
  150.  
  151. argon_create_file $powerbuttonscript
  152.  
  153.  
  154. echo "#!/usr/bin/python" >> $powerbuttonscript
  155.  
  156. echo 'import sys' >> $powerbuttonscript
  157.  
  158. echo "sys.path.append('/storage/.kodi/addons/virtual.system-tools/lib')" >> $powerbuttonscript
  159.  
  160. echo 'import smbus' >> $powerbuttonscript
  161.  
  162. echo "sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')" >> $powerbuttonscript
  163.  
  164. echo 'import RPi.GPIO as GPIO' >> $powerbuttonscript
  165.  
  166. echo 'import os' >> $powerbuttonscript
  167.  
  168. echo 'import time' >> $powerbuttonscript
  169.  
  170. echo 'from threading import Thread' >> $powerbuttonscript
  171.  
  172. echo 'rev = GPIO.RPI_REVISION' >> $powerbuttonscript
  173.  
  174. echo 'if rev == 2 or rev == 3:' >> $powerbuttonscript
  175.  
  176. echo ' bus = smbus.SMBus(1)' >> $powerbuttonscript
  177.  
  178. echo 'else:' >> $powerbuttonscript
  179.  
  180. echo ' bus = smbus.SMBus(0)' >> $powerbuttonscript
  181.  
  182.  
  183. echo 'GPIO.setwarnings(False)' >> $powerbuttonscript
  184.  
  185. echo 'GPIO.setmode(GPIO.BCM)' >> $powerbuttonscript
  186.  
  187. echo 'shutdown_pin=4' >> $powerbuttonscript
  188.  
  189. echo 'GPIO.setup(shutdown_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)' >> $powerbuttonscript
  190.  
  191.  
  192. echo 'def shutdown_check():' >> $powerbuttonscript
  193.  
  194. echo ' while True:' >> $powerbuttonscript
  195.  
  196. echo ' pulsetime = 1' >> $powerbuttonscript
  197.  
  198. echo ' GPIO.wait_for_edge(shutdown_pin, GPIO.RISING)' >> $powerbuttonscript
  199.  
  200. echo ' time.sleep(0.01)' >> $powerbuttonscript
  201.  
  202. echo ' while GPIO.input(shutdown_pin) == GPIO.HIGH:' >> $powerbuttonscript
  203.  
  204. echo ' time.sleep(0.01)' >> $powerbuttonscript
  205.  
  206. echo ' pulsetime += 1' >> $powerbuttonscript
  207.  
  208. echo ' if pulsetime >=2 and pulsetime <=3:' >> $powerbuttonscript
  209.  
  210. echo ' os.system("reboot")' >> $powerbuttonscript
  211.  
  212. echo ' elif pulsetime >=4 and pulsetime <=5:' >> $powerbuttonscript
  213.  
  214. echo ' os.system("shutdown now -h")' >> $powerbuttonscript
  215.  
  216.  
  217. echo 'def get_fanspeed(tempval, configlist):' >> $powerbuttonscript
  218.  
  219. echo ' for curconfig in configlist:' >> $powerbuttonscript
  220.  
  221. echo ' curpair = curconfig.split("=")' >> $powerbuttonscript
  222.  
  223. echo ' tempcfg = float(curpair[0])' >> $powerbuttonscript
  224.  
  225. echo ' fancfg = int(float(curpair[1]))' >> $powerbuttonscript
  226.  
  227. echo ' if tempval >= tempcfg:' >> $powerbuttonscript
  228.  
  229. echo ' return fancfg' >> $powerbuttonscript
  230.  
  231. echo ' return 0' >> $powerbuttonscript
  232.  
  233.  
  234. echo 'def load_config(fname):' >> $powerbuttonscript
  235.  
  236. echo ' newconfig = []' >> $powerbuttonscript
  237.  
  238. echo ' try:' >> $powerbuttonscript
  239.  
  240. echo ' with open(fname, "r") as fp:' >> $powerbuttonscript
  241.  
  242. echo ' for curline in fp:' >> $powerbuttonscript
  243.  
  244. echo ' if not curline:' >> $powerbuttonscript
  245.  
  246. echo ' continue' >> $powerbuttonscript
  247.  
  248. echo ' tmpline = curline.strip()' >> $powerbuttonscript
  249.  
  250. echo ' if not tmpline:' >> $powerbuttonscript
  251.  
  252. echo ' continue' >> $powerbuttonscript
  253.  
  254. echo ' if tmpline[0] == "#":' >> $powerbuttonscript
  255.  
  256. echo ' continue' >> $powerbuttonscript
  257.  
  258. echo ' tmppair = tmpline.split("=")' >> $powerbuttonscript
  259.  
  260. echo ' if len(tmppair) != 2:' >> $powerbuttonscript
  261.  
  262. echo ' continue' >> $powerbuttonscript
  263.  
  264. echo ' tempval = 0' >> $powerbuttonscript
  265.  
  266. echo ' fanval = 0' >> $powerbuttonscript
  267.  
  268. echo ' try:' >> $powerbuttonscript
  269.  
  270. echo ' tempval = float(tmppair[0])' >> $powerbuttonscript
  271.  
  272. echo ' if tempval < 0 or tempval > 100:' >> $powerbuttonscript
  273.  
  274. echo ' continue' >> $powerbuttonscript
  275.  
  276. echo ' except:' >> $powerbuttonscript
  277.  
  278. echo ' continue' >> $powerbuttonscript
  279.  
  280. echo ' try:' >> $powerbuttonscript
  281.  
  282. echo ' fanval = int(float(tmppair[1]))' >> $powerbuttonscript
  283.  
  284. echo ' if fanval < 0 or fanval > 100:' >> $powerbuttonscript
  285.  
  286. echo ' continue' >> $powerbuttonscript
  287.  
  288. echo ' except:' >> $powerbuttonscript
  289.  
  290. echo ' continue' >> $powerbuttonscript
  291.  
  292. echo ' newconfig.append( "{:5.1f}={}".format(tempval,fanval))' >> $powerbuttonscript
  293.  
  294. echo ' if len(newconfig) > 0:' >> $powerbuttonscript
  295.  
  296. echo ' newconfig.sort(reverse=True)' >> $powerbuttonscript
  297.  
  298. echo ' except:' >> $powerbuttonscript
  299.  
  300. echo ' return []' >> $powerbuttonscript
  301.  
  302. echo ' return newconfig' >> $powerbuttonscript
  303.  
  304.  
  305. echo 'def temp_check():' >> $powerbuttonscript
  306.  
  307. echo ' fanconfig = ["65=100", "60=55", "55=10"]' >> $powerbuttonscript
  308.  
  309. echo ' tmpconfig = load_config("'$daemonconfigfile'")' >> $powerbuttonscript
  310.  
  311. echo ' if len(tmpconfig) > 0:' >> $powerbuttonscript
  312.  
  313. echo ' fanconfig = tmpconfig' >> $powerbuttonscript
  314.  
  315. echo ' address=0x1a' >> $powerbuttonscript
  316.  
  317. echo ' prevblock=0' >> $powerbuttonscript
  318.  
  319. echo ' while True:' >> $powerbuttonscript
  320.  
  321. echo ' temp = os.popen("vcgencmd measure_temp").readline()' >> $powerbuttonscript
  322.  
  323. echo ' temp = temp.replace("temp=","")' >> $powerbuttonscript
  324.  
  325. echo ' val = float(temp.replace("'"'"'C",""))' >> $powerbuttonscript
  326.  
  327. echo ' block = get_fanspeed(val, fanconfig)' >> $powerbuttonscript
  328.  
  329. echo ' if block < prevblock:' >> $powerbuttonscript
  330.  
  331. echo ' time.sleep(30)' >> $powerbuttonscript
  332.  
  333. echo ' prevblock = block' >> $powerbuttonscript
  334.  
  335. echo ' try:' >> $powerbuttonscript
  336.  
  337. echo ' bus.write_byte(address,block)' >> $powerbuttonscript
  338.  
  339. echo ' except IOError:' >> $powerbuttonscript
  340.  
  341. echo ' temp=""' >> $powerbuttonscript
  342.  
  343. echo ' time.sleep(30)' >> $powerbuttonscript
  344.  
  345.  
  346. echo 'try:' >> $powerbuttonscript
  347.  
  348. echo ' t1 = Thread(target = shutdown_check)' >> $powerbuttonscript
  349.  
  350. echo ' t2 = Thread(target = temp_check)' >> $powerbuttonscript
  351.  
  352. echo ' t1.start()' >> $powerbuttonscript
  353.  
  354. echo ' t2.start()' >> $powerbuttonscript
  355.  
  356. echo 'except:' >> $powerbuttonscript
  357.  
  358. echo ' t1.stop()' >> $powerbuttonscript
  359.  
  360. echo ' t2.stop()' >> $powerbuttonscript
  361.  
  362. echo ' GPIO.cleanup()' >> $powerbuttonscript
  363.  
  364.  
  365. chmod 755 $powerbuttonscript
  366.  
  367.  
  368. argon_create_file $daemonfanservice
  369.  
  370.  
  371. # Fan Daemon
  372.  
  373. echo "[Unit]" >> $daemonfanservice
  374.  
  375. echo "Description=Argon One Fan and Button Service" >> $daemonfanservice
  376.  
  377. echo "After=multi-user.target" >> $daemonfanservice
  378.  
  379. echo '[Service]' >> $daemonfanservice
  380.  
  381. echo 'Type=simple' >> $daemonfanservice
  382.  
  383. echo "Restart=always" >> $daemonfanservice
  384.  
  385. echo "RemainAfterExit=true" >> $daemonfanservice
  386.  
  387. echo "ExecStart=/usr/bin/python $powerbuttonscript" >> $daemonfanservice
  388.  
  389. echo '[Install]' >> $daemonfanservice
  390.  
  391. echo "WantedBy=multi-user.target" >> $daemonfanservice
  392.  
  393.  
  394. chmod 644 $daemonfanservice
  395.  
  396.  
  397. argon_create_file $removescript
  398.  
  399.  
  400. # Uninstall Script
  401.  
  402. echo '#!/bin/bash' >> $removescript
  403.  
  404. echo 'echo "-------------------------"' >> $removescript
  405.  
  406. echo 'echo "Argon One Uninstall Tool"' >> $removescript
  407.  
  408. echo 'echo "-------------------------"' >> $removescript
  409.  
  410. echo 'echo -n "Press Y to continue:"' >> $removescript
  411.  
  412. echo 'read -n 1 confirm' >> $removescript
  413.  
  414. echo 'echo' >> $removescript
  415.  
  416. echo 'if [ "$confirm" = "y" ]' >> $removescript
  417.  
  418. echo 'then' >> $removescript
  419.  
  420. echo ' confirm="Y"' >> $removescript
  421.  
  422. echo 'fi' >> $removescript
  423.  
  424. echo '' >> $removescript
  425.  
  426. echo 'if [ "$confirm" != "Y" ]' >> $removescript
  427.  
  428. echo 'then' >> $removescript
  429.  
  430. echo ' echo "Cancelled"' >> $removescript
  431.  
  432. echo ' exit' >> $removescript
  433.  
  434. echo 'fi' >> $removescript
  435.  
  436. echo 'if [ -f '$powerbuttonscript' ]; then' >> $removescript
  437.  
  438. echo ' systemctl stop '$daemonname'.service' >> $removescript
  439.  
  440. echo ' systemctl disable '$daemonname'.service' >> $removescript
  441.  
  442. echo ' rm '$powerbuttonscript >> $removescript
  443.  
  444. echo ' rm '$shutdownscript >> $removescript
  445.  
  446. echo ' rm '$removescript >> $removescript
  447.  
  448. echo ' echo "Removed Argon One Services."' >> $removescript
  449.  
  450. echo ' echo "Cleanup will complete after restarting the device."' >> $removescript
  451.  
  452. echo 'fi' >> $removescript
  453.  
  454.  
  455. chmod 755 $removescript
  456.  
  457.  
  458. argon_create_file $configscript
  459.  
  460.  
  461. # Config Script
  462.  
  463. echo '#!/bin/bash' >> $configscript
  464.  
  465. echo 'daemonconfigfile='$daemonconfigfile >> $configscript
  466.  
  467. echo 'echo "--------------------------------------"' >> $configscript
  468.  
  469. echo 'echo "Argon One Fan Speed Configuration Tool"' >> $configscript
  470.  
  471. echo 'echo "--------------------------------------"' >> $configscript
  472.  
  473. echo 'echo "WARNING: This will remove existing configuration."' >> $configscript
  474.  
  475. echo 'echo -n "Press Y to continue:"' >> $configscript
  476.  
  477. echo 'read -n 1 confirm' >> $configscript
  478.  
  479. echo 'echo' >> $configscript
  480.  
  481. echo 'if [ "$confirm" = "y" ]' >> $configscript
  482.  
  483. echo 'then' >> $configscript
  484.  
  485. echo ' confirm="Y"' >> $configscript
  486.  
  487. echo 'fi' >> $configscript
  488.  
  489. echo '' >> $configscript
  490.  
  491. echo 'if [ "$confirm" != "Y" ]' >> $configscript
  492.  
  493. echo 'then' >> $configscript
  494.  
  495. echo ' echo "Cancelled"' >> $configscript
  496.  
  497. echo ' exit' >> $configscript
  498.  
  499. echo 'fi' >> $configscript
  500.  
  501. echo 'echo "Thank you."' >> $configscript
  502.  
  503.  
  504.  
  505. echo 'echo -n "Press Y if you want the fan to be always on:"' >> $configscript
  506.  
  507. echo 'read -n 1 confirm' >> $configscript
  508.  
  509. echo 'echo' >> $configscript
  510.  
  511. echo 'if [ "$confirm" = "y" ]' >> $configscript
  512.  
  513. echo 'then' >> $configscript
  514.  
  515. echo ' confirm="Y"' >> $configscript
  516.  
  517. echo 'fi' >> $configscript
  518.  
  519. echo '' >> $configscript
  520.  
  521. echo 'echo "#" > $daemonconfigfile' >> $configscript
  522.  
  523. echo 'echo "# Argon One Fan Speed Configuration" >> $daemonconfigfile' >> $configscript
  524.  
  525. echo 'echo "#" >> $daemonconfigfile' >> $configscript
  526.  
  527. echo 'echo "# Min Temp=Fan Speed" >> $daemonconfigfile' >> $configscript
  528.  
  529.  
  530. echo 'if [ "$confirm" != "Y" ]' >> $configscript
  531.  
  532. echo 'then' >> $configscript
  533.  
  534. echo ' echo 90"="100 >> $daemonconfigfile' >> $configscript
  535.  
  536. echo ' echo "Fan off."' >> $configscript
  537.  
  538. echo 'else' >> $configscript
  539.  
  540. echo ' echo 1"="100 >> $daemonconfigfile' >> $configscript
  541.  
  542. echo ' echo "Fan always on."' >> $configscript
  543.  
  544. echo 'fi' >> $configscript
  545.  
  546. echo 'systemctl restart '$daemonname'.service' >> $configscript
  547.  
  548.  
  549. chmod 755 $configscript
  550.  
  551.  
  552. needsshutdownedit=0
  553.  
  554. if [ -f $kodishutdownscript ]; then
  555.  
  556. needsshutdownedit=1
  557.  
  558. echo '# Kindly add the following lines under the "halt" and "poweroff" (remove "#" from the start)' >> $kodishutdownscript
  559.  
  560. echo '# /usr/bin/python '$shutdownscript >> $kodishutdownscript
  561.  
  562. else
  563.  
  564. argon_create_file $kodishutdownscript
  565.  
  566. # Shutdown Script
  567.  
  568. echo '#!/bin/bash' >> $kodishutdownscript
  569.  
  570. echo 'case "$1" in' >> $kodishutdownscript
  571.  
  572. echo ' halt)' >> $kodishutdownscript
  573.  
  574. echo ' /usr/bin/python '$shutdownscript >> $kodishutdownscript
  575.  
  576. echo ' ;;' >> $kodishutdownscript
  577.  
  578. echo ' poweroff)' >> $kodishutdownscript
  579.  
  580. echo ' /usr/bin/python '$shutdownscript >> $kodishutdownscript
  581.  
  582. echo ' ;;' >> $kodishutdownscript
  583.  
  584. echo ' reboot)' >> $kodishutdownscript
  585.  
  586. echo ' #' >> $kodishutdownscript
  587.  
  588. echo ' ;;' >> $kodishutdownscript
  589.  
  590. echo ' *)' >> $kodishutdownscript
  591.  
  592. echo ' # your commands here' >> $kodishutdownscript
  593.  
  594. echo ' ;;' >> $kodishutdownscript
  595.  
  596. echo 'esac' >> $kodishutdownscript
  597.  
  598.  
  599. chmod 755 $kodishutdownscript
  600.  
  601. fi
  602.  
  603.  
  604.  
  605.  
  606. systemctl daemon-reload
  607.  
  608. systemctl enable $daemonname.service
  609.  
  610. systemctl start $daemonname.service
  611.  
  612.  
  613. echo "***************************"
  614.  
  615. echo "Argon One Setup Completed."
  616.  
  617. echo "***************************"
  618.  
  619. if [ $needsshutdownedit -gt 0 ]
  620.  
  621. then
  622.  
  623. nano $kodishutdownscript
  624.  
  625. fi
  626.  
  627. echo "Changes take effect upon restart"
  628.  
  629.  
  630.  
  631.  
RAW Paste Data