#!/bin/bash
if [ $CLI_UPDATE ];then
    if [ "TRUE" == `echo $CLI_UPDATE` ];then
        mvn package
    fi
    if [ "true" == `echo $CLI_UPDATE` ];then
        mvn package
    fi
    if [ "True" == `echo $CLI_UPDATE` ];then
        mvn package
    fi    
fi


if [ $GIT_REPO ];then
    echo "single mode (single git repo's checkout->generate->push)."
    GIT_PROD_REPO=$GIT_REPO
    GIT_DEV_REPO=$GIT_REPO
    if [ ! $GIT_BRANCH ];then
        GIT_BRANCH=master
        echo "env GIT_BRANCH does not exist,  using 'master'."
    fi
    GIT_PROD_BRANCH=$GIT_BRANCH
    GIT_DEV_BRANCH=$GIT_BRANCH
    if [ ! $GIT_USER_NAME ];then
        GIT_USER_NAME=ibizlabcli
        echo "env GIT_USER_NAME does not exist,  using 'ibizlabcli'."
    fi
    if [ ! $GIT_USER_EMAIL ];then
        GIT_USER_EMAIL=$GIT_USER_NAME@ibizsys.net
        echo "env GIT_USER_EMAIL does not exist,  using '$GIT_USER_NAME@ibizsys.net'."
    fi
    GIT_DEV_USER_NAME=$GIT_USER_NAME
    GIT_DEV_USER_EMAIL=$GIT_USER_EMAIL
fi

if [ ! $GIT_PROD_REPO ];then
    if [ ! $CODEGEN_CODE_URL ];then
        if [ -d "/generator/output" ];then
            echo "file mode (generate -> /generator/output)."
            FASTOUTPUT=TRUE
        else
            echo "unknow prod git, env GIT_PROD_REPO or CODEGEN_CODE_URL does not exist."
            exit 1
        fi
    fi
    if [ $CODEGEN_CODE_PASSWORD ];then
        if [[ ! "${CODEGEN_CODE_URL}" =~ "@" ]];then
            CODEGEN_CODE_URL=${CODEGEN_CODE_URL/#"http://"/"http://oauth2:${CODEGEN_CODE_PASSWORD}@"}
        else
            CODEGEN_CODE_URL=${CODEGEN_CODE_URL#*@}
            CODEGEN_CODE_URL="http://oauth2:${CODEGEN_CODE_PASSWORD}@"$CODEGEN_CODE_URL
        fi
    fi
    GIT_PROD_REPO=$CODEGEN_CODE_URL
fi

if [ ! $FASTOUTPUT ];then
    echo "extension mode (prod git repo's checkout  ->  generate  -> dev git repo's push)."


    if [ ! $GIT_PROD_BRANCH ];then
        if [ ! $CODEGEN_CODE_BRANCH ];then
            GIT_PROD_BRANCH=master
            echo "env GIT_PROD_BRANCH or CODEGEN_CODE_BRANCH does not exist,  using 'master'."
        else
            GIT_PROD_BRANCH=$CODEGEN_CODE_BRANCH
        fi
    fi

    if [ ! $GIT_DEV_REPO ];then
        if [ ! $CODEGEN_RO_URL ];then
            echo "unknow dev git, env GIT_DEV_REPO or CODEGEN_RO_URL does not exist."
            exit 1
        fi
        GIT_DEV_REPO=$CODEGEN_RO_URL
    fi

    if [ ! $GIT_DEV_BRANCH ];then
        if [ ! $CODEGEN_RO_BRANCH ];then
            GIT_DEV_BRANCH=master
            echo "env GIT_DEV_BRANCH or CODEGEN_RO_BRANCH does not exist,  using 'master'."
        else
            GIT_DEV_BRANCH=$CODEGEN_RO_BRANCH
        fi
    fi

fi


mkdir -p /generator
cd /generator
if [ $FASTOUTPUT ];then
    echo "ln -s /generator/output /generator/prod"
    ln -s /generator/output /generator/prod
else
    rm -rf /generator/prod
    rm -rf /generator/dev
    if [ $GIT_REPO ];then
        git clone -b $GIT_PROD_BRANCH $GIT_PROD_REPO prod
    else
        git clone -b $GIT_PROD_BRANCH --depth=1 $GIT_PROD_REPO prod
    fi
fi

if [ ! $MODELFOLDER ];then
    if [ ! $CODEGEN_MODEL_PATH ];then
        if [ -f "prod/ibizmodel.yaml" ];then
            MODELFOLDER=`sed '/^modelfolder: /!d;s/.*: //' prod/ibizmodel.yaml`
        fi
    else
        MODELFOLDER=$CODEGEN_MODEL_PATH
    fi
fi

if [ ! $MODELFOLDER ];then  
    if [ -f "/model/PSSYSTEM.json" ];then
        MODELFOLDER=/model
    else
        echo "unknow model folder, env MODELFOLDER or CODEGEN_MODEL_PATH does not exist, ibizmodel.yaml not found."
        exit 1
    fi
fi

echo spec dir: $MODELFOLDER
if [ $FASTOUTPUT ];then
    echo "ln -s /generator/output /generator/dev"
    ln -s /generator/output /generator/dev
else
    if [ $GIT_REPO ];then
        echo "ln -s /generator/prod /generator/dev"
        ln -s /generator/prod /generator/dev
    else
        git clone -b $GIT_DEV_BRANCH $GIT_DEV_REPO dev
    fi
fi
cd prod
if [ -f "../dev/generator.yml" ];then
    java ${JAVA_OPTS} -Duser.timezone=$TZ -jar /root/.m2/repository/cn/ibizlab/ibizlab-generator-cli/1.0-SNAPSHOT/ibizlab-generator-cli-1.0-SNAPSHOT.jar generate -i "$MODELFOLDER" -c ../dev/generator.yml -o ../dev
else
    if [ ! $TEMPLATENAME ];then  
        if [ ! $TEMPLATEDIR ];then  
            echo "unknow template, env TEMPLATENAME or TEMPLATEDIR does not exist, generator.yaml not found."
            exit 1
        else
            java ${JAVA_OPTS} -Duser.timezone=$TZ -jar /root/.m2/repository/cn/ibizlab/ibizlab-generator-cli/1.0-SNAPSHOT/ibizlab-generator-cli-1.0-SNAPSHOT.jar generate -i "$MODELFOLDER" -t "$TEMPLATEDIR" -o ../dev
        fi
    else
        if [ ! $TEMPLATEDIR ];then  
            java ${JAVA_OPTS} -Duser.timezone=$TZ -jar /root/.m2/repository/cn/ibizlab/ibizlab-generator-cli/1.0-SNAPSHOT/ibizlab-generator-cli-1.0-SNAPSHOT.jar generate -i "$MODELFOLDER" --embed-template "$TEMPLATENAME:$TEMPLATEVERSION" --embed-template "$TEMPLATENAME2:$TEMPLATEVERSION2" --embed-template "$TEMPLATENAME3:$TEMPLATEVERSION3" --embed-template "$TEMPLATENAME4:$TEMPLATEVERSION4" -o ../dev 
        else
            java ${JAVA_OPTS} -Duser.timezone=$TZ -jar /root/.m2/repository/cn/ibizlab/ibizlab-generator-cli/1.0-SNAPSHOT/ibizlab-generator-cli-1.0-SNAPSHOT.jar generate -i "$MODELFOLDER" --embed-template "$TEMPLATENAME:$TEMPLATEVERSION" --embed-template "$TEMPLATENAME2:$TEMPLATEVERSION2" --embed-template "$TEMPLATENAME3:$TEMPLATEVERSION3" --embed-template "$TEMPLATENAME4:$TEMPLATEVERSION4" -t "$TEMPLATEDIR" -o ../dev 
        fi   
    fi
fi
rm -rf ../dev/generator.xml
cd ../dev/


if [ ! $FASTOUTPUT ];then
    if [ ! $GIT_DEV_USER_NAME ];then
        if [ $CODEGEN_RO_USER ];then
            GIT_DEV_USER_NAME=$CODEGEN_RO_USER
        else
            if [ $CODEGEN_CODE_USER ];then
                GIT_DEV_USER_NAME=$CODEGEN_CODE_USER
            else
                GIT_DEV_USER_NAME=ibizlabcli
                echo "env GIT_DEV_USER_NAME or CODEGEN_RO_USER or CODEGEN_CODE_USER does not exist,  using 'ibizlabcli'."
            fi
        fi
    fi
    git config user.name "$GIT_DEV_USER_NAME"

    if [ ! $GIT_DEV_USER_EMAIL ];then
        GIT_DEV_USER_EMAIL=$GIT_DEV_USER_NAME@ibizsys.net
    fi
    git config user.email "$GIT_DEV_USER_EMAIL"

    set +e
    git add .
    git commit -m "generate by $GIT_DEV_USER_NAME. $GIT_PUSH_MESSAGE"
    git push origin $GIT_DEV_BRANCH
    set -e
fi
exit 0