#!/bin/bash
#
# Copyright 2016-2017 Huawei Technologies Co., Ltd.
#
# 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.
#
# $1 dirsrc
# $2 autorelease build

set -e

dirsrc=$1

if [ -z "$2" ]; then
    BUILD="snapshots"
else
    BUILD=$2
fi

VERSION="1.1.0-SNAPSHOT"

# docker root dir
ROOT=`git rev-parse --show-toplevel`/test/csit/docker

cd $ROOT
dir=$dirsrc/target
mkdir -p $dir

if [ "$BUILD" = "snapshots" ]; then
    $ROOT/scripts/gen-dockerfiles.py $dirsrc
else
    $ROOT/scripts/gen-dockerfiles.py $dirsrc --build $BUILD
fi

# Update build number in workaround files
for file in `find $dirsrc -name 80-workaround.txt`; do
    sed -i "s|autorelease-[0-9]\{4\}|$BUILD|" $file
done


cp $ROOT/../../../distribution/LICENSE $dir
cp -f $dirsrc/*.txt $dir 2>/dev/null || :

if [ -f $dir/20-mysql.txt ]; then
    cp $ROOT/templates/init-mysql.sh $dir/init-mysql.sh
else
    rm -f $dir/init-mysql.sh
fi


# empty 30-tomcat.txt would be created by gen-dockerfiles.py where required
if [ -f $dir/30-tomcat.txt ]; then
    TOMCAT_VERSION=`$ROOT/scripts/get-tomcat-version.sh`
    cat > $dir/30-tomcat.txt <<EOF
# 30-tomcat.txt - AUTOGENERATED, DO NOT MODIFY MANUALLY
# Set up tomcat
RUN wget -q http://mirrors.ocf.berkeley.edu/apache/tomcat/tomcat-8/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz && tar --strip-components=1 -xf apache-tomcat-${TOMCAT_VERSION}.tar.gz && rm -f apache-tomcat-${TOMCAT_VERSION}.tar.gz && rm -rf webapps && mkdir -p webapps/ROOT
RUN echo 'export CATALINA_OPTS="\$CATALINA_OPTS -Xms64m -Xmx256m -XX:MaxPermSize=64m"' > /service/bin/setenv.sh
ENV CATALINA_HOME /service

EOF
fi



cat > $dir/docker-entrypoint.sh <<EOF
#!/bin/bash
#
# Copyright 2016-2017 Huawei Technologies Co., Ltd.
#
# 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.
#
#
# This file was auto-generated by gen-all-dockerfiles.sh; do not modify manually.
#
# $dir/docker-entrypoint.sh
#

if [ -z "\$SERVICE_IP" ]; then
    export SERVICE_IP=\`hostname -i\`
fi
echo
echo Environment Variables:
echo "SERVICE_IP=\$SERVICE_IP"

EOF

if [ $dirsrc != "common-services-msb" ]; then
    cat >> $dir/docker-entrypoint.sh <<EOF
if [ -z "\$MSB_ADDR" ]; then
    echo "Missing required variable MSB_ADDR: Microservices Service Bus address <ip>:<port>"
    exit 1
fi
echo "MSB_ADDR=\$MSB_ADDR"
echo

# Wait for MSB initialization
echo Wait for MSB initialization
for i in {1..20}; do
    curl -sS -m 1 \$MSB_ADDR > /dev/null && break
    sleep \$i
done

EOF
fi

cat >> $dir/docker-entrypoint.sh <<EOF
echo

# Configure service based on docker environment variables
./instance-config.sh

EOF
if [ -f $dir/20-mysql.txt ]; then
    cat >> $dir/docker-entrypoint.sh <<EOF
# Start mysql
su mysql -c /usr/bin/mysqld_safe &

EOF
fi
if [ -f $dir/25-mongodb.txt ]; then
    cat >> $dir/docker-entrypoint.sh <<EOF
# Start mongodb
mongod &

EOF
fi
cat >> $dir/docker-entrypoint.sh <<EOF
# Perform one-time config
if [ ! -e init.log ]; then
    # Perform workarounds due to defects in release binary
    ./instance-workaround.sh

EOF
if [ -f $dir/20-mysql.txt ]; then
    cat >> $dir/docker-entrypoint.sh <<EOF
    # Init mysql; set root password
    ./init-mysql.sh

EOF
fi
cat >> $dir/docker-entrypoint.sh <<EOF
    # microservice-specific one-time initialization
    ./instance-init.sh

    date > init.log
fi

# Start the microservice
./instance-run.sh

EOF

cat > $dir/Dockerfile <<EOF
#
# This file was auto-generated by gen-all-dockerfiles.sh; do not modify manually.
#
# $dir/Dockerfile
#

EOF

cat $dir/*.txt >> $dir/Dockerfile

for file in instance-config.sh instance-init.sh instance-run.sh instance-workaround.sh; do
    if [ ! -f $dirsrc/$file ]; then
	cp -n $ROOT/templates/instance-script.sh $dirsrc/$file
    fi
done

cp -f $dirsrc/instance-*.sh $dir
touch $dir/instance-config.sh
touch $dir/instance-init.sh
touch $dir/instance-run.sh
touch $dir/instance-workaround.sh
chmod +x $dir/*.sh


cat > $dir/pom.xml <<EOF
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.openo.integration.docker</groupId>
  <artifactId>${dirsrc}</artifactId>
  <version>${VERSION}</version>
  <packaging>docker</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>io.fabric8</groupId>
        <artifactId>docker-maven-plugin</artifactId>
	<version>0.19.0</version>
        <extensions>true</extensions>
	<configuration>
	  <images>
	    <image>
	      <name>openoint/${dirsrc}</name>
	      <build>
		<dockerFileDir>.</dockerFileDir>
		<tags>
		  <tag>latest</tag>
		</tags>
	      </build>
	    </image>
	  </images>
	</configuration>
      </plugin>
    </plugins>
  </build>
</project>
EOF