1
0
mirror of git://projects.qi-hardware.com/nanobits.git synced 2024-11-27 18:15:18 +02:00
nanobits/ben-time-set/ben-time-set.sh

120 lines
3.3 KiB
Bash
Raw Normal View History

#!/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
VERSION="0.0.5"
BACKTITLE="Ben NanoNote Time/Date Utility"
TIMEZONE=""
#DATEFORMAT="%Y%m%d"
#TIMEFORMAT="%H%M"
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
# 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
TZ=`cat /etc/TZ`
2011-09-03 19:42:56 +03:00
# Is timezone right?
dialog --backtitle "$BACKTITLE" --yesno "Timezone is: "$TZ" \nIs this correct?" 0 0
2011-09-03 19:42:56 +03:00
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
ZONES=( $(grep -i $(</tmp/result) timezones | 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
TIMEZONE=$(</tmp/result)
#echo $TIMEZONE
#exit 1
#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 will now restart so Linux can adjust.\n\n Reboot?" 0 0
if [ "$?" == 0 ]; then
reboot
exit 0
else
exit 0
fi
2011-09-03 19:42:56 +03:00
fi
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