@@ -265,11 +265,7 @@ set_timezone_and_locales() {
265265 PUBLIC_IP=$( curl --max-time 5 -s https://ipinfo.io/ip)
266266
267267 # Check if we have wireless adaptor
268- if command -v nmcli > /dev/null 2>&1 ; then
269- WIFI_DEVICE=$( LC_ALL=C nmcli dev status | grep " wifi " 2> /dev/null)
270- elif command -v iw > /dev/null 2>&1 ; then
271- WIFI_DEVICE=$( LC_ALL=C iw dev | awk ' $1=="Interface"{print $2}' 2> /dev/null)
272- fi
268+ WIFI_DEVICE=$( LC_ALL=C iw dev | awk ' $1=="Interface"{print $2}' 2> /dev/null)
273269
274270 if [ -z " $PUBLIC_IP " ]; then
275271
@@ -289,49 +285,70 @@ set_timezone_and_locales() {
289285 if [[ " ${response} " =~ ^(Y| y)$ ]]; then
290286
291287 # get list of wireless networks
292- echo -e " \nDetected wireless networks:\n"
293- ARRAY=($( sudo iwlist ${WIFI_DEVICE} scanning | egrep ' ESSID' | sed ' s/^[ \t]*//' | sed ' s/"//g' | sed ' s/ESSID://' | awk ' BEGIN{FS=OFS=","} {$NF=++count OFS $NF} 1' ) )
294- while [[ 1 ]] ; do
295- while [[ 1 ]] ; do
296- for str in ${ARRAY[@]} ; do echo $str | sed " s/,/ \t /g" ; done
297- read -r -p " Enter a number of SSID: " input
298- if [[ " $input " =~ ^[0-9]{,2}$ ]] ; then break ; fi
299- done
300- echo " "
301- # get password
302- while [[ 1 ]] ; do
303- SSID=$( echo ${ARRAY[$input-1]} | cut -d" ," -f2)
304- read -r -p " Enter a password for ${SSID} : " password
305- break
306- done
307-
308- # generate config
309- cat << - EOF > "${SDCARD} "/etc/netplan/30-wifis-dhcp.yaml
310- # Created by Armbian firstlogin script
311- network:
312- wifis:
313- ${WIFI_DEVICE} :
314- dhcp4: yes
315- dhcp6: yes
316- access-points:
317- "$SSID ":
318- password: "${password} "
319- EOF
320-
321- # apply to netplan
322- systemctl daemon-reload
323- netplan apply --timeout 0 2> /dev/null
324- sleep 5
325-
326- # exit if connection is suffesful
327- if [[ -n $( sudo iw ${WIFI_DEVICE} link | grep " $SSID " ) ]]; then break ; fi
288+ scanning=0
289+ broken=1
290+ while [[ ${scanning} -lt 3 ]]; do
291+ sleep 0.5
292+ scanning=$(( scanning + 1 ))
293+ ARRAY=($( iwlist ${WIFI_DEVICE} scanning 2> /dev/null | egrep ' ESSID' | sed ' s/^[ \t]*//' | sed ' s/"//g' | sed ' s/ESSID://' | awk ' BEGIN{FS=OFS=","} {$NF=++count OFS $NF} 1' ) )
294+ if [[ $? == 0 ]]; then broken=0; break ; fi
328295 done
296+ # wifi can also fail
297+ if [[ ${broken} == 1 ]]; then
298+ echo -e " \nWireless connection was \x1B[91mnot detected\x1B[0m.\n"
299+ else
300+ echo -e " \nDetected wireless networks:\n"
301+ scanning=0
302+ broken=1
303+ while [[ ${scanning} -lt 3 ]]; do
304+ scanning=$(( scanning + 1 ))
305+ while [[ 1 ]] ; do
306+ for str in ${ARRAY[@]} ; do echo $str | sed " s/,/ \t /g" ; done
307+ echo " "
308+ read -r -p " Enter a number of SSID: " input
309+ if [[ " $input " =~ ^[0-9]{,2}$ ]] ; then break ; fi
310+ done
311+ # get password
312+ while [[ 1 ]] ; do
313+ SSID=$( echo ${ARRAY[$input-1]} | cut -d" ," -f2)
314+ echo " "
315+ read -r -p " Enter a password for ${SSID} : " password
316+ break
317+ done
318+
319+ # generate config
320+ cat << - EOF > "${SDCARD} "/etc/netplan/30-wifis-dhcp.yaml
321+ # Created by Armbian firstlogin script
322+ network:
323+ wifis:
324+ ${WIFI_DEVICE} :
325+ dhcp4: yes
326+ dhcp6: yes
327+ access-points:
328+ "$SSID ":
329+ password: "${password} "
330+ EOF
331+ chmod 600 /etc/netplan/30-wifis-dhcp.yaml
332+
333+ # apply to netplan
334+ systemctl daemon-reload
335+ netplan apply --timeout 0 2> /dev/null
336+ sleep 5
337+
338+ # exit if connection is suffesful
339+ if [[ -n $( iw " ${WIFI_DEVICE} " link 2> /dev/null | grep " $SSID " ) ]]; then broken=0; break ; fi
340+ done
341+ if [[ ${broken} == 1 ]]; then
342+ echo -e " \n\x1B[91mUnable to connect to Access Point\x1B[0m.\n"
343+ fi
344+ fi # detected or not detected wireless network
329345 fi
330346 echo " "
331347 fi
332348 fi
333349
334350 # Grab IP once again if not found
351+ sleep 3
335352 [[ -z " $PUBLIC_IP " && -n " $WIFI_DEVICE " ]] && PUBLIC_IP=$( curl --max-time 5 -s https://ipinfo.io/ip)
336353
337354 # Call the geolocation API and capture the output
@@ -346,15 +363,17 @@ set_timezone_and_locales() {
346363
347364 TZDATA=$( echo " ${RES} " | cut -d" ," -f1)
348365 CCODE=$( echo " ${RES} " | cut -d" ," -f3 | xargs)
349- echo -e " Detected timezone: \x1B[92m$TZDATA \x1B[0m"
350- echo " "
366+
351367 unset response
352368 while [[ ! " ${response} " =~ ^(Y| y| N| n)$ ]]; do
353- if [ -z $SET_LANG_BASED_ON_LOCATION ]; then
369+ if [ -z " ${SET_LANG_BASED_ON_LOCATION} " ] && [ -n " ${TZDATA} " ]; then
370+ echo -e " Detected timezone: \x1B[92m$TZDATA \x1B[0m"
371+ echo " "
354372 read -r -p " Set user language based on your location? [Y/n] " response
355373 response=${response:- Y}
356374 else
357375 response=$SET_LANG_BASED_ON_LOCATION
376+ break
358377 fi
359378 done
360379 # change it only if we have a match and if we agree
@@ -367,13 +386,11 @@ set_timezone_and_locales() {
367386 # UTF8 is not present everywhere so check again in case it returns empty value
368387 [[ -z " $LOCALES " ]] && LOCALES=$( grep territory /usr/share/i18n/locales/* | grep _" $CCODE " | cut -d " :" -f 1 | cut -d " /" -f 6 |
369388 xargs -I{} grep {} /usr/share/i18n/SUPPORTED | cut -d " " -f 1)
370-
371389 readarray -t options <<< " ${LOCALES}"
372390
373391 if [ -z $PRESET_LOCALE ]; then
374392 # when having more locales, prompt for choosing one
375393 if [[ " ${# options[@]} " -gt 1 ]]; then
376-
377394 options+=(" Skip generating locales" )
378395 echo -e " \nAt your location, more locales are possible:\n"
379396 PS3=' Please enter your choice:'
0 commit comments