#! /bin/sh
# © M E C Swanson 2008
#
#Script to generate the mangle Makefile
#supports Linux, Darwin (MacOSX Intel or PPC), and SunOS 
#can generate Makefile for real*10 or real*8 versions of mangle
#
#USAGE: configure [<OS>] [<architecture>] [<real8 or real10>]
#EXAMPLES:
#to automatically detect OS, architecture, and mangle version: 
#configure
#to generate Makefile for Linux: 
#configure Linux
#to generate Makefile for real8 for Mac PPC: 
#configure Darwin ppc real8

#process command line arguments
if [ "$1" = "" ]; then
    OS=`uname -s`
else
    OS=$1
fi

if [ "$2" = "" ]; then
    ARCH=`arch`
else
    ARCH=$2
fi

if [ "$3" = "" ]; then
    BYTES="real10"
else
    BYTES=$3
fi

#Warnings
if [ "$ARCH" = "ppc" ]; then
    OS=$OS$ARCH
    if [ "$BYTES" = "real10" ]; then
	echo "WARNING: the real*10 version of mangle has not yet been"
	echo "successfully compiled on a PPC Mac.  Please contact the mangle"
	echo "development team (tegmark@mit.edu) if you manage to succeed!"
    fi	
fi

if [ "$OS" = "SunOS" ]; then
    echo "WARNING: mangle2.x has not yet been successfully compiled" 
    echo "on SunOS.  Please contact the mangle development team"
    echo "(tegmark@mit.edu) if you manage to succeed!"
fi

if [ "$ARCH" = "ppc64" ]; then
    echo "WARNING: mangle has not yet been tested on the ppc64 architecture."
    echo "Please contact the mangle development team (tegmark@mit.edu)" 
    echo "if you manage to compile mangle on this system!"
fi

if [ "$OS" != "Linux" ] && [ "$OS" != "Darwin" ] && [ "$OS" != "Darwinppc" ] && [ "$OS" != "SunOS" ] ; then
    echo "WARNING: this configure script does not support $OS."
    echo "Attempting to use configuration for Linux ..."
    OS="Linux"
fi

#Start of Makefile
echo "#Makefile for $OS $ARCH $BYTES generated with configure." > Makefile

cat <<EOF >> Makefile

###############################################################################
# LOCAL CONFIGURATION: EDIT AS DESIRED
###############################################################################

EOF

#For real*10 version of Mangle, add compiler flags to the Makefile for
#Linux, Darwin, Darwinppc, or SunOS
if [ "$BYTES" = "real10" ]; then
    case $OS in
#Linux compiler flags:
	Linux)
cat <<EOF >> Makefile
#------
# Gnu

CC = gcc
CFLAGS = -g -O3 \$(CPPFLAGS) -Wall -DLINUX -DGCC -D_FILE_OFFSET_BITS=64

F77 = gfortran
FFLAGS:= -Wall \$(CPPFLAGS) -g -O3 -DGFORTRAN -ff2c -D_FILE_OFFSET_BITS=64
STATICFLAGS:= -static

#MAKE=gmake

all: mangle

EOF
	    ;;
#Intel Mac compiler flags:
	Darwin)
cat <<EOF >> Makefile	    
#------
# Gnu

CC = gcc
CFLAGS = -g -O3 -Wall -DMACOSX -DGCC -m32 -D_FILE_OFFSET_BITS=64

F77 = gfortran
FFLAGS:= -Wall -g -O3 -DGFORTRAN -ff2c -m32 -D_FILE_OFFSET_BITS=64
#STATICFLAGS:= -static-libgfortran
STATICFLAGS:= -nodefaultlibs -lSystem -lgcc -lm -lgfortran_static
# static linking of gfortran library to compile for distribution.
# hack to work around the fact that the -static-libgfortran flag does 
# not currently work on Darwin - requires running the following 
# command to make a symbolic link to the static gfortran library 
# in /usr/local/gfortran/lib or wherever gfortran is installed:
# sudo ln -s libgfortran.a libgfortran_static.a

#MAKE=gmake

all: mangle

EOF
echo "WARNING: To compile statically with gfortran (i.e., using 'make static'),"
echo "you need to make a symbolic link to the static gfortran library as follows:"
echo ""
echo "cd /usr/local/gfortran/lib (or wherever gfortran is installed)"
echo "sudo ln -s libgfortran.a libgfortran_static.a"
echo ""
	    ;;
#PPC Mac compiler flags:
	Darwinppc)
cat <<EOF >> Makefile	    
#------
# Gnu

CC = gcc
CFLAGS = -g -O3 -Wall -DMACOSX -DGCC -D_FILE_OFFSET_BITS=64

F77 = gfortran
FFLAGS:= -Wall -g -O3 -DGFORTRAN -ff2c -D_FILE_OFFSET_BITS=64
#STATICFLAGS:= -static-libgfortran
STATICFLAGS:= -nodefaultlibs -lSystem -lgcc -lm -lgfortran_static
# static linking of gfortran library to compile for distribution.
# hack to work around the fact that the -static-libgfortran flag does 
# not currently work on Darwin - requires running the following 
# command to make a symbolic link to the static gfortran library 
# in /usr/local/gfortran/lib or wherever gfortran is installed:
# sudo ln -s libgfortran.a libgfortran_static.a

#MAKE=gmake

all: mangle

EOF
	    ;;
#SunOS compiler flags:
	SunOS)
cat <<EOF >> Makefile	    
#----
# Sun

#CC = cc
#CFLAGS = -O -DSUN

CC = gcc
CFLAGS = -g -O3 -DGCC -D_FILE_OFFSET_BITS=64

#F77 = f90
##FFLAGS = -u -O -pg -DSUN
#FFLAGS = -u -O -DSUN

# gnu gfortran
F77 = gfortran
FFLAGS = -W -g -O3 -DGFORTRAN -ff2c -D_FILE_OFFSET_BITS=64

#MAKE=make

all: mangler

EOF
	    ;;
    esac

#If real*10 is not specified, default to real*8 version and add compiler 
#flags to the Makefile for Linux, Darwin, Darwinppc, or SunOS
else
    case $OS in
#Linux compiler flags:
	Linux)
cat <<EOF >> Makefile	    
#------
# Gnu

CC = gcc
CFLAGS = -g -O3 -Wall -DLINUX -DGCC -m32 -D_FILE_OFFSET_BITS=64

## f2c
#F77 = fort77
#FFLAGS = -u -a -C -g -DF2C
##-O3
##-pg

# gnu g77 (which assumes no SAVE)
F77 = g77
FFLAGS = -Wimplicit -fbounds-check -g -O3 -DG77 -m32 -D_FILE_OFFSET_BITS=64

#MAKE=gmake

all: mangle

EOF
	    ;;
#Intel Mac compiler flags:
	Darwin)
cat <<EOF >> Makefile	    
#------
# Gnu

CC = gcc
CFLAGS = -g -O3 -Wall -DMACOSX -DGCC -m32 -D_FILE_OFFSET_BITS=64

# gnu g77 (which assumes no SAVE)
F77 = g77
FFLAGS = -Wimplicit -fbounds-check -g -O3 -DG77 -m32 -D_FILE_OFFSET_BITS=64

#MAKE=gmake

all: mangle

EOF
	    ;;
#PPC Mac compiler flags:
	Darwinppc)

cat <<EOF >> Makefile	    
#------
# Gnu

CC = gcc
CFLAGS = -g -O3 -Wall -DMACOSX -DGCC -D_FILE_OFFSET_BITS=64

# gnu g77 (which assumes no SAVE)
F77 = g77
FFLAGS = -Wimplicit -fbounds-check -g -O3 -DG77 -D_FILE_OFFSET_BITS=64

#MAKE=gmake

all: mangle

EOF
	    ;;
#SunOS compiler flags:
	SunOS)
cat <<EOF >> Makefile	    
#----
# Sun

CC = cc
CFLAGS = -O -DSUN -m32 -D_FILE_OFFSET_BITS=64

#CC = gcc
#CFLAGS = -g -O3 -DGCC

F77 = f77
#FFLAGS = -u -O -pg -DSUN
FFLAGS = -u -O -DSUN -m32 -D_FILE_OFFSET_BITS=64

## gnu g77 (which assumes no SAVE)
#F77 = g77
#FFLAGS = -Wimplicit -g -O3 -DG77

#MAKE=make

all: mangler

EOF
	    ;;
    esac
fi

#add this text to Makefile for all systems:
cat <<EOF >> Makefile
#-----------------
# starlink library

# path to search for the starlink library
ISLIB = -L./lib/starlink -L\$(HOME)/lib/starlink

# starlink library should be called libstar.a
# Edit as desired,
# or make a soft link from the actual starlink library to libstar.a:
#    ln -s <name_of_starlink_library> libstar.a
SLIB = -lstar

###############################################################################
# END OF LOCAL CONFIGURATION
###############################################################################

BIN=../bin
ILIB = -L.
LLIB = \$(LDFLAGS) -lmangle

PROGS = balkanize drangle harmonize map pixelize pixelmap polyid poly2hpx weight_dumb poly2poly ransack rasterize snap unify weight test
#ddcount rotate rrcoeffs

COBJ = progress.o rwhealpix.o rwbinpoly.o advise_fmt.o braktop_.o cmminf.o convert.o copy_format.o copy_poly.o drandom.o drangle_polys.o dranglepolys_.o dump_poly.o findtop_.o get_pixel.o garea.o gcmlim.o gphbv.o gphi.o gptin.o gspher.o gsphr.o gvert.o gvlim.o gvphi.o harmonize_polys.o harmonizepolys_.o healpix_ang2pix_nest.o healpixpolys.o ikrand.o msg.o new_poly.o new_vert.o partition_poly.o places.o poly_id.o poly_sort.o prune_poly.o rasterize.o rdangle.o rdline.o rdmask.o rdmask_.o rdspher.o rrcoeffs.o scale.o sdsspix.o search.o snap_poly.o split_poly.o strcmpl.o strdict.o vmid.o weight_fn.o which_pixel.o wrangle.o wrho.o wrmask.o wrrrcoeffs.o wrspher.o

FOBJ = azel.s.o azell.s.o braktop.s.o felp.s.o fframe.s.o findtop.s.o garea.s.o gaream.s.o gcmlim.s.o gphi.s.o gphim.s.o gphbv.s.o gptin.s.o gsphera.s.o gspher.s.o gsubs.s.o gvert.s.o gvlim.s.o gvphi.s.o iylm.s.o pix2vec_nest.s.o twodf100k.o twodf230k.o twoqz.o wlm.s.o wrho.s.o

EOF

#add this text to Makefile for all systems except Sun:
if [ ! "$OS" = "SunOS" ]; then
cat <<EOF >> Makefile
# objects not already present (will try to extract them from libmangle.a)
OBJ := \$(strip \$(foreach objfile, \$(COBJ) \$(FOBJ), \$(shell if [ ! -r \$(objfile) ]; then echo \$(objfile); fi)))

mangle:
	@ if [ -r libmangle.a -a "\$(OBJ)" ] ; then \\
		echo "extracting files from libmangle.a" ; \\
		ar xo libmangle.a \$(OBJ) >/dev/null 2>&1 ; \\
		rm -f __.SYMDEF ; \\
	fi
	@ \$(MAKE) mangler
	@ rm -f \$(COBJ) \$(FOBJ)

EOF

fi

#append non-system dependent part of (in Makefile.in) to Makefile
cat Makefile.in >> Makefile

echo "Makefile generated for $OS $ARCH $BYTES."
