#! /bin/sh
#
# Copyright (c) 2021-2021 Huawei Technologies Co., Ltd.
# All rights reserved.
#
### BEGIN INIT INFO
# Provides:    pangea-idm-driver
# Required-Start:
# Required-Stop:
# chkconfig: 1235 45 30
# Default-Stop:     0 6
# Description:       start stop idm service
### END INIT INFO

#成功状态
STATUS_DONE="[OK]"
#失败状态
STATUS_FAILED="[FAILED]"
#运行状态
STATUS_RUNNING="[RUNNING]"
#未运行状态
STAUS_UNUSED="[UNUSED]"
#模块设备字符名称
IOCTL_OP_NAME="board_ioctl_op"

export LD_LIBRARY_PATH=$INSTALL_DIR:$LD_LIBRARY_PATH
export IDM_DRIVER_DIR="/usr/lib/modules/pangea_idmdriver/target"

MGMT_CRON="/etc/mgmtcron"
SCRIPT_PATCH="/usr/lib/modules/pangea_idmdriver/mgmt/script"
MGMT_PORT_FILE_NAME="get_mgmt_port_info.py"
DPU_TEMP_FILE_NAME="get_dpu_temp_info.py"
DPU_ALARM_FILE_NAME='hand_dpu_alarm.py'

INIT_MGMT_INFO_CMD="* * * * * flock -x -w 60 $SCRIPT_PATCH/$MGMT_PORT_FILE_NAME -c 'python $SCRIPT_PATCH/$MGMT_PORT_FILE_NAME >/dev/null 2>&1'"
INIT_DPU_TEMP_INFO_CMD="* * * * * flock -x -w 60 $SCRIPT_PATCH/$DPU_TEMP_FILE_NAME -c 'python $SCRIPT_PATCH/$DPU_TEMP_FILE_NAME >/dev/null 2>&1'"
INIT_HAND_DPU_ALARM_CMD="* * * * * flock -x -w 60 $SCRIPT_PATCH/$DPU_ALARM_FILE_NAME -c 'python $SCRIPT_PATCH/$DPU_ALARM_FILE_NAME >/dev/null 2>&1'"

cd ${IDM_DRIVER_DIR}
echo "cur dir:`pwd`, start or stop idm driver..."

#insmod ko公共函数
insmod_ko()
{
    #先检查文件是否存在
    modeName=$1
    if [ ! -f ${modeName} ];then
        echo -e "Can not find ${modeName} ...       ${STATUS_FAILED}"
        return 1
    fi

    #安装ko
    echo "insmod ${modeName}"
    insmod ${modeName}
    if [ $? -ne 0 ]; then
        echo "insmod ${modeName} fail ...            ${STATUS_FAILED}"
        return 1
    else
        echo "insmod ${modeName} succ ...            ${STATUS_DONE}"
        #睡眠1s保证insmod ko之后，正常创建出设备字符
        sleep 1
        #寻找idm.ko生成的board_ioctl_op设备字符文件
        ret=`ls /dev/ |grep ${IOCTL_OP_NAME} -c`
        if [ $ret -ne 0 ]; then
            chown -hR root:storage /dev/${IOCTL_OP_NAME}
            chmod 640 /dev/${IOCTL_OP_NAME}
            echo "chown and chmod ${IOCTL_OP_NAME} succ ... ${STATUS_DONE}"
        else
            echo "chown ${IOCTL_OP_NAME} fail ...   ${STATUS_FAILED}"
        fi
        return 0
    fi
}

#rmmod_ko 公共函数
rmmod_ko()
{
    #卸载ko
    modeName=$1

    ret=`lsmod | cut -f1 -d' ' | grep -w "${modeName}" -c`
    if [ ${ret} -le 0 ]; then
        echo "${modeName} isn't exist before rmmoding ${modeName}"
        return 0
    fi

    echo "rmmod ${modeName}"
    rmmod ${modeName}
    if [ $? -ne 0 ]; then
        echo "rmmod ${modeName} fail ...              ${STATUS_FAILED}"
        return 1
    else
        echo "rmmod ${modeName} succ ...              ${STATUS_DONE}"
        return 0
    fi
}

# 卸载所有内核态的ko
remove_all_kernel()
{
    echo "remove all pangea idm kernel ...             ${STATUS_RUNNING}"
    rmmod_ko idm
    echo "remove all pangea idm kernel ...             ${STATUS_DONE}"
    return 0
}

# 安装所有内核态的ko
insmod_all_kernel()
{
    echo "insmod all pangea idm kernel ...             ${STATUS_RUNNING}"
    cur_kernel_ver=$(uname -r)
    insmod_ko idm.ko
    echo "insmod all pangea idm kernel ...             ${STATUS_DONE}"
    return 0
}

idm_service_start()
{
    echo -e "start idmservice ...                           ${STATUS_RUNNING}"

    #卸载所有内核态的ko
    remove_all_kernel
    if [ $? -ne 0 ];then
        echo -e "Notice: remove all kernel ...              ${STATUS_FAILED}"
        exit 1
    fi

    #安装所有内核态ko
    insmod_all_kernel
    if [ $? -ne 0 ];then
        echo -e "Notice: insmod_all_kernel ...            ${STATUS_FAILED}"
        exit 1
    fi
}

idm_service_stop()
{
    echo -e "stop idmservice ...                           ${STATUS_DONE}"

    #卸载所有内核态的ko
    remove_all_kernel
    if [ $? -ne 0 ];then
        echo -e "Notice: remove all kernel ...             ${STATUS_FAILED}"
        exit 1
    fi
}

idm_service_status()
{
    echo -e "idmservice status ...                         ${STATUS_RUNNING}"
}

# 检查当前环境是否为DPU
check_is_dpu_product()
{
    dpu_product_types=("STL6IEX2F001" "STL6IEC2F001")
    cur_product_type=$(dmidecode -t 2 |grep "Product Name:" -i | awk -F ': ' '{print $2}')
	for(( i=0;i<${#dpu_product_types[@]};i++ )) do
        if [ "$cur_product_type" == "${dpu_product_types[i]}" ]; then
            return 0
        fi
    done;
	echo "cur product is ${cur_product_type}, not need init mgmt."
    return 1
}

# 检查任务是否已经存在
check_task_already_exist()
{   
    task_name=$1
    local task_already_add=$(crontab -u root -l | grep "${task_name}" -c)
    if [ $task_already_add -ne 0 ]; then
        echo "$task_name crontab already exits."
        return 1
    fi
    return 0
}

# 加载dpu管理信息定时任务
init_dpumgmtinfo()
{  
    # 非DPU环境不部署
    check_is_dpu_product
    if [ $? -ne 0 ]; then
        return 0
    fi

    crontab -u root -l | grep -v "#" > $MGMT_CRON

    check_task_already_exist $MGMT_PORT_FILE_NAME
    if [ $? -ne 1 ]; then
        echo "$INIT_MGMT_INFO_CMD" >> $MGMT_CRON
    fi

    check_task_already_exist $DPU_TEMP_FILE_NAME
    if [ $? -ne 1 ]; then
        echo "$INIT_DPU_TEMP_INFO_CMD" >> $MGMT_CRON
    fi

    check_task_already_exist $DPU_ALARM_FILE_NAME
    if [ $? -ne 1 ]; then
        echo "$INIT_HAND_DPU_ALARM_CMD" >> $MGMT_CRON
    fi

    crontab $MGMT_CRON -u root
    systemctl reset-failed crond >/dev/null 2>&1
    service crond restart > /dev/null 2>&1

    if [ -f "$MGMT_CRON" ]; then
        rm -f $MGMT_CRON
    fi
    echo "INFO: dpu mgmt info success."
}

# 移除dpu管理信息定时任务
del_dpumgmtinfo()
{
    # 非DPU环境不部署
    check_is_dpu_product
    if [ $? -ne 0 ]; then
        exit 0
    fi

    crontab -u root -l | grep -v "#" > $MGMT_CRON

    check_task_already_exist $MGMT_PORT_FILE_NAME
    if [ $? -eq 1 ];then
        # 删除命令
        sed -i "/$MGMT_PORT_FILE_NAME/d" $MGMT_CRON
    fi
    check_task_already_exist $DPU_TEMP_FILE_NAME
    if [ $? -eq 1 ];then
        # 删除命令
        sed -i "/$DPU_TEMP_FILE_NAME/d" $MGMT_CRON
    fi

    check_task_already_exist $DPU_ALARM_FILE_NAME
    if [ $? -eq 1 ];then
        # 删除命令
        sed -i "/$DPU_ALARM_FILE_NAME/d" $MGMT_CRON
    fi

    crontab $MGMT_CRON -u root
    systemctl reset-failed crond >/dev/null 2>&1
    service crond restart > /dev/null 2>&1

    if [ -f "$MGMT_CRON" ]; then
        rm -f $MGMT_CRON
    fi
    echo "INFO: delete dpu mgmt info success."
}

case "$1" in
    start)
        idm_service_start
        init_dpumgmtinfo
        ;;
    stop)
        idm_service_stop
        del_dpumgmtinfo
        ;;
    status)
        idm_service_status
        ;;
    *)
        echo "Usage: $0 {start|stop|status}"
        exit 1
        ;;
esac
