#!/bin/bash
# The primary purpose of this script is to run all the tests and upload 
# the results to the nightly-results server. It also generates some HTML
# to display the equiderivability tests in a nicely-formatted way.

# Takes no arguments, unless in LOCAL mode
# see infra/localnightly.sh for an example of how to run this script locally

echo "Beginning eggcc nightly script..."

# -x: before executing each command, print it
# -e: exit immediately upon first error
set -x -e
# if anything in a pipeline fails, fail the whole pipeline
set -o pipefail

export PATH=~/.cargo/bin:$PATH

rustup update
cargo install tokei

# determine physical directory of this script
src="${BASH_SOURCE[0]}"
while [ -L "$src" ]; do
  dir="$(cd -P "$(dirname "$src")" && pwd)"
  src="$(readlink "$src")"
  [[ $src != /* ]] && src="$dir/$src"
done
MYDIR="$(cd -P "$(dirname "$src")" && pwd)"

# Absolute directory paths
TOP_DIR="$MYDIR/.."
RESOURCE_DIR="$MYDIR/nightly-resources"
NIGHTLY_DIR="$TOP_DIR/nightly"
OUTPUT_DIR="$NIGHTLY_DIR/output"
GRAPHS_DIR="$NIGHTLY_DIR/output/graphs"
DATA_DIR="$TOP_DIR/nightly/data"

# Make sure we're in the right place
cd $MYDIR
echo "Switching to nighly script directory: $MYDIR"

# Clean previous nightly run
# CAREFUL using -f
if [ "$1" == "--update" ]; then
  echo "updating front end only (output folder) due to --update flag"
  rm -rf $OUTPUT_DIR
  mkdir -p "$OUTPUT_DIR" "$GRAPHS_DIR"
else
  rm -rf $NIGHTLY_DIR
  # Prepare output directories
  mkdir -p "$NIGHTLY_DIR" "$NIGHTLY_DIR/data" "$NIGHTLY_DIR/data/llvm" "$OUTPUT_DIR" "$GRAPHS_DIR"
fi



pushd $TOP_DIR

# Run profiler.
# locally, run on argument
if [ "$1" == "--update" ]; then
  echo "skipping profile.py, updating front end"
elif [ "$LOCAL" != "" ]; then
  ./infra/profile.py "$DATA_DIR" "$@" 2>&1 | tee $NIGHTLY_DIR/log.txt
else
  export LLVM_SYS_180_PREFIX="/usr/lib/llvm-18/"
  make runtime
  # run on all benchmarks in nightly
  ./infra/profile.py "$DATA_DIR" benchmarks/passing  2>&1 | tee $NIGHTLY_DIR/log.txt
fi

# CFGs now generated inside profile.py (removed separate generate_cfgs.py call)

# generate the plots
# needs to know what the benchmark suites are
./infra/graphs.py "$OUTPUT_DIR" "$NIGHTLY_DIR/data/profile.json" benchmarks/passing 2>&1 | tee $NIGHTLY_DIR/log.txt

# Generate latex after running the profiler (depends on profile.json)
./infra/generate_line_counts.py "$DATA_DIR" 2>&1 | tee $NIGHTLY_DIR/log.txt

popd

# Update HTML index page.
cp "$RESOURCE_DIR"/* "$OUTPUT_DIR"

# Copy data directory to the artifact
cp -r "$NIGHTLY_DIR/data" "$OUTPUT_DIR/data"

# Copy log
cp "$NIGHTLY_DIR/log.txt" "$OUTPUT_DIR"

# gzip all JSON and svgs in the nightly dir
if [ "$LOCAL" == "" ]; then
  gzip "$OUTPUT_DIR/data/profile.json"
  find "$OUTPUT_DIR" -name '*.svg' -exec gzip {} +
  find "$OUTPUT_DIR" -name '*.ll' -exec gzip {} +
fi
