apiVersion: v1
kind: ConfigMap
metadata:
  name: "@(Model.Name)"
  namespace: "@(Model.NameSpace)"
data:
  prepare: |
    bash /kqi/scripts/common/show-logo
    bash /kqi/scripts/common/prepare-kqi-conf

    if [ -n "$MODEL_REPOSITORY" ] || [ -n "$MODEL_REPOSITORY_URL" ] || [ -n "$COMMIT_ID" ]; then
        bash /kqi/scripts/common/prepare-git
    fi
    
    if [ -n "$DATASET_ID" ]; then
        echo "------------------------- [KAMONOHASHI Prepare] deploy dataset $DATASET_ID into /kqi/input . -------------------------"
        kqi dataset get -d /kqi/input/meta.json $DATASET_ID && kqi dataset list-path-pairs $DATASET_ID > /kqi/tmp/datapairs.txt
        if [ $? -ne 0 ]; then
            echo "dataset get failed"
        fi
        # default: create symlink to raw-data, $LOCAL_DATASET = True : copy raw-data to local
        prepare_cmd='ln -s "/kqi/raw/${stored_path}" "/kqi/input/${file_path_c_quote}"'
        if [ $LOCAL_DATASET = "True" ]; then
            prepare_cmd='cp "/kqi/raw/${stored_path}" "/kqi/input/${file_path_c_quote}"'
        fi
        while true
        do
            read -r file_path
            read -r stored_path
            if [ -z "$stored_path" ] ; then break; fi
            printf -v file_path_c_quote %b "$file_path"
            mkdir -p /kqi/input/$(dirname "${file_path}") && eval ${prepare_cmd}
            if [ $? -ne 0 ]; then
                echo "create dataset failed"
                exit 0
            fi
        done </kqi/tmp/datapairs.txt
    fi
  main: |
    if which pip3 > /dev/null 2>&1; then
        pip3 install jupyterlab==1.0.4
    else
        pip install jupyterlab==1.0.4
    fi
    if [ ! -L /kqi/output/input ]; then
        ln -s /kqi/input/ /kqi/output/input
        if [ $? -ne 0 ]; then
            echo "You need to change the name of '/kqi/output/input' or delete it. After that, please run again."
        fi
    fi
    if [ ! -L /kqi/output/git ]; then
        ln -s /kqi/git/ /kqi/output/git
        if [ $? -ne 0 ]; then
            echo "You need to change the name of '/kqi/output/git' or delete it. After that, please run again."
        fi
    fi
    if [ ! -L /kqi/output/parent ]; then
        ln -s /kqi/parent/ /kqi/output/parent
        if [ $? -ne 0 ]; then
            echo "You need to change the name of '/kqi/output/parent' or delete it. After that, please run again."
        fi
    fi
    if [ ! -L /kqi/output/inference ]; then
        ln -s /kqi/inference/ /kqi/output/inference
        if [ $? -ne 0 ]; then
            echo "You need to change the name of '/kqi/output/inference' or delete it. After that, please run again."
        fi
    fi
    cd /kqi/git
    @Raw(Model.EntryPoint)
    if [ $? -ne 0 ]; then
        echo "The execution of startup command is failed. Please review it and run again."
    fi
    jupyter lab --ip=0.0.0.0 --allow-root --notebook-dir=/kqi/output --no-browser > /kqi/attach/.notebook.log 2>&1 &
    bash /kqi/scripts/common/wait-ready
  finish: |
    sleep $EXPIRES_IN
    bash /kqi/scripts/common/prepare-kqi-conf
    echo "------------------------- [KAMONOHASHI Finish] -------------------------"
    kqi notebook halt $NOTEBOOK_ID