#!/bin/bash
# SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: GPL-3.0-or-later

#set -e
readonly PREREQ=""

prereqs() {
	echo "${PREREQ}"
}

case "${1}" in
prereqs)
	prereqs
	exit 0
	;;
esac

tmp_fix_file=$(mktemp /tmp/deepin-fix-init-install-XXXXXXXX.log)
. /usr/share/initramfs-tools/hook-functions

copy_exec /usr/bin/deepin-fix-init /bin/ || echo "not install deepin-fix-init" >> "$tmp_fix_file"
copy_file file /usr/lib/terminfo/l/linux || echo "not install /usr/lib/terminfo/l/linux" >> "$tmp_fix_file"
copy_exec /bin/fbterm /bin/ || echo "not copy_exec fbterm" >> "$tmp_fix_file"
copy_exec /bin/gettext /bin/ || echo "not copy_exec gettext" >> "$tmp_fix_file"
copy_file file /usr/share/terminfo/f/fbterm || echo "not install fbterm " >> "$tmp_fix_file"
copy_file file /usr/share/fonts/truetype/unifont/unifont.ttf || echo "not install unifont.ttf " >> "$tmp_fix_file"
copy_file file /usr/share/locale/fix-init-locale-archive /usr/lib/locale/locale-archive || echo "not install local archive" >> "$tmp_fix_file"
copy_file file /etc/default/locale || echo "not install local" >> "$tmp_fix_file"
copy_file file /bin/gettext.sh || echo "not install gettext.sh" >> "$tmp_fix_file"

if ischroot; then
       echo "not exec deepin-fix-init in chroot 1"
       exit 0
fi

testchroot() {
    init=$(cat /proc/1/comm 2> /dev/null)
    if [ $? -ne 0 ]; then #chroot 环境
            return 0
    else
        if [ "$init" == "systemd" ]; then # 正常环境
            return 1
        else
            return 2 #systemd-nspawn and docker 返回bash的容器环境
        fi
    fi
}

set +e
testchroot
retchroot=$?
set -e
if [ $retchroot -ne 1 ];then
        echo "not exec deepin-fix-init in chroot"
        exit 0
fi

# load locale config
[ -f /etc/default/locale ] && . /etc/default/locale

get_locale_arr() {
	if [ -z "$LANG" ]; then
		return
	fi
	# zh_CN.UTF-8
	echo $LANG
	# trim right .*
	local lang=${LANG%.*}
	# zh_CN
	echo $lang
	# trim right _*
	lang=${LANG%_*}
	# zh
	echo $lang
}

find_file() {
	local pattern="$1"
	for l in $(get_locale_arr); do
		local f=${pattern/@lang/$l}
		if [ -f "$f" ]; then
			echo "$f"
			return
		fi
	done
}

dialog_mo=$(find_file /usr/share/locale/@lang/LC_MESSAGES/dialog.mo)
if [ -f "$dialog_mo" ]; then
	copy_file file "$dialog_mo"
else
	echo WARN: no found dialog.mo >&2
fi

fix_init_mo=$(find_file /usr/share/locale/@lang/LC_MESSAGES/deepin-fix-init.mo)
if [ -f "$fix_init_mo" ]; then
	copy_file file "$fix_init_mo"
else
	echo WARN: no found deepin-fix-init.mo >&2
fi

get_mount_point_dev() {
        findmnt --target "$1" -n -f -v | awk '{print $2}'
}

get_real_device_name() {
    local dev_name=$1
    local dm_output_list=$(dmsetup deps -o devname)
    local retry_idx=1

    while  [ $retry_idx -le 5 ]; do
        local parent_dev=$(echo "$dm_output_list" | grep -w "^${dev_name}:" | sed -n 's/.*(\(.*\)).*/\1/p' )

        if [ ! -b /dev/mapper/$parent_dev ]; then
            if [ -b /dev/$parent_dev ]; then
                real_dev=$parent_dev
            fi
            break
        else
            dev_name=$parent_dev
            ((retry_idx++))
        fi
    done
    if [ -z $real_dev ]; then
        echo "found miss root device for $1">&2
        real_dev=$1
    fi
}

tmp_file=$(mktemp /tmp/deepin-fix-init-XXXXXX)
fix_init_root_dev=$(get_mount_point_dev /)
fix_init_root_fstype=$(blkid -o value -s TYPE "$fix_init_root_dev" || echo "$fix_init_root_fstype" >> "$tmp_fix_file")

if [[ $fix_init_root_dev == /dev/mapper/* && -x /sbin/dmsetup ]]; then
#if [ -x /sbin/dmsetup ]; then
    echo "The root device is: $fix_init_root_dev type: $fix_init_root_fstype"
    get_real_device_name "$(basename $fix_init_root_dev)"
    if [ -n "$real_dev" ]; then
            fix_init_root_dev="/dev/$real_dev"
	    echo "The real root device is: $fix_init_root_dev"
    fi
fi

echo "ROOT_DEV=$fix_init_root_dev" >>"$tmp_file"
echo "ROOT_TYPE=$fix_init_root_fstype" >>"$tmp_file"
copy_file file "$tmp_file" /etc/deepin-fix-init.cfg
if [ -s "$tmp_fix_file" ];then
	copy_file file "$tmp_fix_file" /etc/deepin-fix-install-init.log
fi
rm -f $tmp_file
rm -f ${tmp_fix_file}
