#!/usr/bin/env sh
version=`more ../../version`

SCIPY_MAJOR=0
SCIPY_MINOR=10
SCIPY_PATCH=0
SCIPY_MAJOR_SRC=0
SCIPY_MINOR_SRC=10
SCIPY_PATCH_SRC=0
SCIPY_URL=http://downloads.sourceforge.net/project/scipy/scipy/${SCIPY_MAJOR_SRC}.${SCIPY_MINOR_SRC}.${SCIPY_PATCH_SRC}

SCIPY_NAME=scipy-${SCIPY_MAJOR_SRC}.${SCIPY_MINOR_SRC}.${SCIPY_PATCH_SRC}.tar.gz

if (test "$1" = "--debug") then
    D="--debug";
    OPT=-g; 
    shift
else
    D="";
    OPT=${OPT:=-O}
fi
export OPT

if (test ! -d $1) then
    echo -n "$1 is not a directory; create it? (y/[n])";
    y='n'
    read y;
    if (test ${y} = 'y') then
        mkdir $1; mkdir $1/bin; mkdir $1/lib; mkdir $1/include
        if (test ! -d $1) then
            echo "Could not create $1, installation aborted.";
            exit 1
        fi
    else
        echo 'Installation aborted.';
        exit 1
    fi
fi
prefix=`(cd $1;pwd)`
if (test "$2" = "" ) then
  pyprefix=${prefix}
else
  pyprefix=`(cd $2;pwd)`
fi

if (test ! -d build) then
    # Unpack everything into build
    mkdir build
fi
if (test ! -d sources) then
    mkdir sources
fi

if (test ! -e sources/${SCIPY_NAME}) then
    cd build
    ${BUILD_DIR}/checked_get.sh ${SCIPY_NAME} ${SCIPY_URL}/${SCIPY_NAME}
    cd ..
fi
cd build
tar xzf ../sources/${SCIPY_NAME}
cd ..

cd build
echo "Installation to ${prefix}"
# Make sure /usr/bin/env etc. finds 'ourselves'
PATH="${prefix}/bin:${PATH}"; export PATH

if test `${pyprefix}/bin/python -c "cmd = 'try:\n  import scipy\n  print 0\nexcept:\n  print 1'; exec(cmd)"` = "1" ; then
  echo "could not import module we need to build"
else
  UMAJOR=`${pyprefix}/bin/python -c "import scipy ; print scipy.version.version.split('.')[0]"`
  UMINOR=`${pyprefix}/bin/python -c "import scipy ; print scipy.version.version.split('.')[1]"`
  UPATCH=`${pyprefix}/bin/python -c "import scipy ; print scipy.version.version.split('.')[2]"`
  echo "You have scipy:"${UMAJOR}.${UMINOR}.${UPATCH}
  if test ${UMAJOR} -gt ${SCIPY_MAJOR}  ; then
    echo "no need to build your major is greater than "${SCIPY_MAJOR}
    exit;
  else 
    if test ${UMAJOR} -eq ${SCIPY_MAJOR} ; then
      if test ${UMINOR} -gt ${SCIPY_MINOR}  ; then
      echo "no need to build, your major and minor are greater than "${SCIPY_MAJOR}"."${SCIPY_MINOR}
       exit;
      else
       if test ${UMINOR} -eq ${SCIPY_MINOR} ; then
        if test ${UPATCH} -ge ${SCIPY_PATCH}  ; then
	echo "no need to build, your major, minor and patch are greater than "${SCIPY_MAJOR}"."${SCIPY_MINOR}"."${SCIPY_PATCH}
         exit;
        fi
       fi
      fi
    fi
  fi
echo "building"
fi
BLAS_URL=http://www.netlib.org/blas
BLAS_GZ=blas.tgz
LAPACK_URL=http://www.netlib.org/lapack
LAPACK_GZ=lapack.tgz

(${BUILD_DIR}/checked_get.sh ${LAPACK_GZ} ${LAPACK_URL}/${LAPACK_GZ} ; \
tar xzf ../sources/${LAPACK_GZ};\
export LAPACK_SRC='../lapack-3.3.1/SRC' ; \
export BLAS_SRC='../lapack-3.3.1/BLAS/SRC' ; \
cd scipy-*; \
#cp ../../../../site.cfg . ; \
${pyprefix}/bin/python setup.py build ${D} install --prefix=${prefix} ;\
#rm -rf ../../lapack-lite-3.1.1 ;\
)
