-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzone_auto
More file actions
executable file
·145 lines (125 loc) · 4 KB
/
zone_auto
File metadata and controls
executable file
·145 lines (125 loc) · 4 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
# Default timezone for --default option
DEFAULT_TZ="Asia/Tehran"
# Function to display help
display_help() {
echo "Usage: $0 [OPTION]"
echo
echo "Automatically detect and update timezone based on IP address location."
echo
echo "Options:"
echo " -h, --help Display this help message and exit"
echo " --default Set timezone to $DEFAULT_TZ without detection"
echo
echo "When run without options, the script will:"
echo "1. Detect your current IP address and location"
echo "2. Show your country and capital city"
echo "3. Compare with current system timezone"
echo "4. Prompt to update timezone if different"
exit 0
}
# Function to get current public IP information
get_ip_info() {
echo "Detecting your IP information..."
ip_info=$(curl -s https://ipinfo.io/json)
if [ -z "$ip_info" ]; then
echo "Error: Could not fetch IP information. Check your internet connection."
exit 1
fi
ip=$(echo "$ip_info" | jq -r '.ip')
country=$(echo "$ip_info" | jq -r '.country')
city=$(echo "$ip_info" | jq -r '.city')
timezone=$(echo "$ip_info" | jq -r '.timezone')
echo "----------------------------------------"
echo "Your current IP address: $ip"
echo "Country: $country"
echo "City: $city"
echo "Detected timezone: $timezone"
echo "----------------------------------------"
}
# Function to get the current system timezone
get_current_timezone() {
if [ -f /etc/localtime ]; then
current_tz=$(readlink /etc/localtime | sed 's|/usr/share/zoneinfo/||')
if [ -z "$current_tz" ]; then
current_tz=$(timedatectl | grep "Time zone" | awk '{print $3}')
fi
echo "$current_tz"
else
current_tz=$(timedatectl | grep "Time zone" | awk '{print $3}')
echo "$current_tz"
fi
}
# Function to change timezone
change_timezone() {
local new_tz=$1
echo "Changing timezone to $new_tz..."
# Check if the timezone file exists
if [ ! -f "/usr/share/zoneinfo/$new_tz" ]; then
echo "Error: Timezone $new_tz not found in /usr/share/zoneinfo/"
return 1
fi
# Change the timezone
sudo unlink /etc/localtime 2>/dev/null
sudo ln -s "/usr/share/zoneinfo/$new_tz" /etc/localtime
# Verify the change
current_tz=$(get_current_timezone)
if [ "$current_tz" == "$new_tz" ]; then
echo "Timezone successfully changed to $new_tz"
echo "Current time: $(date)"
else
echo "Warning: Timezone change might not have worked correctly."
echo "Current timezone is: $current_tz"
fi
}
# Check if jq is installed (for JSON parsing)
check_dependencies() {
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed. Please install it first."
echo "For Debian/Ubuntu: sudo apt-get install jq"
echo "For RHEL/CentOS: sudo yum install jq"
exit 1
fi
if ! command -v curl &> /dev/null; then
echo "Error: curl is not installed. Please install it first."
exit 1
fi
}
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help)
display_help
;;
--default)
change_timezone "$DEFAULT_TZ"
exit 0
;;
*)
echo "Unknown parameter: $1"
display_help
exit 1
;;
esac
shift
done
# Main script
check_dependencies
get_ip_info
current_tz=$(get_current_timezone)
echo "Current system timezone: $current_tz"
if [ "$timezone" == "$current_tz" ]; then
echo "Your timezone is already set correctly for your detected location."
exit 0
fi
echo "Your timezone ($current_tz) doesn't match your detected location ($timezone)."
read -p "Do you want to change your timezone to $timezone? [Y/n] " answer
answer=${answer:-Y} # Default to Y if empty
case ${answer^^} in
Y|YES|"")
change_timezone "$timezone"
;;
*)
echo "Timezone not changed."
;;
esac