#!/bin/bash
# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e

groupId=org.yuanrong.m2s
version="v0.0.1"

readonly USAGE="
Usage: bash build.sh [-h] [-v <version>]

Options:
    -h Output this help and exit.
    -v Specified install version.

Example:
1) install specified version into local maven repository.
  $ bash install-adapter.sh -v 0.6.3
"

log_info()
{
    echo "[INFO][$(date +%b\ %d\ %H:%M:%S)][spring-adapter]$*"
}

log_error()
{
    echo "[ERROR][$(date +%b\ %d\ %H:%M:%S)][spring-adapter]$*"
}

while getopts "hv:" opt; do
    case "${opt}" in
    h)
        echo -e "${USAGE}"
        exit 0
        ;;
    v)
        version="${OPTARG}"
        log_info "install version: ${OPTARG}"
        ;;

    *)
        log_error "invalid options: $opt"
        exit 1
        ;;
    esac
done

install_jars() {
    mvn install:install-file -Dfile=microservice-parent.xml -DgroupId=${groupId} -DartifactId=microservice -Dversion=${version} -Dpackaging=pom -N
    mvn install:install-file -Dfile=microservice-adapter.xml -DgroupId=${groupId} -DartifactId=microservice-adapter -Dversion=${version} -Dpackaging=pom -N
    mvn install:install-file -Dfile=microservice-runtime.xml -DgroupId=${groupId} -DartifactId=microservice-runtime -Dversion=${version} -Dpackaging=pom -N
    mvn install:install-file -Dfile=microservice-function.xml -DgroupId=${groupId} -DartifactId=microservice-function -Dversion=${version} -Dpackaging=pom -N

    mvn install:install-file -Dfile=microservice-adapter-common-${version}.jar  -DgroupId=${groupId} -DartifactId=microservice-adapter-common -Dversion=${version} -Dpackaging=jar  -DpomFile=microservice-adapter-common.xml
    mvn install:install-file -Dfile=microservice-adapter-core-${version}.jar -DgroupId=${groupId} -DartifactId=microservice-adapter-core -Dversion=${version} -Dpackaging=jar -DpomFile=microservice-adapter-core.xml
    mvn install:install-file -Dfile=microservice-adapter-springboot2-${version}.jar -DgroupId=${groupId} -DartifactId=microservice-adapter-springboot2 -Dversion=${version} -Dpackaging=jar -DpomFile=microservice-adapter-springboot2.xml
    mvn install:install-file -Dfile=microservice-runtime-api-${version}.jar -DgroupId=${groupId} -DartifactId=microservice-runtime-api -Dversion=${version} -Dpackaging=jar -DpomFile=microservice-runtime-api.xml
    mvn install:install-file -Dfile=microservice-refresh-${version}.jar -DgroupId=${groupId} -DartifactId=microservice-refresh -Dversion=${version} -Dpackaging=jar -DpomFile=microservice-refresh.xml


    mvn install:install-file -Dfile=microservice-sdk.xml -DgroupId=${groupId} -DartifactId=microservice-sdk -Dversion=${version} -Dpackaging=pom -N
    mvn install:install-file -Dfile=microservice-runtime-yuanrong-${version}.jar -DgroupId=${groupId} -DartifactId=microservice-runtime-yuanrong -Dversion=${version} -Dpackaging=jar -DpomFile=microservice-runtime-yuanrong.xml
    mvn install:install-file -Dfile=microservice-function-yuanrong-${version}.jar -DgroupId=${groupId} -DartifactId=microservice-function-yuanrong -Dversion=${version} -Dpackaging=jar -DpomFile=microservice-function-yuanrong.xml
    mvn install:install-file -Dfile=microservice-sdk-functionclient-${version}.jar -DgroupId=${groupId} -DartifactId=microservice-sdk-functionclient -Dversion=${version} -Dpackaging=jar -DpomFile=microservice-sdk-functionclient.xml
    mvn install:install-file -Dfile=microservice-sdk-functionclient-starter-${version}.jar -DgroupId=${groupId} -DartifactId=microservice-sdk-functionclient-starter -Dversion=${version} -Dpackaging=jar -DpomFile=microservice-sdk-functionclient-starter.xml
    mvn install:install-file -Dfile=microservice-sdk-spring-processor-${version}.jar -DgroupId=${groupId} -DartifactId=microservice-sdk-spring-processor -Dversion=${version} -Dpackaging=jar -DpomFile=microservice-sdk-spring-processor.xml
}

install_jars