#!/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 )"

INSTALL_DIR=$DIRNAME/karaf-full-$(date +%d%m%Y-%H%M)

mkdir -p $INSTALL_DIR/bin
mkdir -p $INSTALL_DIR/etc
mkdir -p $INSTALL_DIR/lib
mkdir -p $INSTALL_DIR/system
mkdir -p $INSTALL_DIR/data
mkdir -p $INSTALL_DIR/systemd/system

sudo systemctl stop karaf
echo "Stopped karaf"

cp -R /opt/karaf/bin/* $INSTALL_DIR/bin
cp -R /opt/karaf/etc/* $INSTALL_DIR/etc
cp -R /opt/karaf/lib/* $INSTALL_DIR/lib
cp -R /opt/karaf/system/* $INSTALL_DIR/system
cp -R /opt/karaf/data/* $INSTALL_DIR/data
cp /etc/systemd/system/karaf $INSTALL_DIR/systemd/system
cp /etc/systemd/system/karaf.service $INSTALL_DIR/systemd/system

sudo systemctl start karaf
echo "Started karaf"

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

#pushd ./$INSTALL_DIR/etc
#$DIRNAME/change_ips.sh
#popd

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

pushd $INSTALL_DIR
tar czvf $DIRNAME/karaf_full_$ESB_ENTAXY_VERSION.tgz *
popd

rm -rf $INSTALL_DIR
