-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncTime.sh
executable file
·106 lines (75 loc) · 2.1 KB
/
syncTime.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
#
#-------------------------------------------------
# file: syncTime.sh
#
# This script syncronizes system and RTC time
#-------------------------------------------------
#-----------------------
# check if sudo is used
#-----------------------
if [ "$(id -u)" != 0 ]; then
echo 'ERROR: this script must be run with effective root UUID'
exit 1
fi
#--------------------------------
# delay if first argument exists
#--------------------------------
if [ ! -z "$1" ]; then
sleep $1
fi
#--------------------------------------------
# include utilities script in same directory
#--------------------------------------------
my_dir="`dirname \"$0\"`"
my_dir="`( cd \"$my_dir\" && pwd )`"
if [ -z "$my_dir" ] ; then
exit 1
fi
. $my_dir/utilities.sh
#-------------------------
# is RTC board connected?
#-------------------------
log 'Synchronizing RTC and system time'
#--------------
# get RTC time
#--------------
rtctime="$(get_rtc_time)"
echo RTC time is $rtctime
#----------------------------------------------
# if RTC time is OK, write RTC time to system
#----------------------------------------------
if [[ $rtctime != *"1999"* ]] && [[ $rtctime != *"2000"* ]]; then
rtc_to_system
fi
#---------------------------------------
# wait a moment for Internet connection
#---------------------------------------
sleep 10
if $(has_internet) ; then
#--------------------------
# new time from NTP server
#--------------------------
log 'Internet detected, apply NTP time to system and Witty Pi'
force_ntp_update
system_to_rtc
else
#-----------------
# get system year
#-----------------
sysyear="$(date +%Y)"
if [[ $rtctime == *"1999"* ]] || [[ $rtctime == *"2000"* ]]; then
#-----------------------------------------
# This is the first time RTC has been set
#-----------------------------------------
log 'RTC time has not been set previously'
if [[ $sysyear != *"1969"* ]] && [[ $sysyear != *"1970"* ]]; then
#---------------------
# System time is good
#---------------------
system_to_rtc
else
log 'Neither system nor Witty Pi has correct time'
fi
fi
fi