#!/bin/bash

source _build_dependencies.sh

RED='\033[0;31m'
NC='\033[0m'
FAILED=0

#Checking for step_x/step_y defined in any maps anywhere.
(! grep 'step_[xy]' maps/**/*.dmm)
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}The variables 'step_x' and 'step_y' are present on a map, and they 'break' movement ingame.${NC}"
  FAILED=1
fi

#Checking for 'tag' set to something on maps
(! grep -Pn '( |\t|;|{)tag( ?)=' maps/**/*.dmm)
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}A map has 'tag' set on an atom. It may cause problems and should be removed.${NC}"
  FAILED=1
fi

#Checking for duplicate nanoui templates
(! find nano/templates/ -type f -exec md5sum {} + | sort | uniq -D -w 32 | grep nano)
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}There are identical nanoui template files present.${NC}"
  FAILED=1
fi

#Checking for broken HTML tags (didn't close the quote for class)
(! grep -En "<\s*span\s+class\s*=\s*('[^'>]+|[^'>]+')\s*>" **/*.dm)
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}A broken span tag class is present (check quotes).${NC}"
  FAILED=1
fi

#Checking for bare globals with a leading /
(! grep -En "^/var" **/*.dm)
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}A bare global definition starting with / is present.${NC}"
  FAILED=1
fi

#Checking for bare globals with no /global/ sugar
(! grep -En "^var/(?!global/)" **/*.dm)
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}A bare global definition without the /global/ sugar is present.${NC}"
  FAILED=1
fi

#Checking for lists with trailing attributes
(! grep -En "list/((global/)|(static/)|(tmp/)|(const/))" **/*.dm)
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}A list with trailing /global/, /static/, /tmp/, or /const/ is present.${NC}"
  FAILED=1
fi

#Checking for any 'checked' maps that include 'test'
(! grep 'maps\\.*test.*' *.dme)
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}A map containing the word 'test' is included. This is not allowed to be committed.${NC}"
  FAILED=1
fi

#Check for weird indentation in any .dm files
awk -f tools/indentation.awk **/*.dm
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}Indention testing failed. Please see results and fix indentation.${NC}"
  FAILED=1
fi

#Checking for a change to html/changelogs/example.yml
md5sum -c - <<< "88490b460c26947f5ec1ab1bb9fa9f17 *html/changelogs/example.yml"
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}Do not modify the example.yml changelog file.${NC}"
  FAILED=1
fi

#Checking for color macros
(num=`grep -E '\\\\(red|blue|green|black|b|i[^mc])' **/*.dm | wc -l`; echo "$num escapes (expecting 0)"; [ $num -le 0 ])
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}Do not use any byond color macros (such as \blue), they are deprecated.${NC}"
  FAILED=1
fi

#Checking for missed tags
python tools/TagMatcher/tag-matcher.py ../..
retVal=$?
if [ $retVal -ne 0 ]; then
  echo -e "${RED}Some HTML tags are missing their opening/closing partners. Please correct this.${NC}"
  FAILED=1
fi

# Quit with our status code
exit $FAILED
