2011-11-05 03:15:34 +02:00
#!/bin/bash
2011-09-03 19:42:56 +03:00
#
# Script to facilitate setting the
# date and time on the Ben NanoNote
# Copyright 2011 by Warren "Freemor" Pattison
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# First we set up a few things
2011-11-16 18:03:31 +02:00
VERSION = "0.1.2"
2011-09-03 19:42:56 +03:00
BACKTITLE = "Ben NanoNote Time/Date Utility"
TIMEZONE = ""
2011-11-16 18:03:31 +02:00
TZFILE = "/usr/share/ben-time-set/timezones"
NOTZ = 0
2011-09-03 19:42:56 +03:00
#DATEFORMAT="%Y%m%d"
#TIMEFORMAT="%H%M"
2011-09-07 03:33:53 +03:00
setfont /usr/share/kbd/consolefonts/kernel-6x11-font # size down the font so the Calendar widget fits.
2011-09-03 19:42:56 +03:00
# Check For the dialog program
if [ ! -e /usr/bin/dialog ] ; then
echo "We need the dialog program to do this nicely."
echo "please install it with:"
echo "opkg install dialog"
echo
echo "and try again..."
exit 1
fi
2011-11-16 18:03:31 +02:00
# Check that we have a timezones file
if [ ! -e /usr/share/ben-time-set/timezones ] ; then
if [ -e ./timezones ] ; then
TZFILE = "./timezones"
else
echo "oops.. no timezones file"
echo "We will be unable to set the Timezone."
echo "If you want to be able to set the timezone"
echo "Please download the timezones file and save"
echo "it to the same folder as the script or"
echo "/usr/share/ben-time-set/"
echo "--"
read -p "Press a key to continue" -n1 -s
NOTZ = 1
fi
fi
echo $TZFILE , $NOTZ
#exit 0
2011-09-03 19:42:56 +03:00
# Intro and Instructions
dialog --backtitle " $BACKTITLE " --cr-wrap --trim --msgbox " Use this utility to set the time, date\n\
and timezone on your NanoNote.\n \n \
Use the TAB key to move between fields.\n \
use the directional pad to set the value.\n " 0 0
# Set Timezone first as that requires a reboot
2011-11-16 18:03:31 +02:00
if [ $NOTZ = = 0 ] ; then
TZ = ` cat /etc/TZ`
# Is timezone right?
dialog --backtitle " $BACKTITLE " --yesno "Timezone is: " $TZ " \nIs this correct?" 0 0
if [ " $? " != "0" ] ; then
dialog --backtitle " $BACKTITLE " --menu "Select your Region:" 0 0 8 Africa "" America "" Antarctica "" Arctic "" Asia "" Atlantic "" Australia "" Europe "" Indian "" Pacific "" 2>/tmp/result
if [ " $? " != "0" ] ; then
exit 1
fi
ZONES = ( $( grep -i $( </tmp/result) " $TZFILE " | cut -f2- -d/) )
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
if [ " $? " != "0" ] ; then
exit 1
fi
TIMEZONE = $( </tmp/result)
#Commit Timezone string to firmware
uci set system.@system[ 0] .timezone= " $TIMEZONE "
uci commit system
#Clean-up
rm /tmp/result
#ASK to reboot... (Pet peeve.. never reboot by default)
dialog --backtitle " $BACKTITLE " --yesno "Timezone has been set\nSystem now needs to restart so Linux can adjust.\n\n Reboot?" 0 0
if [ " $? " = = 0 ] ; then
reboot
exit 0
else
clear
echo -e "Remember to reboot so new Timezone can take effect.\n\n"
exit 0
fi
2011-11-05 03:15:34 +02:00
fi
2011-09-03 19:42:56 +03:00
2011-11-16 18:03:31 +02:00
fi
2011-11-05 03:15:34 +02:00
2011-09-03 19:42:56 +03:00
# Get the Date
dialog --backtitle " $BACKTITLE " --calendar "Set the date" 0 0 2>/tmp/time
# Exit if user chose to cancel
if [ " $? " != "0" ] ; then
exit 1
fi
# Get the Time
dialog --backtitle " $BACKTITLE " --timebox "Set the time" 0 0 2>>/tmp/time
# Exit if user chose to cancel
if [ " $? " != "0" ] ; then
exit 1
fi
# Format the input
DAY = ` cut -s -f1 -d '/' /tmp/time`
MONTH = ` cut -s -f2 -d '/' /tmp/time`
YEAR = ` cut -s -f3 -d '/' /tmp/time`
HOURS = ` cut -s -f1 -d ':' /tmp/time`
MINUTES = ` cut -s -f2 -d ':' /tmp/time`
SET = $YEAR $MONTH $DAY $HOURS $MINUTES
echo $SET
# Set and apply to internal clock
date $SET
hwclock --systohc --utc
dialog --backtitle " $BACKTITLE " --infobox "The time and date have now been\nset and saved.\n\nenjoy" 0 0
# Clean Up
rm /tmp/time