#!/bin/bash
#
#    Program to build all and generate all image server (i686 & x86_64)
#    Copyright (C) 2017  AleaJactaEst
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.

declare -i AUTO=1
declare -i BUILD_i686=0
declare -i GENERATE_CONTAINER_i686=0
declare -i GENERATE_IMAGE_i686=0
declare -i BUILD_x86_64=0
declare -i GENERATE_CONTAINER_x86_64=0
declare -i GENERATE_IMAGE_x86_64=0
declare -i CLEAN=0
declare -i SAVE_IMAGES=0

calldir="$(dirname $0)"
basedir=$(cd $calldir; pwd)

usage()
{
cat << EOF
usage:$0 [options]
  script to build all environment

options:
  -h, --help                  : Show this help
  -d, --debug                 : Show debug message
  --build-i686                : Build server and client i686
  --generate-container-i686   : Generate container docker for i686 (share data with host)
  --generate-image-i686       : Generate image docker for i686 (image have all opennel)
  --build-x86_64              : Build server and client x86_64
  --generate-container-x86_64 : Generate container docker for x86_64 (share data with host)
  --generate-image-x86_64     : Generate image docker for x86_64 (image have all opennel)
  --clean                     : remove build & server data
  --save-docker-images        : save all docker images
EOF
}

function msg_debug()
{
    if [[ $DEBUG -ne 0 ]]
    then
        echo "$(date "+%Y/%m/%d %H:%M:%S") DEBUG - $*"
    fi
}

function msg_info()
{
    msg="$(date "+%Y/%m/%d %H:%M:%S") INFO - $*"
    echo "$msg"
    if [[ -d ${basedir}/build ]]
    then
        echo "$msg" >> ${basedir}/build/state.log
    fi
}

function msg_error()
{
    msg="$(date "+%Y/%m/%d %H:%M:%S") ERROR - $*"
    echo "$msg"
    if [[ -d ${basedir}/build ]]
    then
        echo "$msg" >> ${basedir}/build/state.log
    fi
    exit 2
}

while test $# -gt 0
do
    case "$1" in
    -h|--help)
        usage
        exit 1
        ;;
    -d|--debug)
        DEBUG=1
        shift
        ;;
    --build-i686)
        AUTO=0
        BUILD_i686=1
        shift
        ;;
    --generate-container-i686)
        AUTO=0
        GENERATE_CONTAINER_i686=1
        shift
        ;;
    --generate-image-i686)
        AUTO=0
        GENERATE_IMAGE_i686=1
        shift
        ;;
    --build-x86_64)
        AUTO=0
        BUILD_x86_64=1
        shift
        ;;
    --generate-container-x86_64)
        AUTO=0
        GENERATE_CONTAINER_x86_64=1
        shift
        ;;
    --generate-image-x86_64)
        AUTO=0
        GENERATE_IMAGE_x86_64=1
        shift
        ;;
    --clean)
        CLEAN=1
        shift
        ;;
    --save-docker-images)
        SAVE_IMAGES=1
        shift
        ;;
    *)
        usage
        msg_error "options '$1' not recognize"
        ;;
    esac
done

msg_info "[$(basename $0):$LINENO] INIT"
if [[ $AUTO -eq 1 ]]
then
    BUILD_i686=1
    GENERATE_CONTAINER_i686=1
    GENERATE_IMAGE_i686=1
    BUILD_x86_64=1
    GENERATE_CONTAINER_x86_64=1
    GENERATE_IMAGE_x86_64=1
fi

if [[ $CLEAN -ne 0 ]]
then
    msg_debug "[$(basename $0):$LINENO] CLEAN"
    rm -rf "$basedir/build" "$basedir/output" || msg_error "[$(basename $0):$LINENO] CLEAN"
fi

mkdir -p "${basedir}/build"
msg_info "[$(basename $0):$LINENO] BEGIN"

if [[ $BUILD_i686 -ne 0 ]]
then
    msg_debug "[$(basename $0):$LINENO] BUILD DEBIAN JESSIE i686"
    $basedir/builder/debian/jessie/i686/build.sh -c -j 12 -a '-DWITH_SSE2=OFF' -a '-DWITH_SYMBOLS=ON'  -m '-m 20g' -d || msg_error "[$(basename $0):$LINENO] BUILD DEBIAN JESSIE i686"
fi

if [[ $GENERATE_CONTAINER_i686 -ne 0 ]]
then
    msg_debug "[$(basename $0):$LINENO] GENERATE SERVER CONTAINER DEBIAN JESSIE i686"
    $basedir/server/debian/jessie/i686/server-container.sh -r -b -k -w -c -n ||  msg_error "[$(basename $0):$LINENO] GENERATE SERVER CONTAINER DEBIAN JESSIE i686"
fi

if [[ $GENERATE_IMAGE_i686 -ne 0 ]]
then
    msg_debug "[$(basename $0):$LINENO] GENERATE SERVER IMAGE DEBIAN JESSIE i686"
    $basedir/server/debian/jessie/i686/server-image.sh -b -k -w -c -n ||  msg_error "[$(basename $0):$LINENO] GENERATE SERVER IMAGE DEBIAN JESSIE i686"
fi

if [[ $BUILD_x86_64 -ne 0 ]]
then
    msg_debug "[$(basename $0):$LINENO] BUILD DEBIAN JESSIE x86_64"
    $basedir/builder/debian/jessie/x86_64/build.sh -c -j 12 -a '-DWITH_SYMBOLS=ON'  -m '-m 20g' -d || msg_error "[$(basename $0):$LINENO] BUILD DEBIAN JESSIE x86_64"
fi

if [[ $GENERATE_CONTAINER_x86_64 -ne 0 ]]
then
    msg_debug "[$(basename $0):$LINENO] GENERATE SERVER CONTAINER DEBIAN JESSIE x86_64"
    $basedir/server/debian/jessie/x86_64/server-container.sh -r -b -k -g -w -c -n || msg_error "[$(basename $0):$LINENO] GENERATE SERVER CONTAINER DEBIAN JESSIE x86_64"
fi

if [[ $GENERATE_IMAGE_x86_64 -ne 0 ]]
then
    msg_debug "[$(basename $0):$LINENO] GENERATE SERVER IMAGE DEBIAN JESSIE x86_64"
    $basedir/server/debian/jessie/x86_64/server-image.sh -b -k -w -c -n || msg_error "[$(basename $0):$LINENO] GENERATE SERVER IMAGE DEBIAN JESSIE x86_64"
fi

if [[ $SAVE_IMAGES -ne 0 ]]
then
    mkdir -p $basedir/output/images || msg_error "[$(basename $0):$LINENO] Impossible to create $basedir/output/images"
    listimages=( $(docker images --filter=reference='opennel/*' --format "{{.Repository}}") )

    for image in ${listimages[@]}
    do
        mkdir -p $(dirname $basedir/output/images/$image) || msg_error "[$(basename $0):$LINENO] Impossible to create $(dirname $basedir/output/images/$image)"
        docker save -o "$basedir/output/images/$image.tar" "$image" || msg_error "[$(basename $0):$LINENO] Impossible to save image $image"
    done
fi

msg_info "END"