#!/bin/csh -f
#
# first test to see if MOSROOT has been set up
#
if ( $?MOSROOT == 0 ) then
    echo MOSROOT should really be set up to a sensible value, but it is usually
    echo one level above this directory!
    set mosroot = ${cwd:h}
    setenv MOSROOT $mosroot
    echo MOSROOT has been set to $MOSROOT
endif
#
# test to see if CCP4 environment variables have been set and library files
# exist.
#
if ( $?CCP4  == 0 ||$?CLIB  == 0 ) then
    echo You must set up CCP4 properly first! current values of 
    echo environment variables are:
    if ( $?CCP4 == 0 ) then
    echo \* CCP4 has not been defined - check your installation
    exit
    else
    echo \* \$CCP4 is $CCP4
    endif
    if ( $?CLIB == 0 ) then
    echo \* CLIB has not been defined - check your installation
    exit
    else
    echo \* \$CLIB is $CLIB
    endif    
    else
    echo \* CCP4 appears to have been set up properly - checking libraries
endif
#
# CCP4 libraries - pre-version 5 or post? - removed, support only v5 et seq.
#
if (( -e $CLIB/libccp4f.a && -e $CLIB/libccp4c.a ) || ( -e $CLIB/libccp4f.so && -e $CLIB/libccp4c.o )) then
    setenv CCP4_VERSION_5_EXISTS TRUE
#    echo checking source files for CCP4 version 5 incompatibility - this will take a few seconds
#    if ( -d mosflm ) then
#	./toversion5.csh
#    else
#	./toCCP4.5.csh
#    endif
    else
echo You must install CCP4 version 5 \(or later\) - Mosflm no longer builds with 
echo CCP4 version 4 or earlier.
endif
#
# new-style libraries do not exist, check for old-style
#
#if ( $?CCP4_VERSION_5_EXISTS == 0 ) then
#if ( ! -e $CLIB/libccp4.a && ! -e $CLIB/libccp4.so ) then
#    echo \-\-\* CCP4 libraries do not exist in $CLIB - exiting
#    exit
#    else
#    echo \-\-\* OLD-STYLE CCP4 libraries exist - changing Mosflm to reflect this
#    if ( -d mosflm ) then
#	./toversion4.csh
#    else
#	./toCCP4.4.csh
#    endif
#endif
#endif
#
# XDL view libraries
#
if ( ! -e $CLIB/libxdl_view.a && ! -e $CLIB/libxdl_view.so ) then
    echo \-\-\* XDL_VIEW libraries do not exist in $CLIB - exiting
    exit
    else
    echo \-\-\* XDL_VIEW libraries exist - checking host type
endif
#
# test for the host type - currently iris4d (both 64 and new-style 32 
# bit), alpha (Tru64 UNIX), linux (i386, i486, i586, PPC) and set up for
# the make which follows.
#
if ( $HOSTTYPE == iris4d )  then
#    @ x = `file $CLIB/libccp4.a | grep -c 64`
#	echo x = $x
    if ( `file $CLIB/libccp4f.a | grep -c 64` != 0 ) then
    echo \-\-\-\* CCP4 library has 64 bit objects
    setenv HOSTTYPE $HOSTTYPE'_64bit'
    else
    echo \-\-\-\* CCP4 library has 32 bit objects - checking whether new-style \(default CCP4 build since version 4-4.2\!\!\)
	f77 -n32 -c test.f ; f77 -o test test.o $CLIB/libccp4f.a $CLIB/libccp4c.a
      if ( $status != 0 ) then
	echo \-\-\-\* Not new-style - checking for old-style 
	f77 -o32 -c test.f ; f77 -o32 -o test test.o $CLIB/libccp4f.a $CLIB/libccp4c.a
	if ( $status != 0 ) then
          echo \-\-\-\* Not old-style either\; are you sure the CCP4 libraries were built on an SGI?
	  exit
	else
 	  echo \-\-\-\* Old-style 32-bit objects in \$CLIB/libccp4f.a and \$CLIB/libccp4c.a
	  setenv SGI_32 -o32
	endif
	else
 	echo \-\-\-\* New-style 32-bit objects in \$CLIB/libccp4f.a and \$CLIB/libccp4c.a
	setenv SGI_32 -n32
	endif
	/bin/rm -f test.o
    endif
#
# LINUX only, for glibc, newer versions use /usr/include/ncurses.h, older ones 
# /usr/include/curses.h. We must check for the presence of the newer one
# at least and if necessary, make the change in chelp.h
#
    else if (( $HOSTTYPE =~ i*86-linux )||( $HOSTTYPE =~ i*86 )) then
    setenv HOSTTYPE i386-linux
    if ( ! -e /usr/include/ncurses.h ) then
    echo WARNING\! changing \"\#include \<ncurses.h\> to \#include \<curses.h\> in chelp.c
    sleep 2
      set chelpinclude = `find . -name "chelp.h"`
      cat $chelpinclude |sed "s/ncurses\.h/curses\.h/" > tempfile;mv tempfile $chelpinclude
    endif
#
# check for unsupported platforms
#
    else if ( $HOSTTYPE != 'alpha' && $HOSTTYPE != 'i386-linux' && $HOSTTYPE != 'powerpc' && $HOSTTYPE != 'macintosh' ) then
    echo \-\-\-\* Unsupported host type $HOSTTYPE - this build only works on 
    echo \-\-\-\* SGI, Compaq Alpha, Intel Linux, PowerPC Linux and MacOSX
    echo \-\-\-\* consult the README file!
    exit
else if ( $HOSTTYPE == 'alpha' && `uname` == 'Linux' ) then
    set HOSTTYPE=alpha-linux
endif
echo \-\-\-\* Build platform is $HOSTTYPE - setting up compilation flags etc.
#
# now source "setup" - different version for release than for development
#
source ./setup
#
# try to add refix if this is a distribution build
#
if ( -d mosflm ) then
    cd mosflm ; ./use_refix.com ; cd ..
    else
    echo this is not a distribution build\; directory \"mosflm\" doesn\'t exist
    endif
#
# do the build now
#
#make distclean
make
#
#
if ( -e bin/ipmosflm ) then 
    echo ' '
    echo Finished! type \'make clean\' to remove object files and library archives
    echo or \'make distclean\' to remove objects, archives and the executable.
else
    echo ' '
    echo That doesn\'t seem to have worked. Please e-mail the messages from the 
    echo build to Harry Powell \(harry@mrc-lmb.cam.ac.uk\) for further advice.
endif
