#!/usr/bin/env bash

# This script is meant to be used inside the container in
# order to install the necessary tools to correct Verilog problems.

set -e

prefix=/usr/local
njobs=2
sudo=sudo

# Prepare system and install dependencies
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install build-essential curl gperf cmake byacc flex bison libreadline-dev -y

# Download stuff
if [[ ! -f verilog-0.8.7.tar.gz ]]; then
	curl "https://www.cs.upc.edu/~jspedro/cv/verilog-0.8.7.tar.gz" -o verilog-0.8.7.tar.gz
fi
if [[ ! -f verilog-0.8.7-patches.tar.gz ]]; then
	curl "https://www.cs.upc.edu/~jspedro/cv/verilog-0.8.7-patches.tar.gz" -o verilog-0.8.7-patches.tar.gz
fi
if [[ ! -f NuSMV-2.6.0-linux64.tar.gz ]]; then
	curl "http://nusmv.fbk.eu/distrib/NuSMV-2.6.0-linux64.tar.gz" -o NuSMV-2.6.0-linux64.tar.gz
fi

# Unpack stuff
tar xvf verilog-0.8.7.tar.gz
tar xvf verilog-0.8.7-patches.tar.gz
tar xvf NuSMV-2.6.0-linux64.tar.gz

sudo cp -ri NuSMV-2.6.0-Linux/* /usr/local

# Build Icarus Verilog
pushd verilog-0.8.7
for p in ../patches/*; do
	patch -p1 < "$p"
done

./configure --prefix=$prefix
sudo make -j $njobs
popd

# Install stuff
pushd verilog-0.8.7
sudo make install
popd

cat > nusmv-user.conf <<EOF
functor:synth2
functor:synth
functor:syn-rules
-t:dll
flag:DLL=/tmp/jutge-javier-tgt-nusmv/nusmv.tgt
EOF
sudo install nusmv-user.conf $prefix/lib/ivl-0.8/nusmv-user.conf

