#!/bin/bash

# Based on:
#
# 1) https://github.com/OSInside/kiwi/blob/v9.23.20/kiwi/config/functions.sh
#    Or: /usr/lib/python3.6/site-packages/kiwi/config/functions.sh (python3-kiwi package)
#
# 2) https://build.opensuse.org/package/view_file/openSUSE:Factory/live-langset-data/langset.sh?expand=1
#    Or: /usr/sbin/langset.sh (live-langset-data package)
#
# 3) How to check if sed has changed a file
#    https://stackoverflow.com/a/12144347/1657502
#
function baseUpdateSysConfigFailSafe {
    # /.../
    # Update the contents of a sysconfig variable
    # ----
    local FILE=$1
    local VAR=$2
    local VAL=$3
    # If the file does not exist, create it
    if [ ! -f "${FILE}" ]; then
        touch "${FILE}"
    fi
    # If VAR is set, update it
    sed -i.bak -e "s#$VAR=\".*#$VAR=\"$VAL\"#" "${FILE}"
    # diff returns 0 if the file was not changed (i.e. sed made no substitution)
    if diff "${FILE}" "${FILE}.bak" &> /dev/null; then
        # If VAR is not set, define it
        echo "$VAR=\"$VAL\"" >> "${FILE}"
    fi
    # Delete the backup file created by sed
    rm "${FILE}.bak"
}

SELECTED_LANGUAGE=$1

# Set locale
/usr/sbin/langset.sh $SELECTED_LANGUAGE

# Let's touch openSUSE defaults only if the user has selected Brazilian Portuguese
if [ "$SELECTED_LANGUAGE" == "pt_BR" ]; then
    baseUpdateSysConfigFailSafe /etc/sysconfig/keyboard YAST_KEYBOARD "portugese-br,pc104"
    baseUpdateSysConfigFailSafe /etc/sysconfig/language RC_LANG "pt_BR.UTF-8"
    baseUpdateSysConfigFailSafe /etc/sysconfig/language ROOT_USES_LANG "yes"
    baseUpdateSysConfigFailSafe /etc/sysconfig/language INSTALLED_LANGUAGES "pt_BR"
    baseUpdateSysConfigFailSafe /etc/sysconfig/clock DEFAULT_TIMEZONE "Brazil/East"
    echo "pool pool.ntp.br iburst" > /etc/chrony.d/pool.conf
    usermod -c "Usuário da mídia Live" linux
fi

