turnoff 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/sh
  2. # Copyright © 2012-2016 Alex Kost
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. LOGPATH="$HOME/.turnoff_log"
  14. SHUTDOWN=no
  15. CONKEROR=no
  16. TVTIME=no
  17. EMACS=no
  18. EMMS=no
  19. usage () {
  20. echo 'Usage: turnoff [OPTION ...] [TIME]
  21. Switch to VT11 or shut down the computer with some preparations.
  22. Options:
  23. -h, --help display help and exit;
  24. -s, --shutdown [KEY] shutdown (otherwise switch to VT11);
  25. -c, --conkeror [KEY] close conkeror;
  26. -t, --tvtime [KEY] close tvtime.
  27. -e, --emacs [KEY] close emacs;
  28. -m, --emms [KEY] stop EMMS;
  29. -a, --all enable all the above keys (close everything).
  30. KEY is an optional argument: can be "yes" or "no" (default: "yes").
  31. TIME can be in any format, supported by GNU "date" function.'
  32. }
  33. TEMP=`getopt -o has::c::t::e::m:: \
  34. -l help,all,shutdown::,conkeror::,tvtime::,emacs::,emms:: \
  35. -n turnoff -- "$@"`
  36. eval set -- "$TEMP"
  37. while true; do
  38. case "$1" in
  39. -h | --help)
  40. usage
  41. exit 0 ;;
  42. -a | --all)
  43. SHUTDOWN=yes
  44. CONKEROR=yes
  45. TVTIME=yes
  46. EMACS=yes
  47. EMMS=yes
  48. shift ;;
  49. -s | --shutdown)
  50. case "$2" in
  51. "") SHUTDOWN=yes ; shift 2 ;;
  52. *) SHUTDOWN="$2" ; shift 2 ;;
  53. esac ;;
  54. -c | --conkeror)
  55. case "$2" in
  56. "") CONKEROR=yes ; shift 2 ;;
  57. *) CONKEROR="$2" ; shift 2 ;;
  58. esac ;;
  59. -t | --tvtime)
  60. case "$2" in
  61. "") TVTIME=yes ; shift 2 ;;
  62. *) TVTIME="$2" ; shift 2 ;;
  63. esac ;;
  64. -e | --emacs)
  65. case "$2" in
  66. "") EMACS=yes ; shift 2 ;;
  67. *) EMACS="$2" ; shift 2 ;;
  68. esac ;;
  69. -m | --emms)
  70. case "$2" in
  71. "") EMMS=yes ; shift 2 ;;
  72. *) EMMS="$2" ; shift 2 ;;
  73. esac ;;
  74. --)
  75. shift
  76. break ;;
  77. *)
  78. echo "Internal error!"
  79. exit 1 ;;
  80. esac
  81. done
  82. if [ -z "$1" ]; then
  83. DEADLINE=now
  84. SECONDS=0
  85. else
  86. DEADLINE="$1"
  87. SECONDS=$(( $(date --date="$DEADLINE" +%s) - $(date +%s) ))
  88. fi
  89. echo "shutdown: $SHUTDOWN"
  90. echo "conkeror: $CONKEROR"
  91. echo "tvtime: $TVTIME"
  92. echo "emacs: $EMACS"
  93. echo "emms: $EMMS"
  94. echo "---------------- $(date) ----------------" | tee -a "$LOGPATH"
  95. echo 'Computer will be turned off at "'$(date --date="$DEADLINE")\" | tee -a "$LOGPATH"
  96. echo "Sleeping for $SECONDS seconds..." | tee -a "$LOGPATH"
  97. sleep $SECONDS
  98. if [ $EMMS == "yes" ]; then
  99. emacsclient --socket-name=server-emms --eval '(emms-stop)'
  100. echo 'EMMS has been stopped ('$(date)')' | tee -a "$LOGPATH"
  101. sleep 1
  102. fi
  103. if [ $EMACS == "yes" ]; then
  104. wmctrl -x -c emacs
  105. echo 'Closing emacs... ('$(date)')' | tee -a "$LOGPATH"
  106. sleep 1
  107. fi
  108. if [ $CONKEROR == "yes" ]; then
  109. wmctrl -x -c conkeror
  110. echo 'Closing conkeror... ('$(date)')' | tee -a "$LOGPATH"
  111. sleep 1
  112. fi
  113. if [ $TVTIME == "yes" ]; then
  114. tvtime-command quit
  115. echo 'Closing tvtime... ('$(date)')' | tee -a "$LOGPATH"
  116. sleep 1
  117. fi
  118. if [ $SHUTDOWN == "yes" ]; then
  119. echo -e 'Shutting down now... ('$(date)')'"\n" | tee -a "$LOGPATH"
  120. shutdown now
  121. else
  122. echo -e 'Switching to VT11 ('$(date)')'"\n" | tee -a "$LOGPATH"
  123. sudo chvt 11
  124. fi