#!/bin/sh
#
#  Usage: ./mkdoc [-u]
#
#  -u will update the existing R.pmdoc if present, otherwise it is removed first
#

echo " - Detecting build settings"

if [ -z "${FWPATH}" ]; then
    FWPATH=/Library/Frameworks/R.framework
fi

## use leopard or mavericks?
TEMPLATE=R-template.pmdoc
if [ `uname -r | sed 's:\..*::'` = 13 ]; then
    TEMPLATE=R-mavericks-template.pmdoc
fi
if [ `uname -r | sed 's:\..*::'` = 15 ]; then
    TEMPLATE=R-el-capitan-template.pmdoc
fi


RBIN="${FWPATH}/Resources/bin/R"
VERFULL=`"${RBIN}" --version|sed -n -e 's/R version \(.\{0,\}\)$/\1/p'`
if [ -z "${VERFULL}" ]; then
    ## since 2011/07/04 R-devel doesn't show the version, so we need to tease it out from the headers and re-construct the same string it used to give ..
    #echo '#include <Rversion.h>~#include <stdio.h>~int main() { printf("%s.%s %s (%s-%s-%s r%s)\n", R_MAJOR, R_MINOR, R_STATUS, R_YEAR, R_MONTH, R_DAY, R_SVN_REVISION); return 0; }' | tr '~' '\n' > /tmp/get_r_ver.c
    rm -f /tmp/get_r_ver
    gcc -o /tmp/get_r_ver get_r_ver.c -I "${FWPATH}/Headers"
    VERFULL=`/tmp/get_r_ver`
    rm -f /tmp/get_r_ver
    if [ -z "${VERFULL}" ]; then
	echo "*** ERROR: Cannot determine full version."
	exit 1
    fi
fi
VER=`echo "${VERFULL}"|sed -n -e 's/^\([0-9.]\{3,\}\).\{0,\}/\1/p'`
if [ -z "${SVN}" ]; then
    SVN=`echo "${VERFULL}"|sed -n -e 's/.\{0,\} r\([0-9]\{1,\}\)).\{0,\}/\1/p'`
fi
if [ -z "${SVN}" ]; then
    SVN=`echo "cat(R.version[['svn rev']])"|"${RBIN}" --slave`
fi

NPVER=`echo "${VER}"|tr -d .`
VERSH=`echo "${VER}"|sed -e 's/\.[0-9]$//'`
RLVER=`ls "${FWPATH}/Resources/lib/libreadline"*dylib|sed -n -e 's/.\{0,\}readline\.\([0-9]\.[0-9]\).\{0,\}/\1/p'`

NPVER=`echo "${VER}"|tr -d .`
VERSH=`echo "${VER}"|sed -e 's/\.[0-9]$//'`
RLVER=`ls "${FWPATH}/Resources/lib/libreadline"*dylib|sed -n -e 's/.\{0,\}readline\.\([0-9]\.[0-9]\).\{0,\}/\1/p'`
RRPATH="${BASE}/R-${NPVER}-CRAN"

if [ -z "${GUIPATH}" ]; then
        GUIPATH=/Applications
fi

if [ ! -e "${GUIPATH}/R.app/Contents/Info.plist" ]; then
        echo "** ERROR: ${GUIPATH}/R.app is not a valid GUI (set GUIPATH when necessary)"
        exit 1
fi

GUIVER=`sed -n -e 's/.*string>R.app GUI \([0-9.]\{3,\}\).*/\1/p' "${GUIPATH}/R.app/Contents/Info.plist"`
GUISVN=`sed -n -e 's/.*string>R.app GUI [^(]\{1,\}(\([0-9]\{1,\}\).*/\1/p' "${GUIPATH}/R.app/Contents/Info.plist"`

FWPVER=`echo $VERSH|sed -e 's:\.:0:' -e 's:$:'$SVN':'`

echo ""
echo " R  : VER=$VER, SVN=$SVN, VERSH=$VERSH, FWPVER=$FWPVER, VERFULL=$VERFULL"
echo "      FWPATH=$FWPATH, RBIN=$RBIN"
echo " GUI: GUIVER=$GUIVER, GUISVN=$GUISVN"
echo " rl : $RLVER"
echo " pkg: RRPATH=$RRPATH"
echo ""


chmod -R g+w "${GUIPATH}/R.app"
if [ -e "${GUIPATH}/R64.app" ]; then
    chmof -R g+w"${GUIPATH}/R64.app"
fi

if [ -z "${GUISVN}" -o -z "${GUIVER}" ]; then
    echo "** ERROR: unable to find out GUI version/revision (possibly a pre-release?)"
    exit 1;
fi

export VER
export VERFULL
export VERSH
export SVN
export GUIVER
export GUISVN
export RLVER
export BASE
export TP
export FWPATH
export FWPVER

if [ "$1" = '-u' -a -e R.pmdoc ]; then
    echo " - Updating R.pmdoc"

    do_update=1
else
    echo " - Creating R.pmdoc"

    rm -rf R.pmdoc
    mkdir R.pmdoc
fi

for i in `find $TEMPLATE -type f`; do
    j=`echo $i|sed "s:$TEMPLATE:R.pmdoc:"`
    ./envs $i $j
done

if [ -z "${do_update}" ]; then
    echo " - create contents files"
    ./mkcont /Library/Frameworks/R.framework > R.pmdoc/01r-contents.xml
    ./mkcont /Applications/R.app > R.pmdoc/02r-contents.xml
    if [ -e /Applications/R64.app ]; then
	./mkcont /Applications/R64.app > R.pmdoc/03r-contents.xml
    fi
fi

echo " - Done"