debian-config-functions 21 KB
Newer Older
1
2
#!/bin/bash
#
3
# Copyright (c) Authors: http://www.armbian.com/authors, info@armbian.com
4
5
6
7
8
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.

9
10
# Functions:
# main
11
# check_desktop
12
# exceptions
13
14
15
16
17
18
# check_if_installed
# is_package_manager_running
# display_qr_code
# beta_disclaimer
# show_box
# description
19
# reload_bsp
20
# other_kernel_version
21
22
23
24
25
# aval_dtbs
# get_a20modes
# get_h3modes
# add_choose_user
# configure_desktop
26

27

28
#
29
30
# gather info about the board and start with loading menu
#
31
function main(){
32

33
34
	DIALOG_CANCEL=1
	DIALOG_ESC=255
35

36
37
38
39
40
	[[ -f /etc/armbian-release ]] && source /etc/armbian-release && ARMBIAN="Armbian $VERSION $IMAGE_TYPE";
	DISTRO=$(lsb_release -is)
	DISTROID=$(lsb_release -sc)
	KERNELID=$(uname -r)
	[[ -z "${ARMBIAN// }" ]] && ARMBIAN="$DISTRO $DISTROID"
Igor Pečovnik's avatar
bugfix    
Igor Pečovnik committed
41
	DEFAULT_ADAPTER=$(ip -4 route ls | grep default | tail -1 | grep -Po '(?<=dev )(\S+)')
Igor Pecovnik's avatar
Igor Pecovnik committed
42
	LOCALIPADD=$(ip -4 addr show dev $DEFAULT_ADAPTER | awk '/inet/ {print $2}' | cut -d'/' -f1)
43
44
	BACKTITLE="Configuration utility, $ARMBIAN"
	[[ -n "$LOCALIPADD" ]] && BACKTITLE=$BACKTITLE", "$LOCALIPADD
Igor Pecovnik's avatar
Igor Pecovnik committed
45
	TITLE="$BOARD_NAME "
46
47
48
	[[ -z "${DEFAULT_ADAPTER// }" ]] && DEFAULT_ADAPTER="lo"
	OVERLAYDIR="/boot/dtb/overlay";
	[[ "$LINUXFAMILY" == "sunxi64" ]] && OVERLAYDIR="/boot/dtb/allwinner/overlay";
49
	[[ "$LINUXFAMILY" == "meson64" ]] && OVERLAYDIR="/boot/dtb/amlogic/overlay";
50
	# detect desktop
51
	check_desktop
52
53
	dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nLoading Armbian configuration utility ... " 5 45
	sleep 1
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
#
# compare two strings in dot separated version format
#
vercomp () {
    if [[ $1 == $2 ]]
    then
        return 0
    fi
    local IFS=.
    local i ver1=($1) ver2=($2)
    # fill empty fields in ver1 with zeros
    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
    do
        ver1[i]=0
    done
    for ((i=0; i<${#ver1[@]}; i++))
    do
        if [[ -z ${ver2[i]} ]]
        then
            # fill empty fields in ver2 with zeros
            ver2[i]=0
        fi
        if ((10#${ver1[i]} > 10#${ver2[i]}))
        then
            return 1
        fi
        if ((10#${ver1[i]} < 10#${ver2[i]}))
        then
            return 2
        fi
    done
    return 0
}




#
# test compare two strings $1="3.4.12" $2="5.4.2" $3="<" returns 0 if relation is correct
#
testvercomp () {
    vercomp $1 $2
    case $? in
        0) op='=';;
        1) op='>';;
        2) op='<';;
    esac
    if [[ $op != $3 ]]
    then
		return 1
    else
		return 0
    fi
}



116

117
118
119
120
121
122
123
124
#
# read desktop parameters
#
function check_desktop()
{
DISPLAY_MANAGER=""; DESKTOP_INSTALLED=""
check_if_installed nodm && DESKTOP_INSTALLED="nodm";
check_if_installed lightdm && DESKTOP_INSTALLED="lightdm";
125
check_if_installed lightdm && DESKTOP_INSTALLED="gnome";
126
127
[[ -n $(service lightdm status 2> /dev/null | grep -w active) ]] && DISPLAY_MANAGER="lightdm"
[[ -n $(service nodm status 2> /dev/null | grep -w active) ]] && DISPLAY_MANAGER="nodm"
128
[[ -n $(service gdm status 2> /dev/null | grep -w active) ]] && DISPLAY_MANAGER="gdm"
129
130
131
}


132
133


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#
# naming exceptions for packages
#
function exceptions ()
{

	TARGET_FAMILY=$LINUXFAMILY
	UBOOT_BRANCH=$TARGET_BRANCH # uboot naming is different

	if [[ $TARGET_BRANCH == "default" ]]; then TARGET_BRANCH=""; else TARGET_BRANCH="-"$TARGET_BRANCH; fi
	# pine64
	if [[ $TARGET_FAMILY == pine64 ]]; then
		TARGET_FAMILY="sunxi64"
	fi
	# allwinner legacy kernels
	if [[ $TARGET_FAMILY == sun*i ]]; then
		TARGET_FAMILY="sunxi"
		if [[ $UBOOT_BRANCH == "default" ]]; then
			TARGET_FAMILY=$(cat /proc/cpuinfo | grep "Hardware" | sed 's/^.*Allwinner //' | awk '{print $1;}')
		fi
	fi

}




161
#
JC Staudt's avatar
JC Staudt committed
162
# check dpkg status of $1 -- currently only 'not installed at all' case caught
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
check_if_installed (){

	local DPKG_Status="$(dpkg -s "$1" 2>/dev/null | awk -F": " '/^Status/ {print $2}')"
	if [[ "X${DPKG_Status}" = "X" || "${DPKG_Status}" = *deinstall* ]]; then
		return 1
	else
		return 0
	fi
}




#
178
179
180
# check if package manager is doing something
#
function is_package_manager_running() {
181

182
183
184
185
  fuser -s /var/lib/dpkg/lock
  if [[ $? = 0 ]]; then
    # 0 = true
	dialog --colors --title " \Z1Error\Z0 " --backtitle "$BACKTITLE" --no-collapse --msgbox \
root's avatar
root committed
186
	"\n\Z0Package manager is running in the background. \n\nCan't install dependencies. Try again later." 9 53
187
188
189
190
191
    return 0
  else
    # 1 = false
    return 1
  fi
192
193
194
195
196

}



Igor Pecovnik's avatar
Igor Pecovnik committed
197
198
199
200
201
202
203
204
205
206
207
#
# wget with dialog progress bar $1=URL $2=parameters
#
function fancy_wget()
{
LANG=C wget $2 --progress=bar:force:noscroll $1 2>&1 | stdbuf -i0 -o0 -e0 tr '>' '\n' | \
stdbuf -i0 -o0 -e0 sed -rn 's/^.*\<([0-9]+)%\[.*$/\1/p' | dialog --backtitle "$BACKTITLE" --title " Downloading " \
--gauge "Please wait" 7 70 0
}


208

209
#
JC Staudt's avatar
JC Staudt committed
210
# display qr code for authentication method
211
212
213
214
215
216
217
#
function display_qr_code()
{
		clear
		SECRET=$(head -1 /root/.google_authenticator)
		qrencode -d 9 -8 -t UTF8 "otpauth://totp/test?secret=$SECRET"
		echo -e "\nSetting up your OTP-generator\
JC Staudt's avatar
JC Staudt committed
218
219
220
		\nInstall a one-time password authenticator on your mobile device (e.g. FreeOTP) from the Android market or F-Droid.\
		\nIn the application menu, click the corresponding button to create a new account and either scan the QR code or enter the secret key manually:\
		\n\n$SECRET \n\nYou should now see a new passcode token being generated every 60 seconds on your phone.\n"  | fold -sw 38
221
222
223
224
225
226
		read -n 1 -s -r -p "Press any key to continue"
}




227
228
229
230
231
232
233
#
# show disclaimer
#
function beta_disclaimer ()
{
exec 3>&1
	ACKNOWLEDGEMENT=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse --title " Warning " \
234
	--clear \--radiolist "\n$1\n \n" 11 56 5 "Yes, I understand" "" off	 2>&1 1>&3)
235
exec 3>&-
236
237
}

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267



#
# show box
#
function show_box ()
{
	dialog --colors --backtitle "$BACKTITLE" --no-collapse --title " $1 " --clear --msgbox "\n$2\n \n" $3 56
}




#
# show description for MOTD files
#
function description
{
	case $1 in
		*header*)
			echo "Big board logo and kernel info"
		;;
		*sysinfo*)
			echo "Sysinfo - load, ip, memory, uptime, ..."
		;;
		*tips*)
			echo "Shows tip of the day"
		;;
		*updates*)
Chris Rohlfs's avatar
Chris Rohlfs committed
268
			echo "Display number of available updates"
269
270
271
272
273
274
275
276
277
278
		;;
		*armbian-config*)
			echo "Show command for system configuration"
		;;
		*autoreboot-warn*)
			echo "Show warning when reboot is needed"
		;;
		*uk.armbian.com*)
			echo "United Kingdom"
		;;
Igor Pečovnik's avatar
Igor Pečovnik committed
279
280
281
		*mirrors.tuna.tsinghua.edu.cn/armbian/*)
			echo "China"
		;;
Igor Pečovnik's avatar
bugfix    
Igor Pečovnik committed
282
		*mirrors.netix.net/armbian/apt/*)
283
			echo "Bulgarija"
Igor Pečovnik's avatar
Igor Pečovnik committed
284
285
286
287
		;;		
		*mirrors.dotsrc.org/armbian-apt/*)
			echo "Denmark"
		;;		
288
289
290
291
292
293
294
295
296
297
298
299
		*.armbian.com*)
			echo "Estonia"
		;;
		*)
		echo ""
		;;
	esac
}




300
301
302
303
304
305
306
307
308
309
310
311
#
# kernel descriptions in more human friendly format
#
function kernel_desc ()
{
        [[ "$1" == "dev" ]]     && echo "development, unstable"
        [[ "$1" == "next" ]]    && echo "mainline or 2nd generation"
        [[ "$1" == "default" ]] && echo "legacy, stock, 1st build"
}



312
313
314
315
316
317
318
319
#
# reload kernel, bsp and armbian-config
#
function reload_bsp(){
        debconf-apt-progress -- apt-get update
        # test install packages
        TARGET_BRANCH=$BRANCH
        exceptions "$INSTALL_KERNEL"
Igor Pečovnik's avatar
Igor Pečovnik committed
320
321
322
323
324
325
326
327
328
329
330
        dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nProbing test install ..." 5 28
		unset PACKAGE_LIST

		[[ -n $(apt-cache search --names-only "^armbian-${LINUXFAMILY}$") ]] && PACKAGE_LIST=$PACKAGE_LIST" armbian-${LINUXFAMILY}"
		[[ -n $(apt-cache search --names-only "^armbian-${BOARD}$") ]] && PACKAGE_LIST=$PACKAGE_LIST" armbian-${BOARD}"
		[[ -n $(apt-cache search --names-only "^armbian-${DISTROID}$") ]] && PACKAGE_LIST=$PACKAGE_LIST" armbian-${DISTROID}"
		[[ -n $(apt-cache search --names-only "^armbian-${DISTROID}-desktop-xfce$") ]] && PACKAGE_LIST=$PACKAGE_LIST" armbian-${DISTROID}-desktop-xfce"

		# if new packages are absent, reinstall old ones
		if [[ -z $PACKAGE_LIST ]]; then
			PACKAGE_LIST="linux-${DISTROID}-root$TARGET_BRANCH-$BOARD"
331
			check_if_installed armbian-${DISTROID}-desktop && PACKAGE_LIST=$PACKAGE_LIST" armbian-${DISTROID}-desktop"
Igor Pečovnik's avatar
Igor Pečovnik committed
332
333
334
335
		fi

		# reinstall headers only if they are already installed
		check_if_installed linux-headers${TARGET_BRANCH}-${TARGET_FAMILY} && PACKAGE_LIST=$PACKAGE_LIST" linux-headers${TARGET_BRANCH}-${TARGET_FAMILY}"
336

Igor Pečovnik's avatar
Igor Pečovnik committed
337
338
339
340
341
		IFS=" "
		[[ -n $(apt-cache search --names-only "^linux-dtb$TARGET_BRANCH-$TARGET_FAMILY") ]] && PACKAGE_LIST=$PACKAGE_LIST" linux-dtb$TARGET_BRANCH-$TARGET_FAMILY"
		PACKAGE_LIST=$PACKAGE_LIST" linux-image${TARGET_BRANCH}-${TARGET_FAMILY}"
        apt-get -s -y --no-install-recommends install $PACKAGE_LIST > /tmp/reload_bsp.log 2>&1

342
343
344
        # if test download is ok, remove old packages
        if [[ $? = 0 ]]; then
                dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nRemoving current kernel ..." 5 36
Igor Pečovnik's avatar
Igor Pečovnik committed
345
                aptitude remove ~nlinux-image*${LINUXFAMILY} --quiet=100 -y >> /var/log/upgrade.log 2>&1
346
347
                aptitude remove ~nlinux-dtb --quiet=100 -y >> /var/log/upgrade.log 2>&1
                aptitude remove ~nlinux-headers --quiet=100 -y >> /var/log/upgrade.log 2>&1
Igor Pečovnik's avatar
Igor Pečovnik committed
348
                aptitude remove ~nlinux-${DISTROID}-root --quiet=100 -y >> /var/log/upgrade.log 2>&1
349
                aptitude remove ~narmbian-config --quiet=100 -y >> /var/log/upgrade.log 2>&1
Igor Pečovnik's avatar
Igor Pečovnik committed
350
351
352
				aptitude remove ~narmbian-${LINUXFAMILY} --quiet=100 -y >> /var/log/upgrade.log 2>&1
				aptitude remove ~narmbian-${BOARD} --quiet=100 -y >> /var/log/upgrade.log 2>&1
				aptitude remove ~narmbian-${DISTROID} --quiet=100 -y >> /var/log/upgrade.log 2>&1
353
				aptitude remove ~narmbian-${DISTROID}-desktop --quiet=100 -y >> /var/log/upgrade.log 2>&1
Igor Pečovnik's avatar
Igor Pečovnik committed
354
				aptitude remove ~narmbian-${DISTROID}-desktop-xfce --quiet=100 -y >> /var/log/upgrade.log 2>&1
355
                # install packages
Igor Pečovnik's avatar
Igor Pečovnik committed
356
				echo $PACKAGE_LIST >> /var/log/upgrade.log
357
                debconf-apt-progress -- apt-get --reinstall -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
Igor Pečovnik's avatar
Igor Pečovnik committed
358
                -y -qq --no-install-recommends install $PACKAGE_LIST armbian-config
359
                if [[ $? = 1 ]]; then echo "Something went wrong ... check logs."; exit; fi
360
                dialog --title "Switching to $1" --backtitle "$BACKTITLE" --yes-label "Reboot" \
Igor Pečovnik's avatar
Igor Pečovnik committed
361
				--no-label "Cancel" --yesno "\nReboot to apply new settings?" 7 34
362
                if [[ $? = 0 ]]; then reboot; fi
Igor Pečovnik's avatar
Igor Pečovnik committed
363
364
		else
			dialog --backtitle "$BACKTITLE" --title "Warning" --msgbox "\nTest install failed. Can't change firmware \n\nCheck /tmp/reload_bsp.log" 9 48
365
366
367
368
        fi
}


369
370
371
function other_kernel_version ()
{

Igor Pečovnik's avatar
Igor Pečovnik committed
372
373
374
375
376
377
	IFS=$'\r\n'
	GLOBIGNORE='*'

	# get current kernel information
	CURRENT_VERSION_TEMP=$(dpkg -l | grep '^ii' | grep linux-image)
	CURRENT_VERSION=$(echo $CURRENT_VERSION_TEMP | awk '{print $2}')"="$(echo $CURRENT_VERSION_TEMP | awk '{print $3}')
378

Igor Pečovnik's avatar
Igor Pečovnik committed
379
380
	# check what is available from the repository
	LIST=($(apt-cache show linux-image*${LINUXFAMILY} | grep -E  "Package:|version:|Version:"  | sed -n -e 's/^.*: //p' | sed 's/\.$//g'))
381
	new_list=()
Igor Pečovnik's avatar
Igor Pečovnik committed
382
383
384
385
386

	#printf '%s\n' "${LIST[@]}"
	#read

	# create a human readable menu
387
388
	for ((n=0;n<$((${#LIST[@]}));n++));
	do
Igor Pečovnik's avatar
Igor Pečovnik committed
389
390
391
392
393
394
395
396
397
        m=$(( $n + 1 ))
        prvi=$((3*$m - 3))
        drugi=$((3*$m - 2))
        tretji=$((3*$m - 1))
        [[ -z ${LIST[$prvi]} ]] && break
		if [[ $CURRENT_VERSION != "${LIST[$prvi]}=${LIST[$drugi]}" ]]; then
			new_list+=( "${LIST[$prvi]}=${LIST[$drugi]}" )
			new_list+=( ${LIST[$tretji]} )
		fi
398
399
400
401
	done

	# copy back to main array
	LIST=("${new_list[@]}")
JC Staudt's avatar
JC Staudt committed
402
	LIST_LENGTH=$((${#LIST[@]}/2));
403

JC Staudt's avatar
JC Staudt committed
404
	if [ "$LIST_LENGTH" -eq 0 ]; then
Igor Pečovnik's avatar
Igor Pečovnik committed
405
		dialog --backtitle "$BACKTITLE" --title " Warning " --msgbox  "\nNo other kernels available!" 7 32
406
        else
Igor Pečovnik's avatar
Igor Pečovnik committed
407
408
409
410
		beta_disclaimer "Switching between kernels might change functionality of your board or it might fail to boot."
		if [[ -n $ACKNOWLEDGEMENT ]]; then
			exec 3>&1
            TARGET_VERSION=$(dialog --cancel-label "Cancel" --backtitle "$BACKTITLE" --no-collapse \
JC Staudt's avatar
JC Staudt committed
411
			--title "Switch from" --clear --menu "\n${CURRENT_VERSION} $(uname -r) \n \n" $((9+${LIST_LENGTH})) 62 25 "${LIST[@]}" 2>&1 1>&3)
Igor Pečovnik's avatar
Igor Pečovnik committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
			exitstatus=$?;
			exec 3>&-
			if [[ $exitstatus = 0 ]]; then
				IFS=" "
				# install packages
				PACKAGE_LIST="$TARGET_VERSION"
				TARGET_VERSION_DTB=${TARGET_VERSION/image/dtb}
				TARGET_VERSION_PRE=$(echo $TARGET_VERSION_DTB | cut -f1 -d"=")
				TARGET_VERSION_SUB=$(echo $TARGET_VERSION_DTB | cut -f2 -d"=")
				[[ -n $(apt-cache madison "$TARGET_VERSION_PRE" | grep $TARGET_VERSION_SUB ) ]] && PACKAGE_LIST=$PACKAGE_LIST" $TARGET_VERSION_DTB"
				echo $PACKAGE_LIST > /tmp/switch_kernel.log 2>&1
				apt-get -s -y --allow-downgrades --no-install-recommends install $PACKAGE_LIST >> /tmp/switch_kernel.log 2>&1
				if [[ $? = 0 ]]; then
					# remove old kernel
					dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nRemoving current kernel ..." 5 36
					aptitude remove ~nlinux-image*${LINUXFAMILY} --quiet=100 -y >> /var/log/upgrade.log 2>&1
					aptitude remove ~nlinux-dtb --quiet=100 -y >> /var/log/upgrade.log 2>&1
					debconf-apt-progress --	apt-get -y -qq --allow-downgrades --no-install-recommends install $PACKAGE_LIST
					dialog --title "Switching kernel to" --backtitle "$BACKTITLE" --yes-label "Reboot"  --no-label "Cancel" --yesno "\n$TARGET_VERSION?" 7 48
					if [[ $? = 0 ]]; then reboot; fi
				else
					dialog --backtitle "$BACKTITLE" --title "Warning" --msgbox "\nTest install failed. Can't change firmware \n\nCheck /tmp/switch_kernel.log" 9 48
434
				fi
435
			fi
Igor Pečovnik's avatar
Igor Pečovnik committed
436
		fi
437
438
439
440
441
442
443
444
	fi
}



#
# check if board has alternative kernels
#
445
function aval_dtbs ()
446
447
{

448
449
	if [[ $LINUXFAMILY == cubox ]]; then
	local width=80
450
451
452
453
454
455
	LIST=("imx6dl-hummingboard.dtb" "HB Solo/DualLite" "imx6dl-hummingboard-emmc-som-v15.dtb" "HB Solo/DualLite v1.5 with eMMC" "imx6dl-hummingboard-som-v15.dtb" "HB Solo/DualLite v1.5" \
	"imx6dl-hummingboard2.dtb" "HB2 Solo/DualLite" "imx6dl-hummingboard2-emmc-som-v15.dtb" "HB2 Solo/DualLite v1.5 with eMMC" "imx6dl-hummingboard2-som-v15.dtb" "HB2 Solo/DualLite v1.5" \
	"imx6q-hummingboard.dtb" "HB Dual/Quad" "imx6q-hummingboard-emmc-som-v15.dtb" "HB Dual/Quad v1.5 with eMMC" "imx6q-hummingboard-som-v15.dtb" "HB Dual/Quad v1.5" \
	"imx6q-hummingboard2.dtb" "HB2 Dual/Quad" "imx6q-hummingboard2-emmc-som-v15.dtb" "HB2 Dual/Quad v1.5 with eMMC" "imx6q-hummingboard2-som-v15.dtb" "HB2 Dual/Quad v1.5" \
	"imx6dl-cubox-i.dtb" "Cubox-i Solo/DualLite" "imx6dl-cubox-i-emmc-som-v15.dtb" "Cubox-i Solo/DualLite v1.5 with eMMC" "imx6dl-cubox-i-som-v15.dtb" "Cubox-i Solo/DualLite v1.5" \
	"imx6q-cubox-i.dtb" "Cubox-i Dual/Quad" "imx6q-cubox-i-emmc-som-v15.dtb" "Cubox-i Dual/Quad v1.5 with eMMC" "imx6q-cubox-i-som-v15.dtb" "Cubox-i Dual/Quad v1.5")
456
457
458
459
	else
	local width=52
	LIST=("xu4" "Odroid XU4" "xu3" "Odroid XU3" "xu3l" "Odroid XU3 Lite" "hc1" "Odroid HC1/HC2")
	fi
460

JC Staudt's avatar
JC Staudt committed
461
462
	LIST_LENGTH=$((${#LIST[@]}/2));
	if [ "$LIST_LENGTH" -eq 1 ]; then
463
464
465
466
			TARGET_BOARD=${AVAL_KERNEL[0]}
	else
			exec 3>&1
			TARGET_BOARD=$(dialog --cancel-label "Cancel" --backtitle "$BACKTITLE" --no-collapse \
JC Staudt's avatar
JC Staudt committed
467
			--title "Select optimised board configuration" --clear --menu "" $((6+${LIST_LENGTH})) ${width} 25 "${LIST[@]}" 2>&1 1>&3)
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
			exitstatus=$?;
			exec 3>&-
	fi

}




#
# select video modes for a10 and a20
#
function get_a20modes ()
{

	IFS=$'\r'
	GLOBIGNORE='*'
	SCREEN_RESOLUTION=("1920x1080p60" "1280x720p60" "1920x1080p50" "1280x1024p60" "1024x768p60" "800x600p60" "640x480p60" "1360x768p60" "1440x900p60" "1680x1050p60")
	local LIST=()
	for i in "${SCREEN_RESOLUTION[@]}"
	do
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
	done
JC Staudt's avatar
JC Staudt committed
491
492
	LIST_LENGTH=$((${#LIST[@]}/2));
	#echo $LIST_LENGTH
493
	#exit
JC Staudt's avatar
JC Staudt committed
494
	if [ "$LIST_LENGTH" -eq 1 ]; then
495
496
497
498
			SCREEN_RESOLUTION=${SCREEN_RESOLUTION[0]}
	else
			exec 3>&1
			SCREEN_RESOLUTION=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
JC Staudt's avatar
JC Staudt committed
499
			--title "Select video mode" --clear --menu "" $((6+${LIST_LENGTH})) 25 $((1+${LIST_LENGTH})) "${LIST[@]}" 2>&1 1>&3)
500
501
502
503
504
505
506
507
			exec 3>&-
	fi

}




508
509
510
511
512
513
514
515
516
517
518
519
520
521
#
# select video modes for odroid c1/c2
#
function get_odroidmodes ()
{

        IFS=$'\r\n'
        GLOBIGNORE='*'
        SCREEN_RESOLUTION=($(cat /boot/boot.ini | grep -w "# setenv" | grep "hz" | cut -d'"' -f 2))
        local LIST=()
        for i in "${SCREEN_RESOLUTION[@]}"
        do
                        LIST+=( "${i[0]//[[:blank:]]/}" "" )
        done
JC Staudt's avatar
JC Staudt committed
522
523
        LIST_LENGTH=$((${#LIST[@]}/2));
        #echo $LIST_LENGTH
524
        #exit
JC Staudt's avatar
JC Staudt committed
525
        if [ "$LIST_LENGTH" -eq 1 ]; then
526
527
528
529
                        SCREEN_RESOLUTION=${SCREEN_RESOLUTION[0]}
        else
                        exec 3>&1
                        SCREEN_RESOLUTION=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
JC Staudt's avatar
JC Staudt committed
530
                        --title "Select video mode" --clear --menu "" $((6+${LIST_LENGTH})) 25 $((1+${LIST_LENGTH})) "${LIST[@]}" 2>&1 1>&3)
531
532
533
534
535
536
537
538
                        exec 3>&-
        fi

}




539
540
541
542
543
544
545
546
#
# select video modes for h3
#
function get_h3modes ()
{

	IFS=$'\r\n'
	GLOBIGNORE='*'
Igor Pečovnik's avatar
Igor Pečovnik committed
547
	SCREEN_RESOLUTION=($(h3disp -i clean))
548
549
550
551
552
	local LIST=()
	for i in "${SCREEN_RESOLUTION[@]}"
	do
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
	done
JC Staudt's avatar
JC Staudt committed
553
554
	LIST_LENGTH=$((${#LIST[@]}/2));
	#echo $LIST_LENGTH
555
	#exit
JC Staudt's avatar
JC Staudt committed
556
	if [ "$LIST_LENGTH" -eq 1 ]; then
557
558
559
560
			SCREEN_RESOLUTION=${SCREEN_RESOLUTION[0]}
	else
			exec 3>&1
			SCREEN_RESOLUTION=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
JC Staudt's avatar
JC Staudt committed
561
			--title "Select video mode" --clear --menu "" $((6+${LIST_LENGTH})) 25 $((1+${LIST_LENGTH})) "${LIST[@]}" 2>&1 1>&3)
562
563
564
565
566
567
568
			exec 3>&-
	fi

}



569

570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#
# create or pick unprivileged user
#
function add_choose_user ()
{

	IFS=$'\r\n'
	GLOBIGNORE='*'

	local USERS=($(awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd))
	local LIST=()
	for i in "${USERS[@]}"
	do
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
	done
JC Staudt's avatar
JC Staudt committed
585
	LIST_LENGTH=$((${#LIST[@]}/2));
586

JC Staudt's avatar
JC Staudt committed
587
	if [ "$LIST_LENGTH" -eq 0 ]; then
588
589
590
591
592
593
594
595
596
		dialog --backtitle "$BACKTITLE" --title " Notice " --msgbox "\nWe didn't find any unprivileged user with sudo rights which is required to run this service.\
		\n\nPress enter to create one!" 10 48
		read -t 0 temp
		echo -e "\nPlease provide a username (eg. your forename) or leave blank for canceling user creation: \c"
		read -e username
		CHOSEN_USER="$(echo "$username" | tr '[:upper:]' '[:lower:]' | tr -d -c '[:alnum:]')"
		[ -z "$CHOSEN_USER" ] && return
		echo "Trying to add user $CHOSEN_USER"
		adduser $CHOSEN_USER || return
JC Staudt's avatar
JC Staudt committed
597
	elif [ "$LIST_LENGTH" -eq 1 ]; then
598
599
600
601
			CHOSEN_USER=${USERS[0]}
	else
			exec 3>&1
			CHOSEN_USER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
JC Staudt's avatar
JC Staudt committed
602
			--title "Select unprivileged user" --clear --menu "" $((6+${LIST_LENGTH})) 40 15 "${LIST[@]}" 2>&1 1>&3)
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
			exec 3>&-
	fi

}




#
# configure armbian desktop
#
function configure_desktop ()
{

	add_choose_user

	if [ -n "$CHOSEN_USER" ]; then
620
621

		# update packages
622
		debconf-apt-progress -- apt-get update
623

Igor Pečovnik's avatar
Igor Pečovnik committed
624
625
626
627
		# install new package if exists
		unset PACKAGE_SUFIX
		[[ -n $(apt-cache search --names-only "^armbian-${DISTROID}-desktop-xfce$") ]] && PACKAGE_SUFIX="-xfce"

628
629
		# remove desktop package to secure proper install
		if check_if_installed armbian-${DISTROID}-desktop ; then
Igor Pečovnik's avatar
Igor Pečovnik committed
630
			debconf-apt-progress -- apt-get -y remove armbian-${DISTROID}-desktop${PACKAGE_SUFIX} lightdm lightdm-gtk-greeter
631
632
633
		fi

		# install desktop package
Igor Pečovnik's avatar
Igor Pečovnik committed
634
		debconf-apt-progress -- apt-get --reinstall -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y install $1 armbian-${DISTROID}-desktop${PACKAGE_SUFIX} lightdm lightdm-gtk-greeter
root's avatar
root committed
635

Igor Pečovnik's avatar
Igor Pečovnik committed
636
637
		# in case previous install was interrupted
		[[ $? -eq 130 ]] && dpkg --configure -a
root's avatar
root committed
638

639
640
641
		# clean apt cache
		apt-get clean

642
		# add user to groups
643
		for additionalgroup in sudo netdev audio video dialout plugdev input bluetooth systemd-journal ssh; do
644
645
646
				usermod -aG ${additionalgroup} ${CHOSEN_USER} 2>/dev/null
		done

647
648
		# Prevent loading paralel printer port drivers which we don't need here.Suppress boot error if kernel modules are absent
		if [[ -f /etc/modules-load.d/cups-filters.conf ]]; then
Igor Pečovnik's avatar
Igor Pečovnik committed
649
650
651
			sed "s/^lp/#lp/" -i /etc/modules-load.d/cups-filters.conf
			sed "s/^ppdev/#ppdev/" -i /etc/modules-load.d/cups-filters.conf
			sed "s/^parport_pc/#parport_pc/" -i /etc/modules-load.d/cups-filters.conf
652
653
		fi

654
655
656
657
658
659
660
		# enable show windows content on stronger boards
		cpu_cores=$(grep -c '^processor' /proc/cpuinfo | sed 's/^0$/1/')
		if [[ ${cpu_cores} -gt 2 && -f /etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml ]]; then
			sed -i 's/<property name="box_move" type="bool" value=".*/<property name="box_move" type="bool" value="false"\/>/g' \
			/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml
		fi

661
662
663
664
665
666
		# fix for gksu in Xenial
		touch /home/${CHOSEN_USER}/.Xauthority
		cp -R /etc/skel/. /home/${CHOSEN_USER}

		# set up profile sync daemon on desktop systems
		which psd >/dev/null 2>&1
Igor Pečovnik's avatar
Igor Pečovnik committed
667
		if [[ $? -eq 0 && -z $(grep overlay-helper /etc/sudoers) ]]; then
668
669
670
671
			echo "${CHOSEN_USER} ALL=(ALL) NOPASSWD: /usr/bin/psd-overlay-helper" >> /etc/sudoers
			touch /home/${CHOSEN_USER}/.activate_psd
		fi

Igor Pečovnik's avatar
Igor Pečovnik committed
672
673
674
675
676
677
		mkdir -p /etc/lightdm/lightdm.conf.d
		echo "[Seat:*]" > /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
		echo "autologin-user=$CHOSEN_USER" >> /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
		echo "autologin-user-timeout=0" >> /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
		echo "user-session=xfce" >> /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
		ln -s /lib/systemd/system/lightdm.service /etc/systemd/system/display-manager.service >/dev/null 2>&1
Igor Pečovnik's avatar
bugfix    
Igor Pečovnik committed
678
679
		# fix permissions
		chown -R ${CHOSEN_USER}:${CHOSEN_USER} /home/${CHOSEN_USER}/.
Igor Pečovnik's avatar
Igor Pečovnik committed
680
		service lightdm start >/dev/null 2>&1
681
682
	fi

683
}