#!/bin/sh

###
# ~~~~~~licensing~~~~~~
# karaf-features
# ==========
# Copyright (C) 2020 - 2021 EmDev LLC
# ==========
# 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.
# ~~~~~~/licensing~~~~~~
###

# создает архив с обновлением

DIRNAME="$( cd "$(dirname "$0")" ; pwd -P )"

if test -f "$DIRNAME/version"; then
    ESB_ENTAXY_VERSION=`cat $DIRNAME/version`
else
    ESB_ENTAXY_VERSION=1.8.0
fi
echo "Create patch version $ESB_ENTAXY_VERSION"

RELEASE_DIR=$DIRNAME/karaf-$(date +%d%m%Y-%H%M)
mkdir -p $RELEASE_DIR

mkdir -p $RELEASE_DIR/etc
mkdir -p $RELEASE_DIR/data

sudo systemctl stop karaf
echo "Stopped karaf"

cp -R /opt/karaf/etc/* $RELEASE_DIR/etc
cp -R /opt/karaf/data/* $RELEASE_DIR/data
cp $DIRNAME/change_ips.sh $RELEASE_DIR/etc
cp $DIRNAME/install_patch.sh $RELEASE_DIR/install.sh

sudo systemctl start karaf
echo "Started karaf"

rm -rf $RELEASE_DIR/data/tmp/*
rm -rf $RELEASE_DIR/data/log/*
rm -rf $RELEASE_DIR/data/txlog/*

pushd $RELEASE_DIR
tar czvf $DIRNAME/karaf_update_$ESB_ENTAXY_VERSION.tgz *
popd


rm -rf $RELEASE_DIR

curl -v --user 'deployer:deployer' --upload-file $DIRNAME/README.md http://192.168.122.76:8081/repository/karaf_patch/README.md
curl -v --user 'deployer:deployer' --upload-file $DIRNAME/karaf_update_$ESB_ENTAXY_VERSION.tgz http://192.168.122.76:8081/repository/karaf_patch/karaf_update_$ESB_ENTAXY_VERSION.tgz
echo "Upload path to nexus http://192.168.122.76:8081/service/rest/repository/browse/karaf_patch/"
