#!/bin/zsh

zmodload zsh/stat
setopt extendedglob

dir=`dirname "$0"`
PATH=$dir:$PATH

if [ -z "`git --version`" ]; then
    echo 'This script needs git!' >&2
    exit 1
fi

git=git
if hub --version 2> /dev/null > /dev/null; then
    case "$(hub --version)" in
        *hub\ version*) git=hub;;
    esac
fi

if ! ${git} diff --exit-code > /dev/null || ! ${git} diff --staged --exit-code  > /dev/null ; then
    echo 'Your working directory contains local modifications!' >&2
    exit 1
fi

cd "`${git} rev-parse --show-toplevel`"

if [ -n "`${git} ls-files --other --exclude-standard --exclude=build\*`" ]; then
    echo 'You have untracked files!' >&2
    exit 1
fi

branch=$1
# if ! ${git} rev-parse "$branch" > /dev/null 2>&1; then
#     echo "\"$branch\" is not a valid branch!" >&2
#     exit 1
#fi

trap 'echo "(aborting the merge now)" && ${git} merge --abort' EXIT

echo ".. Test merging of the branch \"$branch\"..."
${git} merge --no-ff --no-commit "$branch" && ${git} status && ${git} diff --staged --summary

if true; then # REMOVE
echo '.. Testing permissions...'
detect_wrong_permissions || exit 1

echo '.. Testing licenses...'
detect_packages_licenses || exit 1
if ! ${git} diff --exit-code > /dev/null; then
    echo '=> There are modifications in licenses (check if all modified/added headers files contain a valid SPDX tag compatible with the one in package_info/PKG/license and that no license text is present):'
    ${git} status --porcelain
    exit 1
fi


fi # REMOVE

if [ -d build ] && \
   [ -f build/CMakeCache.txt ] && \
   [ "$(grep -c -iE 'WITH_(demos|examples|tests):BOOL=(ON|TRUE|1)'  build/CMakeCache.txt)" -eq "3" ]; then
    echo '.. Testing CMake configuration...'
    cmake_output=$(cmake build 2>&1)
    cmake_exit_code=$?
    if [ "$cmake_exit_code" -ne 0 ]; then
        echo 'CMake error:'
        printf '%s\n' "${cmake_output}"
        exit 1
    else
        echo OK
    fi
else
    echo '.. Skip the CMake test (please configure a build/ CMake binary directory,'
    echo 'with -DWITH_tests:BOOL=TRUE  -DWITH_examples:BOOL=TRUE  -DWITH_demos:BOOL=TRUE)'
fi

echo '.. List of new files (please check that it is the expected list:'
new_files=( ${(f)"$(${git} status --porcelain | grep -E '^A')"} )
if [ ${#new_files[@]} -gt 0 ]; then
    string=${(F)new_files}
    echo $string
else
    echo 'No new files.'
fi

#check cmake scripts of tests, examples are present
echo '.. Checking if all CMakeLists.txt are present...'
for i in `ls -d ^build*/examples/*/ ^build*/test/*/ ^build*/demo/^(icons|resources)/`; do
  if ! [ -f $i/CMakeLists.txt ]; then
    echo "Error: $i/CMakeLists.txt does not exist!"
    exit 1
  fi
done

#check all tests/examples are tested
echo '.. Checking if all .cpp files in examples/test are compiled...'
CML_ERRORS=""
for CML in `grep -L "GLOB cppfiles" */test/*/CMakeLists.txt */examples/*/CMakeLists.txt`; do
  DIR=`dirname $CML`

  if [ "Arrangement_on_surface_2/test/Arrangement_on_surface_2" = $DIR ]; then
    continue
  fi
  if [ "Installation/test/Installation" = $DIR ]; then
    continue
  fi

  for i in `ls ${DIR}/*.cpp`; do
    f=`basename $i .cpp`
    if ! grep -q $f ${CML}; then
      CML_ERRORS=`echo "${CML_ERRORS}\n$i is not tested!"`
    fi
  done
done


if [ -n "${CML_ERRORS}" ]; then
  echo -n "Some tests/examples are not tested:"
  echo ${CML_ERRORS}
  exit 1
fi

# check project in cmake scripts is correct
echo '.. Checking if all CMakeLists.txt project names are correct...'
project_name_tests=$(for i in ^build*/test/*/CMakeLists.txt; do pkg=$(echo $i | awk -F "/" '{print $3}'); grep -E "${pkg}_Tests\s*\)" -L $i;  done)
project_name_examples=$(for i in ^build*/examples/*/CMakeLists.txt; do pkg=$(echo $i | awk -F "/" '{print $3}'); grep -E "${pkg}_Examples\s*\)" -L $i;  done)
project_name_demo=$(for i in ^build*/demo/*/CMakeLists.txt; do pkg=$(echo $i | awk -F "/" '{print $3}'); grep -E "${pkg}_Demo\s*\)" -L $i;  done)

if [ -n "${project_name_tests}" ]; then
  echo "CMakeLists with incorrect project name"
  echo ${project_name_tests}
  exit 1
fi

if [ -n "${project_name_examples}" ]; then
  echo "CMakeLists with incorrect project name"
  echo ${project_name_examples}
  exit 1
fi

if [ -n "${project_name_demo}" ]; then
  echo "CMakeLists with incorrect project name"
  echo ${project_name_demo}
  exit 1
fi

# check minimal version is the first instruction in cmake scripts
echo '.. Checking if all CMakeLists.txt starts with cmake_minimum_required...'
cmr_tests=$(for i in ^build*/test/*/CMakeLists.txt;     do pkg=$(echo $i | awk -F "/" '{print $3}'); res=$(egrep -v "^\s*#" $i  | grep -v "^\s*$" | head -n 1 | grep -v cmake_minimum_required); if [ -n "${res}" ]; then echo $pkg; fi; done)
cmr_examples=$(for i in ^build*/examples/*/CMakeLists.txt; do pkg=$(echo $i | awk -F "/" '{print $3}'); res=$(egrep -v "^s*#" $i  | grep -v "^\s*$" | head -n 1 | grep -v cmake_minimum_required); if [ -n "${res}" ]; then echo $pkg; fi; done)
cmr_demo=$(for i in ^build*/demo/*/CMakeLists.txt;     do pkg=$(echo $i | awk -F "/" '{print $3}'); res=$(egrep -v "^s*#" $i  | grep -v "^\s*$" | head -n 1 | grep -v cmake_minimum_required); if [ -n "${res}" ]; then echo $pkg; fi; done)

if [ -n "${cmr_tests}" ]; then
  echo "CMakeLists in test with issues:"
  echo ${cmr_tests}
  exit 1
fi

if [ -n "${cmr_examples}" ]; then
  echo "CMakeLists in examples with issues:"
  echo ${cmr_examples}
  exit 1
fi

if [ -n "${cmr_demo}" ]; then
  echo "CMakeLists in demo with issues:"
  echo ${cmr_demo}
  exit 1
fi

#check header files without SPDX license identifier
echo '.. Checking SPDX license identifier presence in header files...'
file_without_SPDX_identifiers=$(for pkg in `find */package_info -name 'license.txt' | awk -F "/" '{print $1}'`; do if [ -e ${pkg}/include ]; then find ${pkg}/include -type f \( -name '*.h' -o -name '*.hpp' \) | xargs -r grep -L "SPDX-License-Identifier"; fi; done)
if [ -n "${file_without_SPDX_identifiers}" ]; then
    echo "The following files do not have a SPDX license identifier:"
    echo ${file_without_SPDX_identifiers}
    exit 1
fi

#check header files without $Id$ tags
echo '.. Checking $Id$ tag presence in header files...'
file_without_Id_tag=$(for pkg in `find */package_info -name 'license.txt' | awk -F "/" '{print $1}'`; do if [ -e ${pkg}/include ]; then find ${pkg}/include -type f \( -name '*.h' -o -name '*.hpp' \) | xargs -r grep -L '$Id\$'; fi; done)
if [ -n "${file_without_Id_tag}" ]; then
    echo 'The following files do not have a $Id$ tag:'
    echo ${file_without_Id_tag}
    exit 1
fi

#check header files without $URL$ tags
echo '.. Checking $URL$ tag presence in header files...'
file_without_URL_tag=$(for pkg in `find */package_info -name 'license.txt' | awk -F "/" '{print $1}'`; do if [ -e ${pkg}/include ]; then find ${pkg}/include -type f \( -name '*.h' -o -name '*.hpp' \) | xargs -r grep -L '$URL\$'; fi; done)
if [ -n "${file_without_URL_tag}" ]; then
    echo 'The following files do not have a $URL$:'
    echo ${file_without_URL_tag}
    exit 1
fi

#check GPL header files without license include directive
echo '.. Checking include directives in GPL header files...'
file_without_license_include=$(for pkg in `find */package_info -name 'license.txt' | awk -F "/" '{print $1}'`; do if [ -e ${pkg}/include ]; then find ${pkg}/include -type f -name '*.h' | xargs -r grep -E -l "^#\s*define\s+CGAL_.*_H\s*$" | xargs -r grep -E -l "SPDX-License-Identifier.*[ (]GPL" | xargs -r grep -L "#include <CGAL/license"; fi; done)
if [ -n "${file_without_license_include}" ]; then
    echo "The following files do not have an include directive of the package license header:"
    echo ${file_without_license_include}
    exit 1
fi

#check no file contains non-utf8 characters
echo '.. Checking if non utf-8 characters are used...'
txt_not_utf8=$(git ls-files -z --stage | awk -F"\t" 'BEGIN { RS="\0" }; { printf "%s\n", $2; }' | xargs file -N | grep "text" | grep -E -v "UTF-8|ASCII|CSV|XML|EPS|FIG|assembler source|Perl script|from flex")
if [ -n "${txt_not_utf8}" ]; then
  echo "The following files have non utf-8 characters:"
  echo ${txt_not_utf8}
  exit 1
fi

#check no file contains tab characters
echo '.. Checking if tab character is used...'
file_with_tabs=$(git ls-files --stage | grep -E '\.txt|\.h$|\.cpp|\.hpp' | awk '{print $4}' | xargs grep -P -l '\t')
if [ -n "${file_with_tabs}" ]; then
  echo "The following files have tabs:"
  echo ${file_with_tabs}
  exit 1
fi

#check no file contains trailing whitespaces characters
echo '.. Look for trailing whitespaces...'
files_with_trailingws=$(git ls-files --stage | grep -E '\.txt|\.h$|\.cpp|\.hpp' | awk '{print $4}' | xargs grep -P -l '\s+$')
if [ -n "${files_with_trailingws}" ]; then
  echo "The following files have trailing whitespaces:"
  echo ${files_with_trailingws}
  exit 1
fi

#check all headers in documentation actually exist
echo '.. Checking if all documentation headers exist'
not_existing_headers=$(bash ./Scripts/developer_scripts/check_headers.sh $PWD)
if [ -n "${not_existing_headers}" ]; then
  echo "The following files are referenced in the documentation but do not exist:"
  echo ${not_existing_headers}
  exit 1
fi

#check documented function named parameters exists
echo '.. Checking documented function named parameters'
nps=$(git ls-files --stage | grep -E '\.h' | awk '{print $4}' | xargs grep -E "cgalParamNBegin\{.+\}" | perl -lne 'print for /cgalParamNBegin{(.+)}/' | sort -u )
git_dir=`pwd`
tmp_dir=`mktemp -d`
cd $tmp_dir
echo $tmp_dir
echo "#include <CGAL/Named_function_parameters.h>\nstruct A{};\nint main(){\nA a;\n" > main.cpp
for i in `echo $nps`; do
  echo "  CGAL::parameters::${i}(a).${i}(a);" >> main.cpp
done
echo "}" >> main.cpp
if ! g++ -DCGAL_NO_STATIC_ASSERTION_TESTS -I$git_dir/STL_Extension/include -I$git_dir/Stream_support/include -I$git_dir/Installation/include main.cpp; then
  echo "ERROR: At least one documented named parameter does not exist"
  cd -
  rm -r $tmp_dir
  exit 1;
fi
cd -
rm -r $tmp_dir

current_rev=$(${git} rev-parse HEAD)
trap 'echo "(aborting the merge now)" && ${git} reset --quiet --hard ${current_rev}' EXIT

echo '.. Dummy merge commit...'
conflicts=( ${(f)"$(${git} status --porcelain | awk '/^[^ ][^ ]/ {print $2}')"} )
if [ ${#conflicts[@]} -gt 0 ]; then ${git} add $conflicts || exit 1; fi
${git} commit --allow-empty -m 'dummy merge commit' || exit 1

echo '.. Size of the gzipped bundle'
trap 'echo "(aborting the merge now)" && rm bundle.gz && ${git} reset --quiet --hard ${current_rev}' EXIT
${git} bundle create bundle ${current_rev}..HEAD > /dev/null 2>&1 && gzip bundle || exit 1
size=$(zstat +size bundle.gz)
echo "=> $size"
exit 0
