add more files
[misc.git] / bin / tzwatch
1 #!/bin/bash
2
3 # tzwatch
4
5 # Displays the time in a number of time zones, specified by the user.
6 # The list of time zones (format as TZ variable) is kept in ${HOME}/.tzlist
7 # The time zone is chosen using tzselect
8 # (Debian default is /usr/bin/tzselect, in package libc6).
9 # ( ${HOME}/.tzlist could be edited by hand, with a valid value for the TZ variable on each line)
10
11 # Command line options:
12 # -c, configure time zones (add or remove)
13 # -f FORMAT display time according to FORMAT (see 'date' command)
14 # If no command line options are given, the time in each zone at the
15 # given moment is printed.
16
17 # Things that could be done:
18 # Possibly this would be better done in perl. Too bad.
19 # A window-graphics display could be nice too.
20 # Add an option -r [n] to run continuously (display updated every n seconds)
21
22 # This program is considered copyrighted and distributable under the terms of the GPL, which I will not bother copying here.
23
24 # Drew Parsons <dparsons@debian.org>
25 # Version 1.2.3 14 July 2002
26 # only refer to timezone label for existing zones when configuring
27 # Version 1.2.2, 20 Jan 2002
28 # change "timezone" to "time zone". tzselect is now in package libc6.
29 # Version 1.2.1, May 2000
30 # made recognition of timezone "Local" case-insensitive
31 # Version 1.2, March 2000
32 # configfile renamed to .tzlist (for compatibility with gworldclock), 'local' zone added
33 # Version 1.1, [2] January 2000 (should that be 1.0.1 ?? )
34 # tzselect gone missing in potato. Located in slink's timezones package.
35 # Version 1.0, 29 November 1999
36 # Original version
37
38
39 # default config file
40 TZWATCH=${HOME}/.tzlist
41
42 if [ "$1" == "--help" ]; then
43 echo "Usage: tzwatch [-c] [-f FORMAT]"
44 echo " -c configure time zones (add or remove)"
45 echo " -f FORMAT display time according to FORMAT (see 'date' command)"
46 exit 0;
47 fi
48
49
50 # set config file if not yet done
51 if [ ! -s $TZWATCH ]; then
52 if [ `type -p tzselect` ]; then
53 tzselect > $TZWATCH
54 else
55 echo tzselect not found. Cannot configure tzwatch.
56 exit 1
57 fi
58 fi
59
60 while getopts "cf:" OPT; do
61 case $OPT in
62 c) # configure: add or remove entry
63 choice=
64 while [ "$choice" != "Done" ]; do
65 echo Choose zone to remove or add new zone:
66 select choice in \
67 `awk '// {print $1}' $TZWATCH` 'Add Zone' 'Add Local Time' 'Done'
68 do
69 case $choice in
70 '')
71 echo 'Please enter a number in range.';;
72 'Add Zone')
73 if [ `type -p tzselect` ]; then
74 tzselect >> $TZWATCH
75 else
76 echo tzselect not found. Cannot add time zone.
77 fi
78 # there ought to something we can do here to make sure the zone has not been already be chosen, sort lines and removes duplicates or whatever
79 echo
80 break;;
81
82 'Add Local Time')
83 if [ `type -p tzselect` ]; then
84 echo Local >> $TZWATCH
85 else
86 echo tzselect not found. Cannot add local time.
87 fi
88 break;;
89
90 'Done')
91 break;;
92 ?*)
93 #swapping '/' in zone name to '\/' for sed. I can't believe how many darn \\\\\'s you need!
94 debarredzone=`echo $choice | sed 's|/|\\\/|'`
95 sed -e "\|$debarredzone|d" $TZWATCH | cat > $TZWATCH
96 break;;
97 esac
98 done
99 done;;
100 f) #format string for date output
101 DATEFORMAT=$OPTARG
102 # if the given format has no leading +, then add one
103 if [ -z "$(echo $DATEFORMAT |
104 sed "{s/^[\'\"]//g; s/[\'\"]$//g}" | # strip surrounding quotemarks
105 awk /^\\+/)" ]; then # check for leading +
106 DATEFORMAT=+$DATEFORMAT
107 fi;;
108 ?*)
109 echo Usage: tzwatch [-c] [-f FORMAT]
110 echo -e "Try \`tzwatch --help' for more information."
111 exit 0;;
112 esac
113 done
114
115
116 ##################################################################3
117 # finally, display times
118 # zdump could be used here, but then '-f' formatting would be lost.
119 # Not that I use date formatting, but it's the principle of the matter.
120
121 # wc always prints file name. How annoying. awk it away.
122 NZ=`wc -l $TZWATCH | awk '{print $1}'`
123 n=1
124 until [ $(( $n )) -gt $(( $NZ )) ]; do
125
126 #grab zone from config file
127 zoneRaw=`awk -v nn=$n 'NR==nn { print $1}' $TZWATCH`
128 zoneName=`awk -v nn=$n 'NR==nn { print $2}' $TZWATCH`
129 # allow "local" time zone to be in any case, "Local" or "local" or whatever
130 zone=`echo $zoneRaw | sed s/local/Local/I`
131
132
133 # Create time string.
134 # Sorry about the doubled date calls, it was the only way I could
135 # get the format string handled correctly (without providing a default
136 # format string myself in the case where the user does not give one)
137 if [ -z "$DATEFORMAT" ]; then
138 if [ $zone == "Local" ]; then
139 TZdate=$(LANG=C date)
140 else
141 TZdate=$(LANG=C TZ="$zone" date)
142 fi
143 else
144 if [ $zone == "Local" ]; then
145 TZdate=$(LANG=C date "$DATEFORMAT")
146 else
147 TZdate=$(LANG=C TZ="$zone" date "$DATEFORMAT")
148 fi
149 fi
150
151 # and display
152 # I'd want the zone on the left, but then the date strings would be misaligned
153 # echo -e "$TZdate $zoneName\t\t $zone"
154 printf "%-30s %-15s %-40s\n" "$TZdate" "$zoneName" "$zone"
155
156 # why doesn't '+=' work for incrementing n, like in C?
157 n=$(( $n + 1 ))
158 done