#!/bin/bash

OutML=src/git_info.ml
OutMLI=src/git_info.mli
OutTMP=tmp.txt

#check that iProver is in a git repo
#if [ -e "./.git" ]
#then
echo "git: iProver is in a git repo; saving iProver git sha1"
echo '(*
  git hash and date of the last commit 
  auto generated by git_info script
  do not modify by hand
  do not add to git
*)
' | tee "$OutML" > "$OutMLI"
    
    #git types 
    echo "type git_info = 
    {
     git_sha1 : string;
     git_date : string;
     git_non_committed_changes : bool;
    }


" | tee -a "$OutML" >> "$OutMLI"

    echo '(** Returns Some(git_info) or None if outside of git repo *)' >> "$OutMLI"
    echo "val git_info_opt : git_info option" >> "$OutMLI"
#    echo "val make_outside_git : bool ref" >>  "$OutMLI"

    if [ -e "./.git" ]
    then
  
        #SHA1
        SHA1="$(git rev-parse --verify HEAD)"
        echo "git: sha1: $SHA1"
        
        #Date 
        Date="$(git show -s --format=%ci $SHA1)"
        echo "git: date: $Date"
        
        #Non committed changes
        if $(git diff-index --quiet HEAD --)
        then
	    #all changes are committed 
	    NCC="false"
        else
	    #there are non committed changes
	    NCC="true"
        fi
        echo "git: non-committed changes: $NCC"
        
        echo "let git_info_opt = 
     Some({   
     git_sha1 = \"$SHA1\";
     git_date  = \"$Date\"; 
     git_non_committed_changes =$NCC;
     })                          
" >> "$OutML"
    else
        echo "git: iProver is not in a git repo; use stored git sha1 in iProver"
        echo "let git_info_opt = None" >> "$OutML"
    fi
#    echo "let make_outside_git = ref false;;" >> "$OutML"
        
#else
#    echo "git: iProver is not in a git repo; use stored git sha1 in iProver"
#    echo "make_outside_git := true;;"  >> "$OutML"
#fi

