#!/usr/bin/env bash

unamestr=`uname`
if [[ "$unamestr" == 'Darwin' ]]; then
    # OSX
    if command -v greadlink >/dev/null 2>&1; then
        # macOS readlink doesn't support -f option
        READLINK=greadlink
    else
        echo "OSX 'readlink' command does not support option '-f', please install 'greadlink'. If you use 'brew', you can install 'greadlink' using 'brew install coreutils'"
        exit 1
    fi
else
    READLINK=readlink
fi

leandir=$(dirname $($READLINK -f $0))/../../
leandir=$($READLINK -f $leandir)

if [ "${1: -3}" != ".ir" ]
then
  echo "Error, `.ir` file expected"
  exit 1
fi

exe="${1%%.*}"

$leandir/bin/lean --run lirc.lean $1 && g++ -o $exe -I../../src -L../../bin -std=c++11 $1.cpp -lleanruntime -lgmp -ldl -ljemalloc
