#!/bin/bash

tmpdir=/tmp/fusionprep

# When we are in 2018 ... add: -`date +%Y`
copyright="<?php // Copyright (c) 2017-2019 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/fusion-$2.tar.gz"
else
  tarball="$tmpdir/fusion${1:+-$1}.tar.gz"
fi

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

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

# Jminify some javascript
# NOTE: Disabled until jsmin is located on actual build box... not inside fusion
#for f in "$tmpdir"/nagiosfusion/fusion/html/static/js/*.js; do
#   tmpout="$f.temp"
#   "$tmpdir/nagiosfusion/tools/jsmin" < "$f" > "$tmpout"
#   mv -f "$tmpout" "$f"
#   unset tmpout
#done

###########Pull In Latest Components, Dashlets##############
# NOTE: removed as fusion is now a monorepo -AC

# Remove svn or git dirs from fusion trunk and all components
find "$tmpdir" -name .svn -type d  -exec rm -rf {} \;
find "$tmpdir" -name .git -type d  -exec rm -rf {} \;

# Remove git placeholders for empty folders
find "$tmpdir" -name .keep -type f  -exec rm -rf {} \;

# 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/FS/" in the teamIT folder on ofiler
if [ "$1" != "BUILD" ]; then
  cd $tmpdir
  tar czf fusion-$2.tar.gz .
  scp fusion-$2.tar.gz "/store/groups/teamIT/For Steve K/Products/FS/fusion-$2.tar.gz.tmp"
  sleep 5
  mv "/store/groups/teamIT/For Steve K/Products/FS/fusion-$2.tar.gz.tmp" "/store/groups/teamIT/For Steve K/Products/FS/fusion-$2.tar.gz"
  sleep 5
  
  rm -f fusion-$2.tar.gz
  cd /tmp/nagiosfusion
fi

clear
echo -n "Encoding PHP files with SourceGuardian..."
echo ""
# Encode files with SourceGuardian
/usr/local/sourceguardian/sourceguardian -b- --phpversion 5.3 --phpversion 5.4 --phpversion 5.5 --phpversion 5.6 --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 --phpversion 8.4 -p "$copyright" @/tmp/fusionprep/nagiosfusion/setup/sg/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 "setup/sg/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 "setup/sg/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

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

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

# Remove some other files from the prep dir
rm -f "$tmpdir/nagiosfusion/prep-release"
rm -f "$tmpdir/nagiosfusion/update-version"
rm -f "$tmpdir/nagiosfusion/README.txt"
rm -f "$tmpdir/nagiosfusion/FUTURE.txt"
rm -rf "$tmpdir/nagiosfusion/testing"
rm -f "$tmpdir/nagiosfusion/fusion/cron/test.php"
rm -f "$tmpdir/nagiosfusion/fusion/html/test.php"

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