#!/bin/sh

SRC="stringPlus.mli stringPlus.ml parsing_helper.mli parsing_helper.ml stringmap.mli stringmap.ml types.mli ptree.mli piptree.mli pitptree.mli  funsymbhash.mli funsymbhash.ml tree.mli tree.ml pitypes.mli param.mli param.ml parser.mli parser.ml lexer.ml pitparser.mli pitparser.ml pitlexer.ml pvqueue.mli pvqueue.ml terms.mli terms.ml termslinks.mli termslinks.ml display.mli display.ml termsEq.mli termsEq.ml reduction_helper.mli reduction_helper.ml evaluation_helper.mli evaluation_helper.ml history.mli history.ml pievent.mli pievent.ml weaksecr.mli weaksecr.ml noninterf.mli noninterf.ml database.mli database.ml selfun.mli selfun.ml rules.mli rules.ml syntax.mli syntax.ml tsyntax.mli tsyntax.ml piparser.mli piparser.ml pilexer.ml spassout.mli spassout.ml simplify.mli simplify.ml pisyntax.mli pisyntax.ml pitsyntax.mli pitsyntax.ml pitransl.mli pitransl.ml pitranslweak.mli pitranslweak.ml destructor.mli destructor.ml lemma.mli lemma.ml reduction.mli reduction.ml reduction_bipro.mli reduction_bipro.ml piauth.mli piauth.ml move_new_let.mli move_new_let.ml proswapper.mli proswapper.ml encode_queries.mli encode_queries.ml version.mli version.ml main.ml"

SRCTOTEX="stringPlus.cmx parsing_helper.cmx param.cmx piparser.cmx pilexer.cmx pitparser.cmx pitlexer.cmx fileprint.ml lexertotex.ml pitlexertotex.ml version.mli version.ml proveriftotex.ml"

SRCTOTEXC="stringPlus.cmo parsing_helper.cmo param.cmo piparser.cmo pilexer.cmo pitparser.cmo pitlexer.cmo fileprint.ml lexertotex.ml pitlexertotex.ml version.mli version.ml proveriftotex.ml"

SRCINTERACT="stringPlus.mli stringPlus.ml parsing_helper.mli parsing_helper.ml stringmap.mli stringmap.ml types.mli ptree.mli pitptree.mli funsymbhash.mli funsymbhash.ml tree.mli tree.ml pitypes.mli param.mli param.ml parser.mli parser.ml lexer.ml pitparser.mli pitparser.ml pitlexer.ml types.mli pitypes.mli pvqueue.mli pvqueue.ml terms.mli terms.ml termslinks.mli termslinks.ml display.mli display.ml termsEq.mli termsEq.ml reduction_helper.mli reduction_helper.ml evaluation_helper.mli evaluation_helper.ml simplify.mli simplify.ml history.mli history.ml pievent.mli pievent.ml database.mli database.ml pitsyntax.mli pitsyntax.ml destructor.mli destructor.ml lemma.mli lemma.ml reduction_bipro.mli reduction_bipro.ml display_interact.mli display_interact.ml menu_helper.mli menu_helper.ml reduction_interact.mli reduction_interact.ml convert_repl.mli convert_repl.ml menu_interact.mli menu_interact.ml version.mli version.ml main_interact.ml"

SRCANAL="rusage.c stringPlus.mli stringPlus.ml analyze.ml"
SRCADDTAGS="stringPlus.mli stringPlus.ml addexpectedtags.ml"

if uname -a | egrep -q \(Cygwin\)\|\(MINGW\)
then
    EXEC=.exe
else
    EXEC=
fi

if [ X"$1" = X-nointeract ]
then
    INTERACT=false
    shift
else
    INTERACT=true
fi

case X$1 in
    Xocb.native )
	cd src &&\
	    ocamlbuild -use-ocamlfind main.native &&\
	    ocamlbuild -use-ocamlfind proveriftotex.native &&\
	    ocamlbuild -use-ocamlfind addexpectedtags.native &&\
	    cp _build/main.native ../proverif$EXEC &&\
	    cp _build/proveriftotex.native ../proveriftotex$EXEC &&\
	    cp _build/addexpectedtags.native ../addexpectedtags$EXEC &&\
	    ocamlopt -I +threads -o ../analyze$EXEC unix.cmxa threads.cmxa $SRCANAL &&\
	    rm -f main.native proveriftotex.native addexpectedtags.native stringPlus.cmx stringPlus.cmi stringPlus.o analyze.cmx analyze.cmi analyze.o rusage.o &&\
	    if [ $INTERACT = true ]
	    then
		ocamlbuild -use-ocamlfind main_interact.native &&\
		    cp _build/main_interact.native ../proverif_interact$EXEC &&\
		    rm -f main_interact.native
	    fi
	    cd .. ;;

    Xocb.byte )
	cd src &&\
	    ocamlbuild -use-ocamlfind main.byte &&\
	    ocamlbuild -use-ocamlfind proveriftotex.byte &&\
	    ocamlbuild -use-ocamlfind addexpectedtags.byte &&\
	    cp _build/main.byte ../proverif$EXEC &&\
	    cp _build/proveriftotex.byte ../proveriftotex$EXEC &&\
	    cp _build/addexpectedtags.byte ../addexpectedtags$EXEC &&\
	    ocamlc -I +threads -o ../analyze$EXEC -custom unix.cma threads.cma $SRCANAL &&\
	    rm -f main.byte proveriftotex.byte addexpectedtags.byte stringPlus.cmo stringPlus.cmi analyze.cmo analyze.cmi rusage.o &&\
	    if [ $INTERACT = true ]
	    then
		ocamlbuild -use-ocamlfind main_interact.byte &&\
		    cp _build/main_interact.byte ../proverif_interact$EXEC &&\
		    rm -f main_interact.byte
	    fi
	    cd .. ;;

    Xocb.debug )
	cd src &&\
	    ocamlbuild -tag debug -use-ocamlfind main.byte &&\
	    ocamlbuild -tag debug -use-ocamlfind proveriftotex.byte &&\
	    ocamlbuild -tag debug -use-ocamlfind addexpectedtags.byte &&\
	    cp _build/main.byte ../proverif$EXEC &&\
	    cp _build/proveriftotex.byte ../proveriftotex$EXEC &&\
	    cp _build/addexpectedtags.byte ../addexpectedtags$EXEC &&\
	    ocamlc -g -I +threads -o ../analyze$EXEC -custom unix.cma threads.cma $SRCANAL &&\
	    rm -f main.byte proveriftotex.byte addexpectedtags.byte stringPlus.cmo stringPlus.cmi analyze.cmo analyze.cmi rusage.o &&\
	    if [ $INTERACT = true ]
	    then
		ocamlbuild -tag debug -use-ocamlfind main_interact.byte &&\
		    cp _build/main_interact.byte ../proverif_interact$EXEC &&\
		    rm -f main_interact.byte
	    fi
	    cd .. ;;

    Xocb.profile )
	cd src &&\
	    ocamlbuild -use-ocamlfind -lflag profileprim.o profileprim.o main.native &&\
	    ocamlbuild -use-ocamlfind proveriftotex.native &&\
	    ocamlbuild -use-ocamlfind addexpectedtags.native &&\
	    cp _build/main.native ../proverif$EXEC &&\
	    cp _build/proveriftotex.native ../proveriftotex$EXEC &&\
	    cp _build/addexpectedtags.native ../addexpectedtags$EXEC &&\
	    ocamlopt -I +threads -o ../analyze$EXEC unix.cmxa threads.cmxa $SRCANAL &&\
	    rm -f main.native proveriftotex.native addexpectedtags.native stringPlus.cmx stringPlus.cmi stringPlus.o analyze.cmx analyze.cmi analyze.o rusage.o &&\
	    if [ $INTERACT = true ]
	    then
		ocamlbuild -use-ocamlfind main_interact.native &&\
		    cp _build/main_interact.native ../proverif_interact$EXEC &&\
		    rm -f main_interact.native
	    fi
	    cd .. ;;

    Xprofile )
	cd src &&\
	    ocamlyacc -v parser.mly &&\
	    ocamllex lexer.mll &&\
	    ocamlyacc -v piparser.mly &&\
	    ocamllex pilexer.mll &&\
	    ocamlyacc -v pitparser.mly &&\
	    ocamllex pitlexer.mll &&\
	    ocamlopt -o ../proverif$EXEC unix.cmxa profileprim.c profile.mli profile.ml str.cmxa $SRC &&\
	    ocamllex lexertotex.mll &&\
	    ocamllex pitlexertotex.mll &&\
	    ocamlopt -o ../proveriftotex$EXEC $SRCTOTEX &&\
	    ocamlopt -I +threads -o ../analyze$EXEC unix.cmxa threads.cmxa $SRCANAL &&\
	    ocamlopt -o ../addexpectedtags$EXEC $SRCADDTAGS &&\
	    if [ $INTERACT = true ]
	    then
		ocamlfind ocamlopt -o ../proverif_interact$EXEC -package lablgtk2 -linkpkg unix.cmxa str.cmxa $SRCINTERACT
	    fi;;

    Xnative | X )
	cd src &&\
	    ocamlyacc -v parser.mly &&\
	    ocamllex lexer.mll &&\
	    ocamlyacc -v piparser.mly &&\
	    ocamllex pilexer.mll &&\
	    ocamlyacc -v pitparser.mly &&\
	    ocamllex pitlexer.mll &&\
	    ocamlopt -o ../proverif$EXEC unix.cmxa str.cmxa $SRC &&\
	    ocamllex lexertotex.mll &&\
	    ocamllex pitlexertotex.mll &&\
	    ocamlopt -o ../proveriftotex$EXEC $SRCTOTEX &&\
	    ocamlopt -I +threads -o ../analyze$EXEC unix.cmxa threads.cmxa $SRCANAL &&\
	    ocamlopt -o ../addexpectedtags$EXEC $SRCADDTAGS &&\
	    if [ $INTERACT = true ]
	    then
		ocamlfind ocamlopt -o ../proverif_interact$EXEC -package lablgtk2 -linkpkg unix.cmxa str.cmxa $SRCINTERACT
	    fi;;

    Xbyte )
	cd src &&\
	    ocamlyacc -v parser.mly &&\
	    ocamllex lexer.mll &&\
	    ocamlyacc -v piparser.mly &&\
	    ocamllex pilexer.mll &&\
	    ocamlyacc -v pitparser.mly &&\
	    ocamllex pitlexer.mll &&\
	    ocamlc -o ../proverif$EXEC unix.cma str.cma $SRC &&\
	    ocamllex lexertotex.mll &&\
	    ocamllex pitlexertotex.mll &&\
	    ocamlc -o ../proveriftotex$EXEC $SRCTOTEXC &&\
	    ocamlc -I +threads -o ../analyze$EXEC -custom unix.cma threads.cma $SRCANAL &&\
	    ocamlc -o ../addexpectedtags$EXEC $SRCADDTAGS &&\
	    if [ $INTERACT = true ]
	    then
		ocamlfind ocamlc -o ../proverif_interact$EXEC -package lablgtk2 -linkpkg unix.cma str.cma $SRCINTERACT
	    fi;;

    Xdebug )
	cd src &&\
	    ocamlyacc -v parser.mly &&\
	    ocamllex lexer.mll &&\
	    ocamlyacc -v piparser.mly &&\
	    ocamllex pilexer.mll &&\
	    ocamlyacc -v pitparser.mly &&\
	    ocamllex pitlexer.mll &&\
	    ocamlc -g -o ../proverif$EXEC unix.cma str.cma $SRC &&\
	    ocamllex lexertotex.mll &&\
	    ocamllex pitlexertotex.mll &&\
	    ocamlc -g -o ../proveriftotex$EXEC $SRCTOTEXC &&\
	    ocamlc -g -I +threads -o ../analyze$EXEC -custom unix.cma threads.cma $SRCANAL &&\
	    ocamlc -g -o ../addexpectedtags$EXEC $SRCADDTAGS &&\
	    if [ $INTERACT = true ]
	    then
		ocamlfind ocamlc -g -o ../proverif_interact$EXEC -package lablgtk2 -linkpkg unix.cma str.cma $SRCINTERACT
	    fi;;

    Xclean )
	rm -f proveriftotex$EXEC
	rm -f proverif$EXEC
        rm -f proverif_interact$EXEC
	rm -f analyze$EXEC
	rm -f addexpectedtags$EXEC
	rm -f src/*lexer.ml src/*lexertotex.ml src/*parser.ml src/*parser.mli src/*.cmx src/*.cmo src/*.cmi src/*.o
	rm -rf src/_build ;;

    Xinstall )
	cp proverif$EXEC $2/bin/proverif$EXEC &&\
	chmod +x $2/bin/proverif$EXEC &&\
	cp proveriftotex$EXEC $2/bin/proveriftotex$EXEC &&\
	chmod +x $2/bin/proveriftotex$EXEC &&\
	mkdir -p $2/doc/proverif &&\
	mkdir -p $2/share/proverif &&\
	cp analyze$EXEC $2/doc/proverif/analyze$EXEC &&\
	chmod +x $2/doc/proverif/analyze$EXEC &&\
	cp addexpectedtags$EXEC $2/doc/proverif/addexpectedtags$EXEC &&\
	chmod +x $2/doc/proverif/addexpectedtags$EXEC &&\
	cp -r examples $2/doc/proverif &&\
	cp docs/manual-untyped.pdf README LICENSE $2/doc/proverif &&\
	cp cryptoverif.pvl cssproverif.css $2/share/proverif &&\
	cp -r emacs $2/share/proverif/emacs &&\
	if [ $INTERACT = true ]
	then
	    cp proverif_interact$EXEC $2/bin/proverif_interact$EXEC &&\
	    chmod +x $2/bin/proverif_interact$EXEC
	fi;;

    *)
	echo Unknown option
esac
