#! /bin/bash

#OCAMLDIR est le répertoire de la distribution OCaml utilisée par WinCaml 
#Ajuster au besoin OCAMLDIR
#idem pour X11INCLUDE et X11LIB notamment pour macOS

OCAMLDIR="$HOME/.opam/$(opam switch show)"
#OCAMLDIR="$HOME/.opam/5.1.1"
#OCAMLDIR="/opt/amd64/ocaml-5.1.1"
OCAMLBIN="$OCAMLDIR"/bin
OCAMLLIB="$OCAMLDIR"/lib/ocaml
[ -d "$OCAMLLIB" ] || OCAMLLIB="$OCAMLDIR"/lib64

#X11INCLUDE=-I/usr/include
#X11LIB=-L/usr/lib/X11

#X11INCLUDE=-I/opt/X11/include
#X11LIB=-L/opt/X11/lib

#--------------------------------------------------------------------------

export PATH="$OCAMLBIN":$PATH
cd "$(dirname "$0")"


if [ ! -s "$OCAMLBIN"/ocaml ];
then
	echo "ocaml absent dans $OCAMLBIN"
	echo "ajuster la variable OCAMLDIR dans le script build"
	exit 1
fi

[ -e "$OCAMLBIN"/ocamlmklib.byte ] && OCAMLMKLIB="$OCAMLBIN/ocamlmklib.byte" || OCAMLMKLIB="$OCAMLBIN/ocamlmklib"

# biblio graphique X11

tar xvf graph.tar.gz
cd graph

for f in *; do
	echo $f
	sed 's|caml/||' $f > aux && mv aux $f
done

gcc -I"$OCAMLLIB" -I"$OCAMLLIB"/caml $X11INCLUDE -fPIC -c *.c

if [ -e "$OCAMLBIN"/ocamlmklib.opt ];
then
	ocamlmklib.opt -o graphics -oc graphics graphics.mli graphics.ml open.o draw.o fill.o color.o text.o image.o make_img.o dump_img.o point_col.o sound.o events.o subwindow.o $X11LIB -lX11
else
	ocamlrun "$OCAMLMKLIB" -o graphics -oc graphics graphics.mli graphics.ml open.o draw.o fill.o color.o text.o image.o make_img.o dump_img.o point_col.o sound.o events.o subwindow.o $X11LIB -lX11
fi

cd ..
mv graph/dllgraphics.so graph/graphics.cm* .
rm -rf graph

