#!/bin/sh
# set -x
# Create, if they don't exist, the .Data directory & subdirectories; the makefile
# Always initialize makefile, unless the -m flag is specified.
# Makefile is not touched if there is currently an S_MAKE_FLAGS and makefile
# If only a makefile exists, the standard S make rules are appended to it.
# Flag -d directory means to make chapter in that directory (make directory if needed).
# Flag -type means to create ___nonfi (Windows or DOS) or ___nonfiles (Unix) to
# Flag -u means to create __init/__prev subdirectories used by undo().
# tell Splus what sort of object->file name mapping is desired in this database.
# All flag arguments must come before any files arguments.
# S CHAPTER [-m] [-d directory] [-type Windows|DOS|Unix] [-u] [files ...]

PROG=`basename $0`
SRC=
OBJ=
FUN=
HELPSGML=
HELPRD=

## now create a makefile if there are object files
## to be loaded.

DOMAKE="yes"
DOUNDO="no"
DIR="."

SCONNECT_TYPE="none"

OPSYS=${OPSYS-`uname -s || echo "UNKNOWN"`}

if [ `PLATFORM` = "WIN386" ]
then
        FS_TYPE="Windows"
else
        FS_TYPE="Unix"
fi

while ( test $# -gt 0 ) do
	case $1 in
	-m) DOMAKE="no"
	    shift
	    ;;
	-d) shift
	    DIR=$1
	    shift
            if test ! -d $DIR
	    then
                mkdir $DIR || exit 1
	    fi
	    cd $DIR || exit 1
	    ;;
       -type) shift
            FS_TYPE=$1
            shift
            if [ $FS_TYPE = "DOS" ]
            then
               FS_TYPE="Windows"
            fi
            if [ $FS_TYPE != "Windows" -a $FS_TYPE != "Unix" ]
            then
               echo "-type must be followed by Windows or Unix" >&2
               exit 3
            fi
            ;;
	-u) DOUNDO="yes"
	    shift
	    ;;
	-sconnectlib)
		shift
		if [ $SCONNECT_TYPE != "none" ]
		then
		    echo "-sconnectlib and -sconnectapp cannot both be specified."
		    exit 4
		fi
		case $OPSYS in
		    Linux | SunOS | HP-UX | OSF1 | AIX) 
			SCONNECT_TYPE="library"
			;;
		    *)	echo "Connect/C++ is not currently supported on this platform."
			exit 5
			;;
		esac
		;;
	-sconnectapp)
		shift
		if [ $SCONNECT_TYPE != "none" ]
		then
		    echo "-sconnectlib and -sconnectapp cannot both be specified."
		    exit 4
		fi
		case $OPSYS in
		    Linux | SunOS | HP-UX | OSF1 | AIX) 
			SCONNECT_TYPE="application"
			;;
		    *)	echo "Connect/C++ is not currently supported on this platform."
			exit 5
			;;
		esac
		;;	    
	-*) echo "Error in $PROG: unknown argument $1"
	    exit 2
	    ;;
	*) files="$files $1"
	   shift
	   ;;
	esac
done

if [ "$DOMAKE" = "no" -a "x$files" != "x" ]
then
	echo "Error in $PROG: the \"-m\" option means no makefile; file arguments then meaningless"; 
	exit 1
fi

chapter=`basename \`pwd\``

if test  -d .Data
then
	if test -d .Data/__Meta
	then
		[ "$S_SILENT_STARTUP" = "" ] && echo "Splus data directory exists in $DIR: leaving it alone"
	else
		echo "Error in $PROG: $DIR/.Data does not look like an SV4-based data directory (no .Data/__Meta)"
		exit 1
	fi
else
	[ "$S_SILENT_STARTUP" = "" ] && echo "Creating data directory for chapter $DIR"
	mkdir .Data || exit 1
fi
if test !  -d .Data/__Meta
then
	mkdir .Data/__Meta || exit 1
fi
if test !  -d .Data/__Hhelp
then
	mkdir .Data/__Hhelp || exit 1
fi
if test !  -d .Data/__Shelp
then
	mkdir .Data/__Shelp || exit 1
fi

if test "$DOUNDO" = "yes"
then
	if test ! -d .Data/__init
	then
		mkdir .Data/__init || exit 1
	fi
	if test ! -d .Data/__prev
	then
		mkdir .Data/__prev || exit 1
	fi
fi

if test $FS_TYPE = "Windows"
then
        if [ -f .Data/___nonfiles -o -f .Data/__Meta/___nonfiles ] ; then
                echo "This looks like an existing Unix .Data directory, cannot convert it to Windows data directory" >& 2
                echo "Please make a new Windows chapter and copy this one to it using Splus to do the copying" >& 2
                exit 4
        fi
        if [ ! -f .Data/___nonfi ] ; then
                touch .Data/___nonfi || exit 1
        fi
        if [ ! -f .Data/__Meta/___nonfi ] ; then
                touch .Data/__Meta/___nonfi || exit 1
        fi
		if test "$DOUNDO" = "yes"
		then
			if [ ! -f .Data/__init/___nonfi ]; then
				touch .Data/__init/___nonfi || exit 1
			fi
			if [ ! -f .Data/__prev/___nonfi ]; then
				touch .Data/__prev/___nonfi || exit 1
			fi
		fi
fi
if test $FS_TYPE = "Unix"
then
        if [ -f .Data/___nonfi -o -f .Data/__Meta/___nonfi ] ; then
                echo "This looks like an existing Windows data directory, cannot convert it to a Unix one" >& 2
                echo "Please make a new Unix chapter and copy this one to it using Splus to do the copying" >& 2
                exit 4
        fi
        if [ ! -f .Data/___nonfile ] ; then
                touch .Data/___nonfile || exit 1
        fi
        if [ ! -f .Data/__Meta/___nonfile ] ; then
                touch .Data/__Meta/___nonfile || exit 1
        fi
		if test "$DOUNDO" = "yes"
		then
			if [ ! -f .Data/__init/___nonfile ]; then
				touch .Data/__init/___nonfile || exit 1
			fi
			if [ ! -f .Data/__prev/___nonfile ]; then
				touch .Data/__prev/___nonfile || exit 1
			fi
		fi
fi

if [ "$DOMAKE" = "no" ]
then
	exit 0
fi

if [ x"$files" = x ]; then
    files=`ls *.[CcfrqRSs] *.ssc *.Rd *.cc *.sgml *.cpp *.cxx 2>/dev/null | grep -v ${chapter}_i.c`
fi

# test for non-empty $files:
if test "x$files" != "x"
then
  set $files
  load="S LIBRARY S.so"
  for i
  do
	case $i in
	*.c)
		SRC="$SRC $i"
		OBJ="$OBJ `basename $i .c`.o"
		;;
	*.cc)
		SRC="$SRC $i"
		OBJ="$OBJ `basename $i .cc`.o"
		;;
	*.C)
		SRC="$SRC $i"
		OBJ="$OBJ `basename $i .C`.o"
		;;
	*.cpp)
		SRC="$SRC $i"
		OBJ="$OBJ `basename $i .cpp`.o"
		;;
	*.cxx)
		SRC="$SRC $i"
		OBJ="$OBJ `basename $i .cxx`.o"
		;;
	*.f)
		SRC="$SRC $i"
		OBJ="$OBJ `basename $i .f`.o"
		;;
	*.r)
		SRC="$SRC $i"
		OBJ="$OBJ `basename $i .r`.o"
		;;
	*.o)
		OBJ="$OBJ $i"
		;;
	*.q | *.s | *.R | *.S | *.ssc)
		FUN="$FUN $i"
		;;
	*.sgml)
		HELPSGML="$HELPSGML $i"
		;;
	*.Rd)
		HELPRD="$HELPRD $i"
		;;
	*)
		echo "Error in $PROG: unsupported file type $i"
		exit 3
		;;
	esac
  done
fi
## end of processing arguments

if test "x$files" != "x" 
then
  
    if test -r [mM]akefile
    then
	MAKEFILE=`ls [mM]akefile`
	if test `echo $MAKEFILE | wc -w` -gt 1 
	then
	    echo "Both makefile and  Makefile exist. Fatal error";
	    exit 1;
	fi
	echo "A copy of the current $MAKEFILE is made as $MAKEFILE.before."
	cp -f $MAKEFILE $MAKEFILE.before
    else
	MAKEFILE=makefile
	[ "$S_SILENT_STARTUP" = "" ] && echo "Creating \"$MAKEFILE\""
    fi

	# Append full new makefile rules
	[ "$S_SILENT_STARTUP" = "" ] && echo "$MAKEFILE will build with:" "`echo $files|tr '\012' ' '`"
	echo > $MAKEFILE "# $MAKEFILE for local CHAPTER"
	echo >> $MAKEFILE "SHELL=/bin/sh

SRC=$SRC
OBJ=$OBJ
FUN=$FUN
HELPSGML=$HELPSGML
HELPRD=$HELPRD

# Use LOCAL_CFLAGS to add arguments for the C compiler
LOCAL_CFLAGS=
# Use LOCAL_CXXFLAGS to add arguments for the C++ compiler
LOCAL_CXXFLAGS=
# Use LOCAL_FFLAGS to add arguments for the FORTRAN compiler
LOCAL_FFLAGS=
"

case `uname -s` in
    Linux)
	echo >> $MAKEFILE '# Use LOCAL_LIBS to add arguments or additional libraries to the linker
# LOCAL_LIBS="-lf2c"
LOCAL_LIBS='
	;;

    SunOS)
	echo >> $MAKEFILE '# Use LOCAL_LIBS to add arguments or additional libraries to the linker
# LOCAL_LIBS="-lM77 -lF77 -lsunmath_mt"
LOCAL_LIBS='
	;;

    *)  echo >> $MAKEFILE '# Use LOCAL_LIBS to add arguments or additional libraries to the linker
LOCAL_LIBS='
	;;
esac

echo >> $MAKEFILE '
include $(SHOME)/library/S_FLAGS
# Uncomment following line to make debuggable, not optimized, compiled code.
# OPTIMIZE_FLAG=-g
'

case $SCONNECT_TYPE in
    none | library )
	echo >> $MAKEFILE 'all: install.funs S.so install.help'
	;;

    application)
	echo >> $MAKEFILE 'all: install.funs S.app install.help'
	;;
esac

echo >> $MAKEFILE '
install.funs: $(FUN)
	@if [ "X$(FUN)" != "X" ] ; then \
	( echo "if(file.exists(\"../DESCRIPTION\")) attachPackageDependencies(descriptionFile = \"../DESCRIPTION\")" ; cat $(FUN)) | $(SHOME)/cmd/Splus ; \
	fi

install.help: $(HELPSGML)
	@if [ "X$(HELPSGML)" != "X" ] ; then \
	$(SHOME)/cmd/Splus HINSTALL ./.Data $(HELPSGML) ; \
	$(SHOME)/cmd/Splus BUILD_JHELP ; \
	fi

install.Rdhelp: $(HELPRD)
	@if [ "X$(HELPRD)" != "X" ] ; then \
	$(SHOME)/cmd/Splus RDINSTALL ./.Data $(HELPRD) ; \
	$(SHOME)/cmd/Splus BUILD_JHELP ; \
	fi'


case $SCONNECT_TYPE in

none)

echo >> $MAKEFILE '
S.so: $(OBJ) 
	@if [ "X$(OBJ)" != "X" ]; then \
	$(SHOME)/cmd/Splus LIBRARY S.so $(OBJ) $(LOCAL_LIBS) $(SQPE_SHARED_LIB) ; \
	fi'
;;

library)
	
echo >> $MAKEFILE '
S.so: $(OBJ) 
	@if [ "X$(OBJ)" != "X" ]; then \
	$(SHOME)/cmd/Splus LIBRARY S.so $(OBJ) $(SCONNECT_SHARED_LIB) $(LOCAL_LIBS) $(SQPE_SHARED_LIB) ; \
	fi
'
;;

application)
	
echo >> $MAKEFILE '
S.app: $(OBJ)
	@if [ "X$(OBJ)" != "X" ]; then \
	DLFLAGS="$(SCONNECT_APP_FLAGS)"; \
	$(SHOME)/cmd/Splus LIBRARY -sconnectapp S.app $(OBJ) $(SQPE_SHARED_LIB) $(SCONNECT_SHARED_LIB) $(LOCAL_LIBS) ; \
	fi
'
;;
esac

echo >> $MAKEFILE '
dump:
	@if test -d ./.Data; then Splus dumpChapter $(SRC); fi

boot:
	@if test -s all.Sdata; \
	  then (BOOTING_S="TRUE" export BOOTING_S; echo "terminate('should have been booting S')"| $(SHOME)/cmd/Splus); \
	fi

clean:
	-rm $(OBJ)
'
fi

echo "S-PLUS chapter $chapter initialized."
exit 0
