#!/bin/sh

source /OSM/script/os_load_base.sh
PATCH_NAME=DTS202102050KX0XNP0V

###load a patch
patch_load()
{
    ###load patch
    sudo livepatch -l /OSM/modules/klp_$1.tar.gz
    if [ 0 != $? ];then
        OS_echo "$1 load patch failed"
        exit 1
    fi
}

###active a patch
patch_active()
{
    ###load patch
    sudo livepatch -a $1
    if [ 0 != $? ];then
        OS_echo "$1 active patch failed"
        exit 2
    fi
}

###rollback a patch
patch_rollback()
{
	###check patch if loaded
	PATCH_LOAD_CHECK=`sudo livepatch -q | grep $1`
	if [ -n "${PATCH_LOAD_CHECK}" ];then
		sudo livepatch -d $1
		if [ 0 != $? ];then
			OS_echo "$1 deactive patch failed"
			exit 3
		else
		    sudo livepatch -r $1
		    if [ 0 != $? ];then
			    OS_echo "$1 delete patch failed"
			    exit 4
		    fi
		fi
	fi
}

PATCH_START()
{
    ###start no need handle
	return
}

PATCH_END()
{
    ###check if unflowlevel_hifc has been loaded, then load this patch, otherwise do not load this patch
	MODULE_LOAD_CHECK=`sudo lsmod |grep unflowlevel_hifc`
	if [ -n "${MODULE_LOAD_CHECK}" ];then
		###load patch
		patch_load $PATCH_NAME
		
        ###active patch
		patch_active $PATCH_NAME
	fi
}

PATCH_ROLLBACK()
{	
	patch_rollback $PATCH_NAME
	
    return
}

PATCH_ROLLEND()
{
	##rollend no need handle
	return
}

L_CMD=$1
case $L_CMD in
    "start") PATCH_START
               ;;
    "end") PATCH_END
               ;;
    "rollstart") PATCH_ROLLBACK
               ;;
    "rollend") PATCH_ROLLEND
               ;;
    *)
        OS_echo "unsupported option."
esac

exit 0
