slitaz-tools view tinyutils/slitaz-config @ rev 1011
Add sdt SliTaz Distro Tracker
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Sat Feb 11 23:41:23 2017 +0100 (2017-02-11) |
parents | 5d80f6fdbdb7 |
children | 491239328786 |
line source
1 #!/bin/sh
2 #
3 # SliTaz Config - A tool with all SliTaz Ncurses configs in one place for
4 # text mode systems (server, ARM devices)
5 #
6 # Copyright (C) 2014-2015 SliTaz ARM - BSD License
7 # Author: Christophe Lincoln <pankso@slitaz.org>
8 #
10 . /lib/libtaz.sh
11 export TEXTDOMAIN='slitaz-tools' #i18n
13 check_root
15 title="{ $(_ 'SliTaz Config') }"
16 about='/usr/share/doc/slitaz/post-install.txt'
17 tmpdir="/tmp/$(basename $0)"
18 tmp="$tmpdir/$$"
19 height='20'
20 width='72'
23 # Use a tmp directory
25 mkdir -p $tmpdir
27 quit() {
28 rm -rf $tmpdir; exit 0
29 }
32 #
33 # GUI Functions
34 #
37 # Coded for the ARM first boot settings
39 about_post_install() {
40 dialog --cr-wrap \
41 --title "{ $(_ 'Post Installation') }" \
42 --exit-label "$(_ 'Continue')" \
43 --textbox "$about" $height $width
44 }
47 # Set root passwd
49 root_passwd() {
50 dialog --title "{ $(_ 'Root Password') }" --colors \
51 --inputbox "\n$(_ 'Enter new password for %s' '\Zb\Z1root')" \
52 12 $width 2>$tmp
53 passwd=$(cat $tmp)
54 [ -z "$passwd" ] && return 0
55 echo "root:$passwd" | chpasswd --md5 >/dev/null
56 }
59 # Add a new user
61 add_user() {
62 title2="{ $(_ 'Add User') }"
64 dialog --title "$title2" --colors \
65 --inputbox "\n$(_ 'Enter login name for the new \Zb\Z4user')" 12 $width 2>$tmp
66 user=$(cat $tmp)
67 [ -z "$user" ] && return 0
69 dialog --title "$title2" --colors \
70 --inputbox "\n$(_ 'Enter password for user \Zb\Z4%s' "$user")" 12 $width 2>$tmp
71 passwd=$(cat $tmp)
72 [ -z "$passwd" ] && return 0
74 adduser -D -g "SliTaz User" -G users $user
75 echo "$user:$passwd" | chpasswd --md5 >/dev/null
77 # User groups
78 for group in audio cdrom video tty; do
79 addgroup $user $group >/dev/null
80 done
82 # Make sure system-wide applications.conf is used
83 config="/home/$user/.config"
84 mkdir -p $config/slitaz
85 cp -f /etc/slitaz/applications.conf $config/slitaz
87 # Slim default user on post-install
88 if [ -f "/etc/slim.conf" ] && [ ! -f "/var/lib/slitaz/post-install" ]; then
89 sed -i s"/default_user .*/default_user $user/" /etc/slim.conf
90 fi
91 }
94 set_date() {
95 clear; newline
96 echo "Old date: $(date)"
97 rdate -s tick.greyware.com 2>/dev/null
98 echo "New date: $(date)"
99 sleep 4
100 }
103 # Catch ESSIDs and format output for Ncurses Dialog or GTK Yad tree.
104 # We get the list of networks by Cell and without spaces.
105 # Usage: detect_wifi --output=gtk (default output to dialog)
107 detect_wifi() {
108 . /etc/network.conf
109 ifconfig $WIFI_INTERFACE up
111 if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
112 IFS=$'\n'; hidden=''
113 for i in $(iwlist $WIFI_INTERFACE scan | grep -Eo 'Cell [0-9]*'); do
114 scan=$(iwlist $WIFI_INTERFACE scan last | \
115 awk '/(Cell|ESS|Qual|Encry|IE: WPA|WPA2)/ {print}' | \
116 sed "/$i/,/Cell/ !d")
118 essid=$(echo $scan | cut -d '"' -f 2)
120 quality=$(echo $scan | grep -Eo 'Quality[:=][^ ]*' | tr -cd '0-9/')
121 [ -z "$quality" ] && quality='-----'
123 crypto="$(echo $scan | sed 's/.*key:\([^ ]*\).*/\1/')"
125 # Check encryption type
126 if echo "$scan" | grep -q WPA*; then
127 crypto="WPA"
128 fi
130 # Connected or not connected...
131 if ifconfig | grep -A1 $WIFI_INTERFACE | grep -qF 'inet' && \
132 iwconfig $WIFI_INTERFACE | grep ESSID | grep -qwF "$essid"; then
133 status="..$(_ 'connected')"
134 else
135 status=''
136 fi
138 # Output
139 if [ -z "$essid" ]; then
140 hidden='yes'
141 else
142 case "$output" in
143 gtk)
144 #echo -e "$(_n 'any')\n$(_n 'N/A')\n$(_n 'none')\n$(_n '-')"
145 echo -e "$essid\n$quality\n$crypto\n$status" ;;
146 *)
147 echo "$essid"
148 echo "$(_ 'Quality'):${quality}..$(_ 'Key'):${crypto}${status}" ;;
149 esac
150 fi
151 done
152 [ -n "$hidden" ] && echo "$(_ 'hidden.network')" "$(_ 'Connect.to.a.hidden.network')"
153 fi
154 }
157 # Show message and percentage on the dialog gauge
159 msg() {
160 sleep 1; echo -e "XXX\n$1\n$MSG\nXXX"
161 }
164 # Wireless config so users don't have to edit any config files on post
165 # install to get connected. If the wired connection is used it will auto
166 # connect with DHCP so no need for a dialog frontend.
168 wifi_setup() {
169 . /etc/network.conf
170 dialog \
171 --clear --title "$title" \
172 --ok-label "$(_ 'Select')" \
173 --menu "\n$(_ 'Connect to a Wi-Fi network')" \
174 $height $width 14 \
175 "any" "$(_ 'Connect.to.any.network')" \
176 $(detect_wifi) 2>$tmp
178 # Handle options
179 case "$?" in
180 1|255) quit ;;
181 0) essid=$(cat $tmp) ;;
182 esac
184 # Connect to hidden network
185 if [ "$essid" == "$(_ 'hidden.network')" ]; then
186 dialog --title "{ $(_ 'Wi-Fi ESSID') }" \
187 --inputbox "\n$(_ 'Enter Wi-Fi access point ESSID (name)')" \
188 12 $width 2>$tmp
189 essid=$(cat $tmp)
190 [ -z "$essid" ] && exit 0
192 dialog --title "{ $(_ 'Wi-Fi Password') }" --colors \
193 --inputbox "\n$(_ 'Enter Wi-Fi key (password) for \Zb\Z4%s' "$essid")" \
194 12 $width 2>$tmp
195 key=$(cat $tmp)
196 else
198 # Check if we need to prompt user for an encrypted network
199 key=$(iwlist $WIFI_INTERFACE scan last | grep -E 'Cell |Encryption|ESSID' | \
200 grep -C1 "$essid" | sed -n 's|.*key:\(.*\)|\1|p')
202 if [ "$key" == "on" ]; then
203 dialog --title "{ $(_ 'Wi-Fi Password') }" --colors \
204 --inputbox "\n$(_ 'Enter Wi-Fi key (password) for \Zb\Z4%s' "$essid")" \
205 12 $width 2>$tmp
206 key=$(cat $tmp)
207 [ -z "$key" ] && exit 0
208 fi
209 fi
211 # Configure connection
212 {
213 MSG="\n$(_ 'Shutting down network interfaces...')"
214 msg 0; stopd network.sh >/dev/null 2>&1
215 msg 10
217 MSG="\n$(_ 'Configuring: %s...' '/etc/network.conf')"
218 msg 20
219 # WIFI_KEY_TYPE=any should work for WEP/WPA*
220 sed -i \
221 -e s"/^WIFI=.*/WIFI=\"yes\"/" \
222 -e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$essid\""/ \
223 -e s"/^WIFI_KEY=.*/WIFI_KEY=\"$key\"/" \
224 -e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"any\"/" \
225 /etc/network.conf
227 MSG="\n$(_ 'Restarting Wi-Fi interface...')"
228 msg 30; startd network.sh >/dev/null 2>&1
229 msg 40; msg 50; msg 60; msg 70; msg 80
231 MSG="\n$(_ 'Checking connection...')"
232 msg 90; sleep 2
233 ip="$(ifconfig $WIFI_INTERFACE | fgrep 'inet addr' | sed 's|.*:\([^ ]*\) .*|\1|')"
235 if [ -n "$ip" ]; then
236 MSG="\n$(_ 'IP address: %s' "$ip")"
237 else
238 MSG="\n$(_ 'Unable to connect...')"
239 fi
240 msg 100; sleep 2
242 } | dialog --title "{ $(_ 'Wi-Fi Config') }" --gauge "" 8 $width 0
243 }
246 # Main Dialog menu
248 main_box() {
249 dialog \
250 --clear --title "$title" \
251 --ok-label "$(_ 'Exec')" --cancel-label "$(_ 'Quit')" \
252 --menu "" $height $width 14 \
253 "keyboard" "$(_ 'System keyboard setting')" \
254 "locale" "$(_ 'System language setting')" \
255 "wifi-setup" "$(_ 'Wi-Fi network settings')" \
256 "add-user" "$(_ 'Add a new user')" \
257 "root-passwd" "$(_ 'Change root password')" \
258 "set-date" "$(_ 'Set system date from the web')" \
259 "quit" "$(_ 'Exit from SliTaz Config')" 2>$tmp
261 # Handle options
262 opt="$?"
263 case "$opt" in
264 1|255) quit ;;
265 esac
267 # Handle actions
268 action=$(cat $tmp)
269 case "$action" in
270 keyboard) tazkeymap ;;
271 locale) tazlocale ;;
272 wifi-setup) wifi_setup ;;
273 add-user) add_user ;;
274 root-passwd) root_passwd ;;
275 set-date) set_date ;;
276 quit) quit ;;
277 esac
278 }
281 #
282 # Handle commands
283 #
285 case "$1" in
286 *_*)
287 # Execute functions
288 $@ ;;
289 *)
290 while true; do
291 main_box
292 done ;;
293 esac
296 # Clean exit
298 #rm -rf ${tmpdir}
299 exit 0