#!/bin/bash

tmpdir=/tmp/nagioslogserverprep
copyright="<?php // Copyright (c) 2008-`date +%Y` Nagios Enterprises, LLC.  All rights reserved. ?>"

# Version to be included in the tarball filename
# Specified as first command line argument
if [ "x$1" = "xAUX" ]; then
	tarball="$tmpdir/nagioslogserver-$2.tar.gz"
else
	tarball="$tmpdir/nagioslogserver${1:+-$1}.tar.gz"
    release="yes"
fi
#tarball="$tmpdir/nagioslogserver${2:+-$2}.tar.gz"

# Delete prep dir if it already exists
if [ -d "$tmpdir" ]; then
	rm -rf "$tmpdir/nagioslogserver" "$tarball"
fi

# Create prep dir and copy files over
mkdir -p "$tmpdir/nagioslogserver"
cp -r  * "$tmpdir/nagioslogserver"

# If the build is for a release then this creates a different unencrypted tarball per Ethan G's request
# It puts in in a folder called "For Steve K/Products/LS/" in the teamIT folder on ofiler
if [ "$1" != "BUILD" ]; then
	cd $tmpdir
    tar czf nagioslogserver-$2.tar.gz .
    scp nagioslogserver-$2.tar.gz "/store/groups/teamIT/For Steve K/Products/LS/nagioslogserver-$2.tar.gz.tmp"
	sleep 5
	mv "/store/groups/teamIT/For Steve K/Products/LS/nagioslogserver-$2.tar.gz.tmp" "/store/groups/teamIT/For Steve K/Products/LS/nagioslogserver-$2.tar.gz"
	sleep 5	
    rm -f nagioslogserver-$2.tar.gz
    cd /tmp/nagioslogserver
fi

echo -n "Encoding PHP files with SourceGuardian..."
echo ""
sourceguardian -b- --phpversion 7.0 --phpversion 7.1 --phpversion 7.2 --phpversion 7.3 --phpversion 7.4 --phpversion 8.0 --phpversion 8.1 --phpversion 8.2 --phpversion 8.3 -p "$copyright" @"$tmpdir"/nagioslogserver/sourceguardian/flist

error_code=$?
if [ $error_code == 0 ]; then
  echo "Successfully encoded"
elif [ $error_code == 18 ]; then
  echo ""
  echo ""
  echo ""
  echo "========================================================"
  echo "Sourceguardian failed to encrypt some files, exiting now"
  echo "Due to the encoding failure DO NOT RELEASE!!"
  echo ""
  echo ""
  echo ""
  exit $error_code
elif [ $error_code == 2 ]; then
  echo ""
  echo ""
  echo ""
  echo "===================================================================================="
  echo "There were some php syntax issues, please review the log to see what the issues were"
  echo "Due to the encoding failure DO NOT RELEASE!!"
  echo ""
  echo ""
  echo ""
  exit $error_code
else
  echo ""
  echo ""
  echo "" 
  echo "======================="
  echo "There were other issues"
  echo "Due to the encoding failure DO NOT RELEASE!!" 
  echo ""
  echo ""
  echo ""
  exit $error_code
fi

echo "=========================================="
echo "SourceGuardian Encryption Verification..."
echo ""

# PROD - Check for (presence) of test artifacts - "sg_load"
chmod +x /root/scripts/sg_verify.sh
/root/scripts/sg_verify.sh -flist "$tmpdir/nagioslogserver/sourceguardian/flist" -searchTxt "sg_load"
sgTrue_error_code=$?

if [ $sgTrue_error_code == 0 ]; then
  echo "SUCCESS - All Files Verified"
  echo "=========================================="
else
  echo "FAILURE - DO NOT RELEASE!!"
  echo "=========================================="
  exit $sgTrue_error_code
fi

# PROD - Alternate Check for (absence) of test artifacts - "sourceguardian-check"

# /root/scripts/sg_verify.sh -flist "$tmpdir/nagioslogserver/sourceguardian/flist" -searchTxt "sourceguardian-check"
# sgFalse_error_code=$?

# if [ $sgFalse_error_code != 0 ]; then
#    echo "SUCCESS - All Files Verified"
#    echo "=========================================="
# else
#    echo "FAILURE - DO NOT RELEASE!!"
#    echo "=========================================="
#    exit $sgFalse_error_code
# fi

if [ "$release" = "yes" ];then
#build kibana dist
(

    cd "$tmpdir/nagioslogserver/subcomponents/kibana/kibana"

    # load cached from if they exist
    if [ -d /tmp/node_modules ];then
        cp -r /tmp/node_modules .
	fi
    npm install

	grunt build --force

    # cache these in temp for re-use
    cp -r node_modules /tmp

    rm -rf docs sample src tasks test node_modules Gruntfile.js index.html .gitignore .jshintrc package.json README.md
    mv dist src
    if [ "$?" -ne "0" ];then
        echo "Grunt build failed, you cannot continue";
        exit 1;
    fi
)
fi


# Delete temp files
find "$tmpdir" -name \*~ -delete

# Delete "installed" marker files
rm -f installed.*

#Remove deploy script
rm -f "$tmpdir/nagioslogserver/basedir/html/deploy.sh"

# Remove some other files from the prep dir
rm -f "$tmpdir/nagioslogserver/sourceguardian/flist"
rm -f "$tmpdir/nagioslogserver/prep-release"

# Remove svn dirs
find "$tmpdir" -name .svn -type d  -exec rm -rf {} \;

# Create a new tarball for shipment
cd $tmpdir
tar -czf "$tarball" -C ./  nagioslogserver/


