Advertisement
sbicknel

Alarm dialog reminder

Feb 8th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.04 KB | None | 0 0
  1. #!/bin/bash -
  2.  
  3. # vim: set tabstop=8 softtabstop=4 shiftwidth=4 smarttab noexpandtab:
  4.  
  5. ${DEBUG}
  6.  
  7. #========================================================================
  8. #          FILE:  filename
  9. #
  10. #         USAGE:  filename [-h|--help][-V|--version]|[-l|--list]
  11. #                 -l    
  12. #                 --list    list alarms
  13. #         -h
  14. #         --help    display program help and exit
  15. #
  16. #         -u
  17. #         --usage   display a usage message and exit
  18. #  
  19. #         -V
  20. #         --version display the program version number and exit
  21. #         -x        display debugging information
  22. #
  23. #   DESCRIPTION:  This template is a starting point for creating full-
  24. #         featured shell scripts.
  25. #
  26. #       OPTIONS:  --help, --version
  27. #  REQUIREMENTS:  ---
  28. #          BUGS:  ---
  29. #         NOTES:  ---
  30. #        AUTHOR:  Scott Bicknell (SB), [email protected]
  31. #       COMPANY:  
  32. #       VERSION:  1.0
  33. #       CREATED:  
  34. #      REVISION:  ---
  35. #
  36. # Copyright 2010 Scott Bicknell, sbicknel (at) gmail dot com
  37. #
  38. # This program is free software: you can redistribute it and/or modify
  39. # it under the terms of the GNU General Public License as published by
  40. # the Free Software Foundation, either version 3 of the License, or
  41. # (at your option) any later version.
  42. #
  43. # This program is distributed in the hope that it will be useful,
  44. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  46. # GNU General Public License for more details.
  47. #
  48. # You should have received a copy of the GNU General Public License
  49. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  50. #========================================================================
  51.  
  52.  
  53. PROGNAME="${0##*/}"
  54. VERSION='1.0'
  55. PATH="${HOME}/bin:/sbin:/bin:/usr/bin:/usr/local/bin"
  56.  
  57. # honor the user's editor preference
  58. if [[ -n $EDITOR ]]; then
  59.     editor=$EDITOR
  60. elif [[ -n $VISUAL ]]; then
  61.     editor=$VISUAL
  62. else
  63.     editor=/usr/bin/vi
  64. fi
  65.  
  66. umask 077
  67.  
  68. # -----------------------------------------------------------------------
  69. # Display error messages on standard error.
  70. # -----------------------------------------------------------------------
  71. error () {
  72.     printf '%s: %s\n' $PROGNAME "$@"
  73. } >&2
  74.  
  75.  
  76. # -----------------------------------------------------------------------
  77. # Display messages on standard output.
  78. # -----------------------------------------------------------------------
  79. message () {
  80.     printf '%s: %s\n' $PROGNAME "$@"
  81. }
  82.  
  83.  
  84. # -----------------------------------------------------------------------
  85. # Remove temporary files. Add additional file names, if necessary.
  86. # -----------------------------------------------------------------------
  87. clean_up () {
  88.     rm -f ${TEMP_DIR}/${PROGNAME}.$$.*
  89. }
  90.  
  91.  
  92. # -----------------------------------------------------------------------
  93. # toggle write permission on or off. If turning them on, only give them
  94. # to the owner. If turning them off, turn them off for everyone. This
  95. # function operates on a list of files or folders.
  96. # -----------------------------------------------------------------------
  97. toggleWritePermissions () {
  98.     for f in $@; do
  99.     owp=$(stat --printf='%A' $f) # owner's write permissions
  100.     owp=${owp:2:1}
  101.     case $owp in
  102.         -)
  103.         chmod u+w $f
  104.         ;;
  105.  
  106.         w)
  107.         chmod a-w $f
  108.         ;;
  109.     esac
  110.     done
  111. }
  112.  
  113.  
  114. # -----------------------------------------------------------------------
  115. # Function for exit due to fatal program error
  116. #   Accepts 1 argument:
  117. #       string containing descriptive error message
  118. # -----------------------------------------------------------------------
  119. error_exit () {
  120.     error "${1:-'Unknown Error'}"
  121.     clean_up
  122.     exit 1
  123. }
  124.  
  125.  
  126. # -----------------------------------------------------------------------
  127. # Function to handle termination signals
  128. #   Accepts 1 argument:
  129. #       signal_spec
  130. # -----------------------------------------------------------------------
  131. signal_exit () {
  132.     case $1 in
  133.    
  134.     INT)
  135.         error_exit 'Program aborted by user'
  136.         ;;
  137.  
  138.     TERM)
  139.         error_exit 'Program terminated'
  140.         ;;
  141.  
  142.     *)
  143.         error_exit 'Terminating on unknown signal'
  144.         ;;
  145.  
  146.     esac
  147. }
  148.  
  149. trap 'signal_exit TERM' TERM HUP QUIT PIPE
  150. trap 'signal_exit INT'  INT
  151.  
  152.  
  153. # -----------------------------------------------------------------------
  154. # Function called for a graceful exit
  155. #   No arguments
  156. # -----------------------------------------------------------------------
  157. graceful_exit () {
  158.     clean_up
  159.     exit
  160. }
  161.  
  162.  
  163. # -----------------------------------------------------------------------
  164. # Function to create temporary files
  165. #   optional number of files to create--default is one
  166. #   returns an array ($TEMP_FILE) containing temporary file names
  167. # -----------------------------------------------------------------------
  168. make_temp_files () {
  169.  
  170.     declare fnum=${1:-1} # number of temporary files to create
  171.  
  172.     # Use user's local tmp directory if it exists
  173.     if [[ -d ~/tmp ]]; then
  174.     TEMP_DIR=~/tmp
  175.     else
  176.     TEMP_DIR=/tmp
  177.     fi
  178.  
  179.     # Temp files for this script, using paranoid method of creation to
  180.     # insure that file names are not predictable. This is for security to
  181.     # avoid "tmp race" attacks.
  182.     for (( n=0; n<$fnum; n++ )); do
  183.     TEMP_FILE[$n]=$(mktemp -q "${TEMP_DIR}/${PROGNAME}.$$.XXXXXX")
  184.     if [[ "${TEMP_FILE[$n]}" = '' ]]; then
  185.         error_exit 'cannot create temp file!'
  186.     fi
  187.     done
  188. }
  189.  
  190.  
  191. # -----------------------------------------------------------------------
  192. # Display a short message about program usage. This is also used by the
  193. # helptext function to display part of its help text.
  194. # -----------------------------------------------------------------------
  195. usage () {
  196.     cat <<- EOF
  197.    
  198. USAGE: $PROGNAME: [-h|--help][-u|--usage][-V|--version][-x]|[-l|--list]
  199. EOF
  200. } >&2
  201.  
  202.  
  203. # -----------------------------------------------------------------------
  204. # Display the program version number
  205. # -----------------------------------------------------------------------
  206. version () {
  207.     printf '%s %s\n' $PROGNAME $VERSION
  208. }
  209.  
  210.  
  211. # -----------------------------------------------------------------------
  212. # Function to display help message for program
  213. #   No arguments
  214. # -----------------------------------------------------------------------
  215. helptext () {
  216.     local tab=$(printf '\t\t')
  217.  
  218.     usage
  219.     cat <<- -EOF-
  220.  
  221.  
  222. DESCRIPTION:
  223.  
  224. Create one-time reminders. This program requires either kdialog or
  225. zenity and a valid X \$DISPLAY setting.
  226.  
  227.  
  228. Options:
  229.  
  230. -l, --list      List alarms
  231.  
  232. -h, --help  Display this help message and exit.
  233. -u, --usage Display a usage message and exit.
  234. -V, --version   Display program version and exit.
  235. -x      Display debugging output when running the program
  236.  
  237.  
  238. -EOF-
  239.     version
  240. } >&2
  241.  
  242.  
  243. # -----------------------------------------------------------------------
  244. # list pending alarms
  245. # -----------------------------------------------------------------------
  246. listAlarms () {
  247.     # get a list of at jobs
  248.     jobs=( $(atq | awk '{print $1}') )
  249.  
  250.     # get a list of at jobs containing the text "Alarm"
  251.     for job in ${jobs[@]}; do
  252.     if at -c $job | grep -q 'Alarm'; then
  253.         alarms=( ${alarms[@]} $job )
  254.     fi
  255.     done
  256.  
  257.     # check for an empty list. Create error message if needed
  258.     if [[ ${#alarms[@]} -eq 0 ]]; then
  259.     alarms[0]='No alarms to list'
  260.     kdialog --msgbox "${alarms[0]}" --title "${PROGNAME^}" 2>/dev/null
  261.     graceful_exit
  262.     fi
  263.  
  264.     # display the resulting list
  265.     alarmList=$"$(
  266.     for alarm in ${alarms[@]}; do
  267.         atq | grep "^$alarm"
  268.     done | sort
  269.    )"
  270.     case $dialog in
  271.     kdialog)
  272.         kdialog --msgbox "$alarmList" --title "${PROGNAME^}" 2>/dev/null
  273.         ;;
  274.  
  275.     zenity)
  276.         zenity --info --text="$alarmList" --title="${PROGNAME^}" 2>/dev/null
  277.         ;;
  278.  
  279.     esac
  280. }
  281.  
  282. # -----------------------------------------------------------------------
  283. # Verify that the usage function and the helptext function both have
  284. # documentation for each long option. Verify that the header comment
  285. # also has each long option listed.
  286. # -----------------------------------------------------------------------
  287. if [[ "$1" = '--help' ]]; then
  288.     helptext
  289.     graceful_exit
  290. fi
  291. if [[ "$1" = '--version' ]]; then
  292.     version
  293.     graceful_exit
  294. fi
  295. if [[ "$1" = '--usage' ]]; then
  296.     usage
  297.     graceful_exit
  298. fi
  299. if [[ "$1" = '--list' ]]; then
  300.     listAlarms
  301.     graceful_exit
  302. fi
  303.  
  304. # -----------------------------------------------------------------------
  305. # Verify that the usage function and the helptext function both have
  306. # documentation for each short option. Verify that the header comment
  307. # also has each short option listed in the usage section.
  308. # -----------------------------------------------------------------------
  309.  
  310. # detect which environment is running and set the dialog program
  311. # appropriately
  312. if [[ "$(pidof ksmserver)" ]]; then
  313.     if which kdialog >/dev/null 2>&1; then
  314.     dialog=kdialog
  315.     elif which zenity >/dev/null 2>&1; then
  316.     dialog=zenity
  317.     else
  318.     error_exit "Please install kdialog"
  319.     fi
  320. elif [[ "$(pidof gnome-session)" ]]; then
  321.     if which zenity >/dev/null 2>&1; then
  322.     dialog=zenity
  323.     elif which kdialog >/dev/null 2>&1; then
  324.     dialog=kdialog
  325.     else
  326.     error_exit "Please install zenity"
  327.     fi
  328. elif [[ "$(pidof xfce-mcs-manager)" ]]; then
  329.     if which zenity >/dev/null 2>&1; then
  330.     dialog=zenity
  331.     elif which kdialog >/dev/null 2>&1; then
  332.     dialog=kdialog
  333.     else
  334.     error_exit "Please install zenity"
  335.     fi
  336. else
  337.     if which kdialog >/dev/null 2>&1; then
  338.     dialog=kdialog
  339.     elif which zenity >/dev/null 2>&1; then
  340.     dialog=zenity
  341.     else
  342.     error_exit "Please install kdialog or zenity"
  343.     fi
  344. fi
  345.  
  346. while getopts :hluVx opt; do
  347.     case $opt in
  348.  
  349.     h)
  350.         helptext
  351.         graceful_exit
  352.         ;;
  353.  
  354.     l)
  355.         listAlarms
  356.         graceful_exit
  357.         ;;
  358.  
  359.     u)
  360.         usage
  361.         graceful_exit
  362.         ;;
  363.    
  364.     V)
  365.         version
  366.         graceful_exit
  367.         ;;
  368.  
  369.     x)  # set debugging output on a per-run basis. The $DEBUG
  370.         # environment variable will enable degugging output at
  371.         # the top of the script.
  372.         set -x
  373.         ;;
  374.  
  375.     :)  # process missing required parameters
  376.         error_exit "-$OPTARG: required argument missing"
  377.         ;;
  378.  
  379.     *)  # process unknown options
  380.         error "Unknown option: -$OPTARG"
  381.         usage
  382.         clean_up
  383.         exit 1
  384.         ;;
  385.  
  386.     esac
  387. done
  388. shift $[ $OPTIND - 1 ]
  389.  
  390. # to create multiple files, give number as argument to function call. This
  391. # creates an array of file names starting at index 0 ($TEMP_FILE)
  392. make_temp_files 1
  393.  
  394. case $dialog in
  395.  
  396.     kdialog)
  397.     time=$(kdialog --inputbox 'Alarm time:' --title "${PROGNAME^}" 2>/dev/null)
  398.     if [[ -z "$time" ]]; then
  399.         graceful_exit
  400.     fi
  401.     case ${time,,} in
  402.  
  403.         noon)
  404.         time=1200
  405.         ;;
  406.  
  407.         midnight)
  408.         time=0000
  409.         ;;
  410.  
  411.     esac
  412.     if ! date --date="$time" >/dev/null; then
  413.         kdialog --msgbox 'Invalid date string' --title "${PROGNAME^}" 2/dev/null
  414.         error_exit 'Invalid date string'
  415.     fi
  416.     alarmTime=$(date --date="$time" '+%-H:%M %b %-d')
  417.     message=$(kdialog --inputbox 'Message:' --title "${PROGNAME^}" 2>/dev/null)
  418.     if [[ -z "$message" ]]; then
  419.         graceful_exit
  420.     fi
  421.     at $alarmTime <<EOF
  422.     export DISPLAY=$DISPLAY
  423.     kdialog --msgbox "$message" --title "${PROGNAME^}" 2>/dev/null
  424. EOF
  425.     kdialog --msgbox "Alarm scheduled for: $alarmTime" --title "${PROGNAME^}" 2>/dev/null
  426.     ;;
  427.  
  428.     zenity)
  429.     time=$(zenity --entry --text='Alarm time:' --title=${PROGNAME^} 2>/dev/null)
  430.     if [[ -z "$time" ]]; then
  431.         graceful_exit
  432.     fi
  433.     case ${time,,} in
  434.  
  435.         noon)
  436.         time=1200
  437.         ;;
  438.  
  439.         midnight)
  440.         time=0000
  441.         ;;
  442.  
  443.     esac
  444.     if ! date --date="$time" >/dev/null; then
  445.         zenity --info --text='Invalid date string' --title=${PROGNAME^} 2>/dev/null
  446.         error_exit 'Invalid date string'
  447.     fi
  448.     alarmTime=$(date --date="$time" '+%-H:%M %b %-d')
  449.     message=$(zenity --entry --text="Message:" --title=${PROGNAME^} 2>/dev/null)
  450.     if [[ -z "$message" ]]; then
  451.         graceful_exit
  452.     fi
  453.     at $alarmTime <<EOF
  454.     export DISPLAY=$DISPLAY
  455.     zenity --info --text="$message" --title=${PROGNAME^} 2>/dev/null
  456. EOF
  457.     zenity --info --text="Alarm scheduled for: $alarmTime" --title=${PROGNAME^} 2>/dev/null
  458.     ;;
  459.  
  460.     *)
  461.     error_exit 'no dialog utility found'
  462.     ;;
  463.  
  464. esac
  465.  
  466. graceful_exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement