#!/bin/sh


if test -z "$PCx_ARCH" ; then
  PCx_ARCH=`./pcxarch`; export PCx_ARCH
fi

if test "$1" = "PCx_wssmp" ; then
  echo ;
  echo "***************************************************";
  echo Building PCx with the IBM WSSMP Cholesky solver....;
  echo "***************************************************";
  echo ;
  if test -z "$WSSMP_LIB" ; then
    if (test -f wssmp/libwssmpp2.a) then
      WSSMP_LIB=../wssmp/libwssmpp2.a
    else 
      if test -f wssmp/libwssmp.a ; then
        WSSMP_LIB=../wssmp/libwssmp.a;
      else
        echo WSSMP library not found in ./wssmp/ - aborting ; 
        exit 1 ;
      fi
    fi
  fi
  export WSSMP_LIB ;
# go ahead and make it
  TARGETDIR=.; export TARGETDIR;
  make $1 ;
else 
# here if user typed "build PCx_NgPeyton" or "build PCx" or "build"
  if (test "$1" = "PCx_NgPeyton" -o "$1" = "PCx" -o -z "$1") then
    echo ;
    echo "***************************************************";
    echo Building PCx with the Ng Peyton Cholesky solver....
    echo "***************************************************";
    echo ;
    if test -z "$NG_LIB" ; then
      NG_LIB=../Ng-Peyton/cholesky.a
      if (test ! -f Ng-Peyton/cholesky.a) then
        if (test -d Ng-Peyton) then
          cd Ng-Peyton;
          make;
          cd ../ ;
        else
          echo No directory ./Ng-Peyton, aborting.... ; 
          exit 1 ;
        fi
      fi
      if (test ! -f Ng-Peyton/cholesky.a) then
        echo Tried to build cholesky.a in Ng-Peyton, but ;
        echo this file was apparently not created. ;
      fi
      export NG_LIB ;
    fi
# go ahead and make it
    TARGETDIR=.; export TARGETDIR;
    make $1 ;
  else
    if test "$1" = "PCx_mysolver" ; then
      echo ;
      echo "***********************************************";
      echo Building PCx with the user-supplied solver ;
      echo Requires file ./SRC/mysolver.c and ;
      echo      library ./mysolver/libmysolver.a to be present ;
      echo "***********************************************";
      echo ;
      if (test ! -f ./SRC/mysolver.c) then 
        echo File ./SRC/mysolver.c not present - aborting ;
        exit 1 ;
      fi
      if (test ! -f ./mysolver/libmysolver.a) then
        echo Library ./mysolver/libmysolver.a not present - aborting ;
        exit 1 ;
      fi
# go ahead and make PCx_mysolver
      TARGETDIR=.; export TARGETDIR;
      make $1 ;
    else
      if test "$1" = "mex" ; then
        echo ;
        echo "***********************************************";
        echo   The Matlab Interface is only available with ; 
        echo   the Ng-Peyton solver, checking its availability.... ;
        echo ;
	if test -z "$NG_LIB" ; then
	  NG_LIB=../Ng-Peyton/cholesky.a ;
 	  if (test ! -f Ng-Peyton/cholesky.a) then
	    if (test -d Ng-Peyton) then
	      cd Ng-Peyton ;
	      echo ;
	      echo Building Ng-Peyton library..... ;
              echo "***********************************************";
	      make;
	      cd ../ ;
	    else
	      echo No directory ./Ng-Peyton, aborting....  ;
              echo ;
              echo "***********************************************";
	      exit 1 ; 
	    fi
          else
	    echo ;
	    echo Found! ;
	    echo 
	  fi
	  if (test !  -f Ng-Peyton/cholesky.a) then
	    echo Tried to build cholesky.a in Ng-Peyton, but ; 
	    echo this file was apparently not created.  ; 
	    echo Aborting.....
	    exit ;
	  fi
      export NG_LIB ;
	  export MEX ;
	  make mex ;
	  echo ;
	  echo Now set your MATLABPATH to include PCx/mex, and you ;
	  echo should be able to run PCx.m from the MATLAB prompt. ;
	  echo Type 'help PCx' on the prompt for syntax information. ;
          echo "***********************************************" ;
        else
          echo Argument "$1" not recognized - aborting.... ;
          echo ;
	  exit 1 ;
	fi
      fi
    fi
  fi
fi

