#!/bin/sh

# default program name
progname="onyx-scry"

# or use first cmd line arg
if [ ! -z "$1" ]; then
  progname=$1
fi

if [ -e ${progname}.ts ]; then
  rm ${progname}.ts
fi

# build the source map and concatenate the source
srcmap=$(mktemp)
for f in *.ts; do
  lines=$(wc -l ${f})
  set ${lines}
  lines=$1

  echo "${f}\t${lines}" >> ${srcmap}
  cat ${f} >> ${progname}.ts
done

# generate temporary file for intermediate output
errorOut=$(mktemp)

# compile and write output to temporary file
tsc --strict --target ES2015 --outFile ../static/${progname}.js ${progname}.ts | sed -e s/\(/:/ -e s/,/:/ -e s/\):// | nobs >> ${errorOut}

# if sourcemapper panics you can uncomment this
# cat ${errorOut}

# translate lines into original source with the source map and output to stdout
 ../buildtools/sourcemapper ${errorOut} ${srcmap}


# delete the temporary files
rm ${errorOut}
 rm ${progname}.ts
rm ${srcmap}