#!/bin/bash

set -e

if [[ "${is_minimal}" = "true" ]]; then
    return 0
elif [[ "${is_ghost}" = "true" ]]; then
    return 0
fi

USERNAME=$(get_value username)
PASSWORD=$(get_value_bytearray password)
AUTO_LOGIN=$(get_value autologin)
KUID=1000
data_unformat=$(get_value data-unformat)

# oem mode
if [[ "${is_oem_mode}" = "true" ]]; then
    USERNAME=oem
    PASSWORD=Kylin123.
    AUTO_LOGIN=1
    KUID=29999
    # sudo 免密
    echo "oem ALL=(ALL) NOPASSWD: ALL" >/etc/sudoers.d/oem
fi

### TODO
# groups zz adm cdrom sudo dip plugdev lpadmin sambashare

if [[ "${data_unformat}" == "true" ]]; then

	if [[ -f /usr/share/kylin-os-installer/user-groups.txt ]]; then
		while read i
		do
        		user_name=
        		user_name=${i%%=*}
        		echo "${user_name}"

        		user_groups=
        		user_groups=${i##*=}
        		echo "${user_groups}"
        		user_default_groups=(${user_groups})

        		for group in ${user_default_groups[*]}; do
                		adduser ${user_name} $group || true
        		done

		done < /usr/share/kylin-os-installer/user-groups.txt	
		rm -rf /usr/share/kylin-os-installer/user-groups.txt
	fi

	if [[ "${is_oem_mode}" = "true" ]]; then
		echo "unformat data partition && oem"
	else
		return 0
	fi
fi

if [[ "${is_990_9a0}" = "true" ]]; then
        echo "this is 990_9a0"
else
	if [[ "${is_oem_mode}" = "true" ]]; then
    		return 0
	fi
fi


msg "设置用户 ${USERNAME}"
useradd -m -s /bin/bash -u ${KUID} "${USERNAME}"
usermod -c "${USERNAME}" "${USERNAME}"
echo "${USERNAME}:${PASSWORD}" | chpasswd
# /etc/sudoers

add_groups() {
    user_default_groups=(adm cdrom sudo dip plugdev users lpadmin sambashare debian-tor libvirtd lxd)
    for group in ${user_default_groups[*]}; do
        adduser ${USERNAME} $group || true
    done
}

set_autologin() {
    if [[ "${AUTO_LOGIN}" = "1" ]]; then
        if [[ -d /etc/lightdm ]]; then
            # Configure LightDM autologin
            LightDMCustomFile=/etc/lightdm/lightdm.conf
            AutologinParameters="autologin-guest=false\n\
autologin-user=${USERNAME}\n\
autologin-user-timeout=0"
        if ! grep -qs '^autologin-user' ${LightDMCustomFile}; then
                if ! grep -qs '^\[Seat:\*\]' ${LightDMCustomFile}; then
                    echo '[Seat:*]' >>${LightDMCustomFile}
                fi
                sed -i "s/\[Seat:\*\]/\[SeatDefaults]\n${AutologinParameters}/" ${LightDMCustomFile}
            # oem config scenario
            else
                #sed -i "s/^\(\(str  *\)\?autologin-user\)=.*$/\1=${USERNAME}/g;" /etc/lightdm/lightdm.conf
                sed -i "s/^autologin-user=.*$/autologin-user=${USERNAME}/g" ${LightDMCustomFile}
            fi
        fi
	systemctl restart accounts-daemon.service || true
    fi
}

add_groups

set_autologin


