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

readonly PREREQ="plymouth"

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

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

if [ -n "$break" ]; then
        # 因为启动参数 break 而执行 panic 函数，立即退出
        exit 0
fi

if [ "$BOOT" != local ]; then
        echo 'boot is not local, exit'
        exit 0
fi

cfgfile=/etc/deepin-fix-init.cfg
if [ -f "$cfgfile" ];then
	# 从文件中获取 ROOT_DEV 的值
	root_dev=$(grep "ROOT_DEV" "$cfgfile" | cut -d'=' -f2)
	# 从文件中获取 ROOT_TYPE 的值
	root_type=$(grep "ROOT_TYPE" "$cfgfile" | cut -d'=' -f2)

	if [ "$root_type" == "crypto_LUKS" ];then
		echo "$root_type not support, exit"
		exit 0
	fi
	cur_root_type=$(blkid -o value -s TYPE "$root_dev")
	if [ -n "$cur_root_type" ] && [ "$cur_root_type" == "crypto_LUKS" ];then
		echo "$cur_root_type not support, exit"
		exit 0
	fi
fi

unset IFS
read -r console rest </proc/consoles
if [ "${console}" = "tty0" ]; then
        # Need to choose a specific VT
        console="tty1"
fi

mkdir -p /usr/lib/locale/
if [ -f /usr/share/locale/fix-init-locale-archive ]; then
	mv /usr/share/locale/fix-init-locale-archive /usr/lib/locale/locale-archive
fi

if [ -f /etc/default/locale ]; then
	. /etc/default/locale
else
	LANG=en_US
	LANGUAGE=en_US.UTF-8
fi
export LANG
export LANGUAGE

sh -c "exec fbterm -s 20 -n unifont -- deepin-fix-init <> /dev/${console} 1>&0 2>&1"
#/bin/deepin-fix-init -u ${root_uuid}

EXIT_FILE="/tmp/deepin-fix-init.txt"
if [ -f ${EXIT_FILE} ]; then
        touch "/tmp/panic_continue_boot"
	exit
fi
