set -e                  # exit on error
set -o pipefail         # exit on pipeline error
set -u                  # treat unset variable as error

print_ok "Configuring locales..."
dpkg-reconfigure locales
judge "Configure locales"

print_ok "Configuring locales to $LANG..."

cat << EOF > /etc/default/locale
#  File generated by update-locale
LANG="$LANG"
EOF
judge "Configure /etc/default/locale"

# Comment the lines below.
# This is because if you uncomment them, the ubiquity installer will throw an error indicating that `grub-install: error "cannot find EFI directory"`.
# I have wasted several hours trying to figure out why the ubiquity installer is throwing this error. But I have not found a solution yet.
# So please just comment the lines below.

# cat << EOF > /etc/skel/.pam_environment
# LANGUAGE	DEFAULT=$LANG_MODE:$LANG_PACK_CODE
# LANG	DEFAULT=$LANG
# LC_NUMERIC	DEFAULT=$LANG
# LC_TIME	DEFAULT=$LANG
# LC_MONETARY	DEFAULT=$LANG
# LC_PAPER	DEFAULT=$LANG
# LC_NAME	DEFAULT=$LANG
# LC_ADDRESS	DEFAULT=$LANG
# LC_TELEPHONE	DEFAULT=$LANG
# LC_MEASUREMENT	DEFAULT=$LANG
# LC_IDENTIFICATION	DEFAULT=$LANG
# PAPERSIZE	DEFAULT=a4
# EOF
# judge "Configure /etc/skel/.pam_environment"


# This is actually setting on the system clock, not the timezone. Do not use this command:
# > timedatectl set-timezone $TIMEZONE
# To set the timezone for the new OS being built (In chroot environment), use the following command:

# If TIMEZONE was not set, exit.
if [ -z "$TIMEZONE" ]; then
    print_error "Error: TIMEZONE is not set."
    exit 1
fi

if [ ! -f /usr/share/zoneinfo/$TIMEZONE ]; then
    print_error "Error: /usr/share/zoneinfo/$TIMEZONE not found."
    exit 1
fi

print_ok "Configuring /etc/timezone to $TIMEZONE..."
echo $TIMEZONE > /etc/timezone
judge "Configure /etc/timezone"

print_ok "Configuring /etc/localtime to $TIMEZONE..."
rm /etc/localtime
ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
judge "Configure /etc/timezone and /etc/localtime"


