#!/bin/bash
set -o nounset
set -o errexit
set -o pipefail
#set -o xtrace

BASE=$(cd `dirname $0`; cd ../; pwd)

param=$1
ASCEND910_VERSION=${ASCEND910_VERSION:-910-v26.1.0}
NVIDIA_VERSION=v0.17.1
NVIDIA_VGPU_VERSION=gpu-v1.0.RC1
ENFLAME_VERSION=v2.0.0
BIREN_VERSION=v0.7.3
HYGON_VERSION=v2.4.1
imageDir=$BASE/down

source $BASE/utils/logger
source $BASE/utils/env

if [[ "$param" = "ascend910" ]]; then
    if [[ ! -f "$imageDir/ascend-device-plugin_${ASCEND910_VERSION}.tar" ]];then
        logger error "Ascend k8s device plugin($ASCEND910_VERSION) image not exist."
        exit 1
    fi
    docker load -i "$imageDir/ascend-device-plugin_${ASCEND910_VERSION}.tar"
    docker tag "kubemore/ascend-device-plugin:$ASCEND910_VERSION" \
               "easzlab.io.local:5000/kubemore/ascend-device-plugin:$ASCEND910_VERSION"
    docker push "easzlab.io.local:5000/kubemore/ascend-device-plugin:$ASCEND910_VERSION"
    kubectl apply -f $BASE/device-plugin/ascend-device-plugin_${ASCEND910_VERSION}.yaml
elif [[ "$param" = "nvidia" ]]; then
    if [[ ! -f "$imageDir/nvidia-device-plugin_${NVIDIA_VERSION}.tar" ]];then
        logger error "Nvidia k8s device plugin($NVIDIA_VERSION) image not exist."
        exit 1
    fi
    docker load -i "$imageDir/nvidia-device-plugin_${NVIDIA_VERSION}.tar"
    docker tag "kubemore/nvidia-device-plugin:$NVIDIA_VERSION" \
               "easzlab.io.local:5000/kubemore/nvidia-device-plugin:$NVIDIA_VERSION"
    docker push "easzlab.io.local:5000/kubemore/nvidia-device-plugin:$NVIDIA_VERSION"
    kubectl apply -f $BASE/device-plugin/nvidia-device-plugin_${NVIDIA_VERSION}.yaml
elif [[ "$param" = "enflame" ]]; then
    if [[ ! -f "$imageDir/enflame-device-plugin_${ENFLAME_VERSION}.tar" ]];then
        logger error "Enflame k8s device plugin($ENFLAME_VERSION) image not exist."
        exit 1
    fi
    docker load -i "$imageDir/enflame-device-plugin_${ENFLAME_VERSION}.tar"
    docker tag "kubemore/enflame-device-plugin:$ENFLAME_VERSION" \
               "easzlab.io.local:5000/kubemore/enflame-device-plugin:$ENFLAME_VERSION"
    docker push "easzlab.io.local:5000/kubemore/enflame-device-plugin:$ENFLAME_VERSION"
    kubectl apply -f $BASE/device-plugin/enflame-device-plugin_${ENFLAME_VERSION}.yaml
elif [[ "$param" = "biren" ]]; then
    if [[ ! -f "$imageDir/biren-device-plugin_${BIREN_VERSION}.tar" ]];then
        logger error "Biren k8s device plugin(BIREN_VERSION) image not exist."
        exit 1
    fi
    docker load -i "$imageDir/biren-device-plugin_${BIREN_VERSION}.tar"
    docker tag "kubemore/biren-device-plugin:$BIREN_VERSION" \
               "easzlab.io.local:5000/kubemore/biren-device-plugin:$BIREN_VERSION"
    docker push "easzlab.io.local:5000/kubemore/biren-device-plugin:$BIREN_VERSION"
    kubectl apply -f $BASE/device-plugin/biren-device-plugin_${BIREN_VERSION}.yaml
elif [[ "$param" = "hygon" ]]; then
    if [[ ! -f "$imageDir/hygon-device-plugin_${HYGON_VERSION}.tar" ]];then
        logger error "Hygon k8s device plugin($HYGON_VERSION) image not exist."
        exit 1
    fi
    docker load -i "$imageDir/hygon-device-plugin_${HYGON_VERSION}.tar"
    docker tag "kubemore/hygon-device-plugin:$HYGON_VERSION" \
               "easzlab.io.local:5000/kubemore/hygon-device-plugin:$HYGON_VERSION"
    docker push "easzlab.io.local:5000/kubemore/hygon-device-plugin:$HYGON_VERSION"
    kubectl apply -f $BASE/device-plugin/hygon-device-plugin_${HYGON_VERSION}.yaml
else
    logger error "Bad k8s device plugin type: $param"
    exit 1
fi
