slitaz-tools view tinyutils/slitaz-config @ rev 930

Finish previous tiny edit
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Nov 25 00:41:01 2014 +0200 (2014-11-25)
parents 0764c1dac3c2
children 5d80f6fdbdb7
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 SliTaz ARM - BSD License
7 # Author: Christophe Lincoln <pankso@slitaz.org>
8 #
9 . /lib/libtaz.sh
10 export TEXTDOMAIN='slitaz-tools' #i18n
12 check_root
14 title="{ $(_ 'SliTaz Config') }"
15 about="/usr/share/doc/slitaz/post-install.txt"
16 tmpdir="/tmp/$(basename $0)"
17 tmp="$tmpdir/$$"
18 height="20"
19 width="72"
21 # Use a tmp directory
22 mkdir -p $tmpdir
24 quit() {
25 rm -rf $tmpdir; exit 0
26 }
29 #
30 # GUI Functions
31 #
33 # Coded for the ARM first boot settings
34 about_post_install() {
35 dialog --cr-wrap \
36 --title "{ $(_ 'Post Installation') }" \
37 --exit-label "$(_ 'Continue')" \
38 --textbox "$about" $height $width
39 }
41 # Set root passwd
42 root_passwd() {
43 dialog --title "{ $(_ 'Root Password') }" --colors \
44 --inputbox "\n$(_ 'Enter new password for \Zb\Z1root')" \
45 12 $width 2>$tmp
46 passwd=$(cat $tmp)
47 [ -z "$passwd" ] && return 0
48 echo "root:$passwd" | chpasswd --md5 >/dev/null
49 }
51 # Add a new user
52 add_user() {
53 title2="{ $(_ 'Add User') }"
55 dialog --title "$title2" --colors \
56 --inputbox "\n$(_ 'Enter login name for the new \Zb\Z4user')" 12 $width 2>$tmp
57 user=$(cat $tmp)
58 [ -z "$user" ] && return 0
60 dialog --title "$title2" --colors \
61 --inputbox "\n$(_ 'Enter password for user \Zb\Z4$user')" 12 $width 2>$tmp
62 passwd=$(cat $tmp)
63 [ -z "$passwd" ] && return 0
65 adduser -D -g "SliTaz User" -G users $user
66 echo "$user:$passwd" | chpasswd --md5 >/dev/null
68 # User groups
69 for group in audio cdrom video tty; do
70 addgroup $user $group >/dev/null
71 done
73 # Make sure system-wide applications.conf is used
74 config="/home/$user/.config"
75 mkdir -p $config/slitaz
76 cp -f /etc/slitaz/applications.conf $config/slitaz
78 # Slim default user on post-install
79 if [ -f "/etc/slim.conf" ] && [ ! -f "/var/lib/slitaz/post-install" ]; then
80 sed -i s"/default_user .*/default_user $user/" /etc/slim.conf
81 fi
82 }
84 set_date() {
85 clear; newline
86 echo "Old date: $(date)"
87 rdate -s tick.greyware.com 2>/dev/null
88 echo "New date: $(date)"
89 sleep 4
90 }
92 # Catch ESSIDs and format output for Ncurses Dialog or GTK Yad tree.
93 # We get the list of networks by Cell and without spaces.
94 # Usage: detect_wifi --output=gtk (default output to dialog)
95 detect_wifi() {
96 . /etc/network.conf
97 ifconfig $WIFI_INTERFACE up
99 if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
100 IFS=$'\n'; hidden=''
101 for i in $(iwlist $WIFI_INTERFACE scan | grep -Eo 'Cell [0-9]*'); do
102 scan=$(iwlist $WIFI_INTERFACE scan last | \
103 awk '/(Cell|ESS|Qual|Encry|IE: WPA|WPA2)/ {print}' | \
104 sed "/$i/,/Cell/ !d")
106 essid=$(echo $scan | cut -d '"' -f 2)
108 quality=$(echo $scan | grep -Eo 'Quality[:=][^ ]*' | tr -cd '0-9/')
109 [ -z "$quality" ] && quality='-----'
111 crypto="$(echo $scan | sed 's/.*key:\([^ ]*\).*/\1/')"
113 # Check encryption type
114 if echo "$scan" | grep -q WPA*; then
115 crypto="WPA"
116 fi
118 # Connected or not connected...
119 if ifconfig | grep -A1 $WIFI_INTERFACE | grep -qF 'inet' && \
120 iwconfig $WIFI_INTERFACE | grep ESSID | grep -qwF "$essid"; then
121 status="..$(_ 'connected')"
122 else
123 status=''
124 fi
126 # Output
127 if [ -z "$essid" ]; then
128 hidden='yes'
129 else
130 case "$output" in
131 gtk)
132 #echo -e "$(_n 'any')\n$(_n 'N/A')\n$(_n 'none')\n$(_n '-')"
133 echo -e "$essid\n$quality\n$crypto\n$status" ;;
134 *)
135 echo "$essid"
136 echo "$(_ 'Quality'):${quality}..$(_ 'Key'):${crypto}${status}" ;;
137 esac
138 fi
139 done
140 [ -n "$hidden" ] && echo "$(_ 'hidden.network')" "$(_ 'Connect.to.a.hidden.network')"
141 fi
142 }
144 # Show message and percentage on the dialog gauge
145 msg() {
146 sleep 1; echo -e "XXX\n$1\n$MSG\nXXX"
147 }
149 # Wireless config so users don't have to edit any config files on post
150 # install to get connected. If the wired connection is used it will auto
151 # connect with DHCP so no need for a dialog frontend.
152 wifi_setup() {
153 . /etc/network.conf
154 dialog \
155 --clear --title "$title" \
156 --ok-label "$(_ 'Select')" \
157 --menu "\n$(_ 'Connect to a Wi-Fi network')" \
158 $height $width 14 \
159 "any" "$(_ 'Connect.to.any.network')" \
160 $(detect_wifi) 2>$tmp
162 # Handle options
163 case "$?" in
164 1|255) quit ;;
165 0) essid=$(cat $tmp) ;;
166 esac
168 # Connect to hidden network
169 if [ "$essid" == "$(_ 'hidden.network')" ]; then
170 dialog --title "{ $(_ 'Wi-Fi ESSID') }" \
171 --inputbox "\n$(_ 'Enter Wi-Fi access point ESSID (name)')" \
172 12 $width 2>$tmp
173 essid=$(cat $tmp)
174 [ -z "$essid" ] && exit 0
176 dialog --title "{ $(_ 'Wi-Fi Password') }" --colors \
177 --inputbox "\n$(_ 'Enter Wi-Fi key (password) for \Zb\Z4$essid')" \
178 12 $width 2>$tmp
179 key=$(cat $tmp)
180 else
182 # Check if we need to prompt user for an encrypted network
183 key=$(iwlist $WIFI_INTERFACE scan last | grep -E 'Cell |Encryption|ESSID' | \
184 grep -C1 "$essid" | sed -n 's|.*key:\(.*\)|\1|p')
186 if [ "$key" == "on" ]; then
187 dialog --title "{ $(_ 'Wi-Fi Password') }" --colors \
188 --inputbox "\n$(_ 'Enter Wi-Fi key (password) for \Zb\Z4$essid')" \
189 12 $width 2>$tmp
190 key=$(cat $tmp)
191 [ -z "$key" ] && exit 0
192 fi
193 fi
195 # Configure connection
196 {
197 MSG="\n$(_ 'Shutting down network interfaces...')"
198 msg 0; stopd network.sh >/dev/null 2>&1
199 msg 10
201 MSG="\n$(_ 'Configuring: /etc/network.conf...')"
202 msg 20
203 # WIFI_KEY_TYPE=any should work for WEP/WPA*
204 sed -i \
205 -e s"/^WIFI=.*/WIFI=\"yes\"/" \
206 -e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$essid\""/ \
207 -e s"/^WIFI_KEY=.*/WIFI_KEY=\"$key\"/" \
208 -e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"any\"/" \
209 /etc/network.conf
211 MSG="\n$(_ 'Restarting Wi-Fi interface...')"
212 msg 30; startd network.sh >/dev/null 2>&1
213 msg 40; msg 50; msg 60; msg 70; msg 80
215 MSG="\n$(_ 'Checking connection...')"
216 msg 90; sleep 2
217 ip="$(ifconfig $WIFI_INTERFACE | fgrep 'inet addr' | sed 's|.*:\([^ ]*\) .*|\1|')"
219 if [ "$ip" ]; then
220 MSG="\n$(_ 'IP address: $ip')"
221 else
222 MSG="\n$(_ 'Unable to connect...')"
223 fi
224 msg 100; sleep 2
226 } | dialog --title "{ $(_ 'Wi-Fi Config') }" --gauge "" 8 $width 0
227 }
229 # Main Dialog menu
230 main_box() {
231 dialog \
232 --clear --title "$title" \
233 --ok-label "$(_ 'Exec')" --cancel-label "$(_ 'Quit')" \
234 --menu "" $height $width 14 \
235 "keyboard" "$(_ 'System keyboard setting')" \
236 "locale" "$(_ 'System language setting')" \
237 "wifi-setup" "$(_ 'Wi-Fi network settings')" \
238 "add-user" "$(_ 'Add a new user')" \
239 "root-passwd" "$(_ 'Change root password')" \
240 "set-date" "$(_ 'Set system date from the web')" \
241 "quit" "$(_ 'Exit from SliTaz Config')" 2>$tmp
243 # Handle options
244 opt="$?"
245 case "$opt" in
246 1|255) quit ;;
247 esac
249 # Handle actions
250 action=$(cat $tmp)
251 case "$action" in
252 keyboard) tazkeymap ;;
253 locale) tazlocale ;;
254 wifi-setup) wifi_setup ;;
255 add-user) add_user ;;
256 root-passwd) root_passwd ;;
257 set-date) set_date ;;
258 quit) quit ;;
259 esac
260 }
262 #
263 # Handle commands
264 #
266 case "$1" in
267 *_*)
268 # Execute functions
269 $@ ;;
270 *)
271 while true; do
272 main_box
273 done ;;
274 esac
276 # Clean exit
277 #rm -rf ${tmpdir}
278 exit 0