358b398359774c18e5df232b4e0f3fb66b86e20d
[openpandora.oe.git] / recipes / pandora-system / pandora-scripts / op_datetime.sh
1 #!/bin/sh
2
3 # Set the timezone and date/time
4
5 while mainsel=$(zenity --title="Date / Time / Timezone" --width="400" --height="250" --list --column "id" --column "Please select" --hide-column=1 --text="You can set the time and date or select a different timezone.\n" "td" "Change Time and Date" "tz" "Select Timezone" "sync" "Sync time over Internet" "autosync" "Sync on network connection" --ok-label="Change Setting" --cancel-label="Exit"); do
6
7
8 case $mainsel in
9
10   "tz")
11
12     while ! area=$(zenity --list --title "Select your time zone" --text="Please select your area" --column="Select your area" --print-column=1 "Africa" "America" "Asia" "Australia" "Europe" "Pacific" --width=500 --height=260) || [ "x$area" = "x" ] ; do
13           zenity --title="Error" --error --text="Please select your area." --timeout=6
14     done
15
16     while ! timezone=$(ls -1 /usr/share/zoneinfo/$area | zenity ---width=500 --height=200 --title="Select your closest location" --list --column "Closest Location" --text "Please select the location closest to you") || [ "x$timezone" = "x" ] ; do
17            zenity --title="Error" --error --text="Please select your location." --timeout=6
18     done
19
20     echo $timezone
21     rm /etc/localtime && ln -s /usr/share/zoneinfo/$area/$timezone /etc/localtime
22     echo $area/$timezone >> /etc/timezone
23   ;;
24
25   "td")
26
27   #Make sure we clean up any leading zeros in the day (as Zenity freaks out)
28   date_d=`date +%d | sed 's/^0//'`
29   date_m=`date +%m | sed 's/^0//'`
30   date_y=`date +%Y`
31
32   date=""
33   while [ "x$date" = "x" ] ; do
34     date=$(zenity --calendar --text="Please select the current date" --title "Please select the current date" --day=$date_d --month=$date_m --year=$date_y --date-format="%Y%m%d" --width=500) || exit 1
35   done
36
37   echo $date
38
39   time_h=`date +%H`
40   time_m=`date +%M`
41
42   while true; do
43     time=$(zenity --title="Enter actual time" --entry --text "Please enter the time in 24hour format (HH:MM).\n" --entry-text "$time_h:$time_m") || exit 1
44     if test -n "$time" && date -d "$time"; then
45       break;
46     fi
47     zenity --title="Error" --error --text="Please input the time." --timeout 6
48   done
49
50   # take care of screensaver first
51   screensaver_enabled=true
52   if xset q | grep -A2 'Screen Saver' | grep -q 'timeout:.*\<0\>.*cycle'; then
53     screensaver_enabled=false
54   fi
55   xset s off
56
57   date +%Y%m%d -s $date
58   date +%H:%M -s $time
59   hwclock -u -w
60
61   if $screensaver_enabled; then
62     xset s on
63   fi
64   ;;
65   "sync")
66   screensaver_enabled=true
67   if xset q | grep -A2 'Screen Saver' | grep -q 'timeout:.*\<0\>.*cycle'; then
68     screensaver_enabled=false
69   fi
70   xset s off
71   (
72   test -e /etc/init.d/ntpd && sudo /etc/init.d/ntpd stop
73   sudo ntpdate pool.ntp.org
74   ) |
75         zenity --progress \
76         --title="Syncing..." \
77         --text="Syncing with time server...\nPlease wait a while..." \
78         --pulsate
79   test -e /etc/init.d/ntpd && sudo /etc/init.d/ntpd start
80   if $screensaver_enabled; then
81     xset s on
82   fi
83   ;;
84   "autosync")
85     if zenity --question --title="Autosync time" --text="The Pandora can automatically sync the time whenever you connect to a network.\n\nShould it do that?" --ok-label="Yes" --cancel-label="No"; then
86       chmod +x /etc/NetworkManager/dispatcher.d/ntpdate.sh
87     else
88       chmod -x /etc/NetworkManager/dispatcher.d/ntpdate.sh
89     fi  
90   ;;
91 esac
92 done