#! /bin/bash

export MANDIR=../man

# inla.models.generate call has been moved to the Makefile, to allow
# overriding the R binary

function mmp ()
{
    awk '/:EXTRA:/ {$1=""; extra = $0; next}
   /:NAME:/{
	name=$2
	f=  MANDIR "/" name ".Rd"
	print "Make help page", f >> "/dev/stderr"
	print f >> "/dev/stdout"
        print "%%" > f
        print "%% WARNING! DO NOT EDIT!" >> f
        print "%% This file is automatically generated from " filename  >> f
        print "%%" >> f
	print "\\name{" $2 "}" >> f
	print "\\alias{" $2 "}" >> f
	print "\\alias{inla." $2 "}" >> f
	print "\\alias{inla.set." $2 ".default}" >> f
	print "\\alias{set." $2 ".default}" >> f
	print "\\alias{"$2 ".default}" >> f
	print "\\title{Control variables in " name "}" >> f
	print "\\description{Control variables in \\code{" name "} for use in \\code{inla}}" >>f
	print "\\usage{" >> f
	##print "inla(formula, ..., " name "=list( <arg>=<value>[, <arg>=<value>]))" >>f
        print "inla.set." name ".default(...)" >> f
        print name "(REPLACEME)" >> f
	print "}" >> f
	print "\\arguments{" >> f
	print "\\item{...}{Possible arguments}" >> f
	next;}
    /:ARGUMENT:/{ arg=$2; $1="";$2=""; desc=$0;
	print "\\item{" arg "}{" desc "}" >> f
	next;}
    /:SEEALSO:/ {
	print "}" >> f
	print "\\value{" >> f
        print " " extra >> f
        print "The function \\code{" name "} is used to TAB-complete arguments and returns a list of given arguments." >> f
        print "The function \\code{inla.set." name ".default} returns a list with all the default values of all parameters within this control statement." >> f
        print "}" >> f
        extra = ""
	print "\\seealso{" >> f
	print SEEALSO >> f
	for(i=2;i<=NF-1;i++){
	    print "\\code{\\link{" $i "}}," >> f
	}
	if (NF>1){
	    print "\\code{\\link{" $NF "}}" >> f
	}
        print "}" >> f
	next;
    }' MANDIR=$MANDIR SEEALSO="$SEEALSO" filename="$1"
}	    

SEEALSO=$(grep :NAME: set.default.arguments.R|awk '{printf "\\\\code{\\\\link{%s}}, ",  $2}')
for f in set.default.arguments.R; do
    helppages=$(awk '/:[A-Z]+:/{print}' $f | mmp $f)
done

Rsort () {
    ## sort using R (to get the same ordering)
    export TT_FILE=$(echo ~/tmp/tt)
    export TTT_FILE=$(echo ~/tmp/ttt)

    rm -f $TT_FILE $TTT_FILE
    cat > $TT_FILE
    echo 'write(sort(readLines(Sys.getenv("TT_FILE"))),ncol=1,file=Sys.getenv("TTT_FILE"))' | R --vanilla --slave 2>/dev/null
    cat $TTT_FILE
}

for f in $helppages; do
    ## need to replace REPLACEME with a sorted list of all arguments
    echo "Postprocess $f"
    WITHTHIS=$(grep \\item $f | awk '$0 !~ /[.][.][.]/{print}' | awk -F'}' '{print $1}' | awk -F'{' 'NF > 1{print $2}'|Rsort|awk 'NR==1{a=$1;next;}{printf a ", "; a=$1;};END{printf a}')
    awk '/REPLACEME/{gsub(/REPLACEME/,WITHTHIS);print;next};{print}' WITHTHIS="$WITHTHIS" $f > tt && mv -f tt $f
done

## second part
for f in *.R; do
    if grep -q '##!' $f; then
	
	fname=${f%%.R}.Rd
	{
	    echo "%%" 
	    echo "%% WARNING! DO NOT EDIT!" 
	    echo "%% This file is automatically generated from $f" 
	    echo "%%" 
	    awk '/##!/ { gsub(/^.*##!/, ""); print }' $f  
	} > ../man/$fname
	echo "Make help page from $f"
	
    fi
done

## NAMESPACE generation has been moved to Makefile

