Root/ben-time-set/usr/bin/ben-time-set.sh

1#!/bin/bash
2#
3# Script to facilitate setting the
4# date and time on the Ben NanoNote
5# Copyright 2011 by Warren "Freemor" Pattison
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20# First we set up a few things
21
22VERSION="0.1.2"
23BACKTITLE="Ben NanoNote Time/Date Utility"
24TIMEZONE=""
25TZFILE="/usr/share/ben-time-set/timezones"
26NOTZ=0
27#DATEFORMAT="%Y%m%d"
28#TIMEFORMAT="%H%M"
29setfont /usr/share/kbd/consolefonts/kernel-6x11-font # size down the font so the Calendar widget fits.
30
31# Check For the dialog program
32
33if [ ! -e /usr/bin/dialog ]; then
34   echo "We need the dialog program to do this nicely."
35   echo "please install it with:"
36   echo "opkg install dialog"
37   echo
38   echo "and try again..."
39   exit 1
40fi
41
42# Check that we have a timezones file
43
44if [ ! -e /usr/share/ben-time-set/timezones ]; then
45    if [ -e ./timezones ]; then
46        TZFILE="./timezones"
47    else
48        echo "oops.. no timezones file"
49        echo "We will be unable to set the Timezone."
50        echo "If you want to be able to set the timezone"
51        echo "Please download the timezones file and save"
52        echo "it to the same folder as the script or"
53        echo "/usr/share/ben-time-set/"
54        echo "--"
55        read -p "Press a key to continue" -n1 -s
56        NOTZ=1
57    fi
58fi
59
60echo $TZFILE, $NOTZ
61#exit 0
62
63# Intro and Instructions
64
65dialog --backtitle "$BACKTITLE" --cr-wrap --trim --msgbox "Use this utility to set the time, date\n\
66and timezone on your NanoNote.\n\n\
67Use the TAB key to move between fields.\n\
68use the directional pad to set the value.\n" 0 0
69
70# Set Timezone first as that requires a reboot
71
72if [ $NOTZ == 0 ]; then
73
74    TZ=`cat /etc/TZ`
75
76    # Is timezone right?
77
78    dialog --backtitle "$BACKTITLE" --yesno "Timezone is: "$TZ" \nIs this correct?" 0 0
79
80    if [ "$?" != "0" ]; then
81        dialog --backtitle "$BACKTITLE" --menu "Select your Region:" 0 0 8 Africa "" America "" Antarctica "" Arctic "" Asia "" Atlantic "" Australia "" Europe "" Indian "" Pacific "" 2>/tmp/result
82        if [ "$?" != "0" ]; then
83           exit 1
84        fi
85        ZONES=( $(grep -i $(</tmp/result) "$TZFILE" | cut -f2- -d/) )
86
87        dialog --backtitle "$BACKTITLE" --menu "Select the nearest City:" 0 0 8 `for ITEM in $(seq 0 $((${#ZONES[@]} - 1))); do echo ${ZONES[$ITEM]#*|} ${ZONES[$ITEM]%|*}; done` 2>/tmp/result
88        if [ "$?" != "0" ]; then
89           exit 1
90        fi
91        TIMEZONE=$(</tmp/result)
92
93        #Commit Timezone string to firmware
94        uci set system.@system[0].timezone="$TIMEZONE"
95        uci commit system
96        
97        #Clean-up
98        rm /tmp/result
99        
100        #ASK to reboot... (Pet peeve.. never reboot by default)
101        dialog --backtitle "$BACKTITLE" --yesno "Timezone has been set\nSystem now needs to restart so Linux can adjust.\n\n Reboot?" 0 0
102        if [ "$?" == 0 ]; then
103            reboot
104            exit 0
105        else
106            clear
107            echo -e "Remember to reboot so new Timezone can take effect.\n\n"
108            exit 0
109        fi
110    fi
111
112fi
113
114# Get the Date
115
116dialog --backtitle "$BACKTITLE" --calendar "Set the date" 0 0 2>/tmp/time
117
118# Exit if user chose to cancel
119if [ "$?" != "0" ]; then
120   exit 1
121fi
122
123# Get the Time
124
125dialog --backtitle "$BACKTITLE" --timebox "Set the time" 0 0 2>>/tmp/time
126
127# Exit if user chose to cancel
128if [ "$?" != "0" ]; then
129   exit 1
130fi
131
132# Format the input
133
134DAY=`cut -s -f1 -d '/' /tmp/time`
135MONTH=`cut -s -f2 -d '/' /tmp/time`
136YEAR=`cut -s -f3 -d '/' /tmp/time`
137HOURS=`cut -s -f1 -d ':' /tmp/time`
138MINUTES=`cut -s -f2 -d ':' /tmp/time`
139
140SET=$YEAR$MONTH$DAY$HOURS$MINUTES
141
142echo $SET
143
144
145# Set and apply to internal clock
146
147date $SET
148hwclock --systohc --utc
149dialog --backtitle "$BACKTITLE" --infobox "The time and date have now been\nset and saved.\n\nenjoy" 0 0
150
151
152# Clean Up
153
154rm /tmp/time
155
156

Archive Download this file

Branches:
master



interactive