#!/bin/bash
#

project=$1
file=$2
dir=/opt/template

#git_dir=${dir}/ThirdServiceTemplate
#git_url="git@gitee.com:zzwteccom/ThirdServiceTemplate.git"

#if [[ ! -d ${git_dir} ]];then
#  git clone ${git_url} ${git_dir}
#fi

#cd ${git_dir}
#git checkout master || git checkout -t remotes/origin/ master > ${dir}/git.log
#rc=$?
#if [[ ${rc} != '0' ]];then
#  printf "false\n"
#  exit 1
#fi
#git pull origin master >> ${dir}/git.log
#rc=$?
#if [[ ${rc} != '0' ]];then
#  printf "false\n"
#  exit 1
#fi

mkdir -p ${dir}/company

rm -rf ${dir}/company/${project}
outmodeds=`find ${dir}/generate_config/*.yml -type f -mmin +120 -exec ls -l {} \; | awk -F '/' '{print $NF}' | awk -F '.' '{print $1}'`
for outmoded in ${outmodeds[*]};do
  rm -rf ${dir}/company/${outmoded}
  rm -rf ${dir}/generate_config/${outmoded}.yml
done
find ${dir}/generate_config/*.txt -type f -mmin +120 -exec rm -rf {} \;

cp -rf ${dir}/ThirdServiceTemplate ${dir}/company/${project}
cp -f ${dir}/generate_config/${project}.yml ${dir}/company/${project}/src/main/resources/application.yml
cp -f ${dir}/generate_config/${file} ${dir}/company/${project}/src/main/webapp/static/${file}

cd ${dir}/company/${project}
/usr/local/maven/bin/mvn clean compile package -Dmaven.test.skip=true > ${dir}/company/${project}/mvn.log
rc=$?
if [[ ${rc} = '0' ]];then
  printf "true\n"
  mv ${dir}/company/${project}/target/*.jar ${dir}/company/${project}/target/${project}.jar
else
  printf "false\n"
fi
