#!/bin/sh
# shellcheck disable=SC1091
if [ ! "$(id -u)" -eq 0 ]; then
  echo "Please execute this script as root."
  exit
fi
. ./01_SetVariables.sh
TEMPDIR="$(mktemp -d)" || exit 1
export TEMPDIR
# the script SeTlocales prevides the functions SeTCode and SeTLocaleDir
# shellcheck disable=SC1091
. ../files-in-initrd/SeTlocales
cd "$TEMPDIR" || exit 1
# Copying new and modified files in $TMP/initrd/
while read -r i; do
	[ ! "$i" ] && continue
	echo "$i included in the installer "
	mkdir -p "$DEST"/"$(dirname "$i")"
	cp -r "$SCRIPTS"/"$(basename "$i")" "$DEST"/"$(dirname "$i")"
	chmod 0755 "$DEST"/"$(dirname "$i")"/"$(basename "$i")"
done < "$ROOTDIR"/files_in_the_initrd.list
# Copying the documentation
mkdir -p "$DEST"/usr/docinstaller
cp "$ROOTDIR"/doc/*html "$DEST"/usr/docinstaller
cp "$ROOTDIR"/doc/doc "$DEST"/bin/
chmod 0755 "$DEST"/bin/doc
chmod 0644 "$DEST"/usr/docinstaller/*
mkdir -p "$DEST"/usr/share/fonts
cp "$ROOTDIR"/fonts/* "$DEST"/usr/share/fonts
# /etc/rc.d/rc.espeakup calls speakup-restore shipped in the
# slint-scripts
# but we include this package in the installer...
cp -a /usr/sbin/speakup-restore "$DEST"/sbin/
# We need these programs to display translations
# But we include gettext* in the installer...
# cp -a /usr/bin/envsubst  "$DEST"/usr/bin/
# cp -a /usr/bin/gettext*  "$DEST"/usr/bin/
# we will include gconv-modules and all libraries in /usr/lib64/gconv
# associated to encodings of PO files
# We ship aaa_glibc-solibs in the installer, so...
# mkdir -p "$DEST"/usr/lib64/gconv
# for i in gconv-modules ISO8859-1.so ISO8859-15.so ISO8859-2.so KOI8-R.so; do
# cp -a usr/lib64/gconv/"$i" "$DEST"/usr/lib64/gconv
# glibc-i18n is too heavy to ship it in the intsaller, so...
# Copying /usr/lib64/locale/<locale>/* and the (generated) MO files in: $TMP/initrd
mkdir -p "$DEST"/usr/lib64/locale
. "$ROOTDIR"/files-in-initrd/SeTlocales
while read -r Locale; do
	ll=$(echo "$Locale"|cut -c 1-2)
	# the script SeTlocales provides the functions SeTCode and SeTLocaleDir
	SeTLocaleDir
	# Include the files gettext needs in /usr/lib64/locale.
	mkdir -p "$DEST"/usr/share/locale/"$LocaleDir"/LC_MESSAGES
	cp -r  /usr/lib64/locale/"$Locale" \
		"$DEST"/usr/lib64/locale
	if [ -f  /usr/share/locale/"$LocaleDir"/LC_MESSAGES/gettext-runtime.mo ]; then
		cp /usr/share/locale/"$LocaleDir"/LC_MESSAGES/gettext-runtime.mo \
		"$DEST"/usr/share/locale/"$LocaleDir"/LC_MESSAGES
	elif [ -f /usr/share/locale/"$ll"/LC_MESSAGES/gettext-runtime.mo ]; then
		cp /usr/share/locale/"$ll"/LC_MESSAGES/gettext-runtime.mo \
		"$DEST"/usr/share/locale/"$LocaleDir"/LC_MESSAGES
	fi
	if [ -f /usr/share/locale/"$LocaleDir"/LC_MESSAGES/dialog.mo ]; then
		cp /usr/share/locale/"$LocaleDir"/LC_MESSAGES/dialog.mo \
		"$DEST"/usr/share/locale/"$LocaleDir"/LC_MESSAGES
	elif [ -f /usr/share/locale/"$ll"/LC_MESSAGES/dialog.mo ]; then
		cp /usr/share/locale/"$ll"/LC_MESSAGES/dialog.mo \
		"$DEST"/usr/share/locale/"$LocaleDir"/LC_MESSAGES
	fi
	if [ -f  /usr/share/locale/"$LocaleDir"/LC_MESSAGES/util-linux.mo ]; then
		cp /usr/share/locale/"$LocaleDir"/LC_MESSAGES/util-linux.mo \
		"$DEST"/usr/share/locale/"$LocaleDir"/LC_MESSAGES
	elif [ -f /usr/share/locale/"$ll"/LC_MESSAGES/util-linux.mo ]; then
		cp /usr/share/locale/"$ll"/LC_MESSAGES/util-linux.mo \
		"$DEST"/usr/share/locale/"$LocaleDir"/LC_MESSAGES
	fi
	if [ -f  /usr/share/locale/"$LocaleDir"/LC_MESSAGES/nano.mo ]; then
		cp /usr/share/locale/"$LocaleDir"/LC_MESSAGES/nano.mo \
		"$DEST"/usr/share/locale/"$LocaleDir"/LC_MESSAGES
	elif [ -f /usr/share/locale/"$ll"/LC_MESSAGES/nano.mo ]; then
		cp /usr/share/locale/"$ll"/LC_MESSAGES/nano.mo \
		"$DEST"/usr/share/locale/"$LocaleDir"/LC_MESSAGES
	fi
done < "$ROOTDIR"/doc/locales_list_sorted_by_name
