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.

67 lines
1.7 KiB

  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-3.0+
  3. # Copyright (C) 2018, Martin Kepplinger <martink@posteo.de>
  4. RED='\033[0;31m'
  5. GREEN='\033[0;32m'
  6. YELLOW='\033[0;33m'
  7. NC='\033[0m'
  8. FLASHROM=$(whereis -b flashrom | cut -d ' ' -f 2)
  9. DMIDECODE=$(whereis -b dmidecode | cut -d ' ' -f 2)
  10. force_x230_and_root()
  11. {
  12. if [ "$EUID" -ne 0 ] ; then
  13. echo -e "${RED}Please run this as root.${NC} And make sure you have the following programs:"
  14. echo "dmidecode"
  15. echo "flashrom"
  16. exit 1
  17. fi
  18. local LAPTOP=$(${DMIDECODE} | grep -i x230 | sort -u)
  19. if [ -z "$LAPTOP" ] ; then
  20. echo "This is no Thinkpad X230. This script is useless then."
  21. exit 0
  22. fi
  23. }
  24. check_battery() {
  25. local capacity=$(cat /sys/class/power_supply/BAT*/capacity 2>/dev/null || echo -ne "0")
  26. local online=$(cat /sys/class/power_supply/AC/online 2>/dev/null || cat /sys/class/power_supply/ADP*/online 2>/dev/null || echo -ne "0")
  27. local failed=0
  28. if [ "${online}" == "0" ] ; then
  29. failed=1
  30. fi
  31. if [ "${capacity}" -lt 25 ]; then
  32. failed=1
  33. fi
  34. if [ $failed == "1" ]; then
  35. echo -e "${YELLOW}WARNING:${NC} To prevent shutdowns, we recommend to only run this script when"
  36. echo " your laptop is plugged in to the power supply AND"
  37. echo " the battery is present and sufficiently charged (over 25%)."
  38. while true; do
  39. read -r -p "Continue anyways? (please do NOT!) y/N: " yn
  40. case $yn in
  41. [Yy]* ) break;;
  42. [Nn]* ) exit;;
  43. * ) exit;;
  44. esac
  45. done
  46. fi
  47. }
  48. warn_not_root() {
  49. if [[ $EUID -eq 0 ]]; then
  50. echo -e "${YELLOW}WARNING:${NC} This should not be executed as root!"
  51. fi
  52. }
  53. poweroff() {
  54. if [ "$(command -v systemctl 2>/dev/null 2>&1)" ]; then
  55. systemctl poweroff
  56. else
  57. shutdown -P now
  58. fi
  59. }