You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.3 KiB

  1. #!/bin/bash
  2. if [ $(id -u) -ne 0 ]; then
  3. echo "Installer must be run as root."
  4. echo "Try 'sudo bash $0'"
  5. exit 1
  6. fi
  7. clear
  8. echo "This script will install Adafruit"
  9. echo "fan service, which will turn on an"
  10. echo "external fan controlled by a given pin"
  11. echo
  12. echo "Operations performed include:"
  13. echo "- In /boot/config.txt, enable camera"
  14. echo "- apt-get update"
  15. echo "- Install Python libraries:"
  16. echo " picamera, pygame, PIL"
  17. echo "- Downgrade SDL library for pygame"
  18. echo " touch compatibility"
  19. echo "- Download Dropbox Updater and"
  20. echo " Adafruit Pi Cam software"
  21. echo
  22. echo "Run time 1+ minutes. Reboot not required."
  23. echo
  24. group=ADAFRUIT
  25. function info() {
  26. system="$1"
  27. group="${system}"
  28. shift
  29. FG="1;32m"
  30. BG="40m"
  31. echo -e "[\033[${FG}\033[${BG}${system}\033[0m] $*"
  32. }
  33. function bail() {
  34. FG="1;31m"
  35. BG="40m"
  36. echo -en "[\033[${FG}\033[${BG}${group}\033[0m] "
  37. if [ -z "$1" ]; then
  38. echo "Exiting due to error"
  39. else
  40. echo "Exiting due to error: $*"
  41. fi
  42. exit 1
  43. }
  44. if [ "$1" != '-y' ]; then
  45. echo -n "CONTINUE? [y/N]"
  46. read
  47. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  48. echo "Canceled."
  49. exit 0
  50. fi
  51. fi
  52. echo "Continuing..."
  53. # check init system (technique borrowed from raspi-config):
  54. info FAN 'Checking init system...'
  55. if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
  56. echo "Found systemd, OK!"
  57. elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
  58. bail "Found sysvinit, but we require systemd"
  59. else
  60. bail "Unrecognised init system"
  61. fi
  62. info FAN 'Adding adafruit_fan.service'
  63. cat > /etc/systemd/system/adafruit_fan.service <<EOF
  64. [Unit]
  65. Description=Fan service for some Adafruit boards
  66. After=network.target
  67. [Service]
  68. Type=oneshot
  69. ExecStartPre=-/bin/bash -c 'echo 4 >/sys/class/gpio/export'
  70. ExecStartPre=/bin/bash -c 'echo out >/sys/class/gpio/gpio4/direction'
  71. ExecStart=/bin/bash -c 'echo 1 >/sys/class/gpio/gpio4/value'
  72. RemainAfterExit=true
  73. ExecStop=/bin/bash -c 'echo 0 >/sys/class/gpio/gpio4/value'
  74. StandardOutput=journal
  75. [Install]
  76. WantedBy=multi-user.target
  77. EOF
  78. info FAN 'Enabling adafruit_fan.service'
  79. sudo systemctl enable adafruit_fan.service
  80. sudo systemctl start adafruit_fan.service
  81. info FAN 'Done!'
  82. echo "You can stop the fan service with 'sudo systemctl stop adafruit_fan.service'"
  83. echo "You can start the fan service with 'sudo systemctl start adafruit_fan.service'"