#!/bin/bash

set -e


if [[ -f /lib/cryptsetup/scripts/set_cryptsetup_lang ]]; then
	bash /lib/cryptsetup/scripts/set_cryptsetup_lang || echo "set_cryptsetup_lang is failed"
fi

#if [[ $(uname -m) =~ 'sw_64' ]]; then
#    if [[ -f /usr/sbin/update-initramfs ]]; then
#    	msg "更新 initramfs"
#		apt purge -y casper || echo "卸载casper包失败"
#    	update-initramfs -u
#    	sync
#    fi
#    if [[ -f /usr/sbin/update-grub ]]; then
#    	update-grub
#    fi
#    return 0
#fi

#if grep -q 'zh_CN' /etc/default/locale; then
#    touch /usr/share/initramfs-tools/scripts/lang_is_zh
#else
#    rm -f /usr/share/initramfs-tools/scripts/lang_is_zh || true
#fi

if [[ -f /usr/sbin/update-initramfs ]]; then

    if egrep -qi '3A5000' /proc/cpuinfo; then
	echo "this is 3A5000,update-initramfs in the after apt purge casper"	
    else
    	msg "更新 initramfs"
	apt purge -y casper || echo "卸载casper包失败"
    	update-initramfs -u
    	sync
    fi
fi


#
if [[ "x`archdetect`" = "xloongarch64/generic" ]] && [[ -f /usr/sbin/grub-install ]]; then
    grub-install || true
fi

if [[ "x`archdetect`" = "xloong64/generic" ]] && [[ -f /usr/sbin/grub-install ]]; then
    grub-install || true
fi

if [[ -f /usr/sbin/update-grub ]]; then
    update-grub
fi

kernel_vesion=`uname -r`

if [[ "${is_ghost}" == "true" ]]; then
  return 0
fi

mv /boot/grub/grub.cfg /boot/loader/grub.cfg
ln -s ../loader/grub.cfg /boot/grub/grub.cfg
#for initrd_path in /boot/loader/entries/* ; do
#    initrd_file_path=`grep -rn "initramfs" $initrd_path | awk -F " " '{print $2}'`
#    cp /boot/initrd.img-$kernel_vesion /boot$initrd_file_path
#done
#rm /boot/initrd.img-$kernel_vesion
