#!/bin/bash
###############################################################################
#
# Copyright 2008, 2015, Mississippi State University
#
# This file is part of the Loci Framework.
#
# The Loci Framework is free software: you can redistribute it and/or modify
# it under the terms of the Lesser GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The Loci Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# Lesser GNU General Public License for more details.
#
# You should have received a copy of the Lesser GNU General Public License
# along with the Loci Framework.  If not, see <http://www.gnu.org/licenses>
#
###############################################################################

set -e
set -u

source=`pwd`
logname=${LOGNAME-user}
target=${LOCI_TARGET-/usr/tmp/$logname/}
subdir=Loci
LIB_POSTFIX="so"

ARCH=${LOCI_ARCH-`uname -s`}
if [ $ARCH == "Darwin" ]; then
    LIB_POSTFIX="dylib"
fi
if [ $ARCH == "IRIX64" ]; then
# If IRIX OS, default to sgi compiler, otherwise default to gcc
  COMP=${LOCI_COMP-CC_sgi}
else
  COMP=${LOCI_COMP-gcc}
fi

while [ $# -ne 0 ]; do 
    case "$1" in
	--subdir)
	    shift
	    subdir=$1
	    ;;
	--target)
	    shift
	    subdir=""
	    target=$1
	    ;;
	--source)
	    shift
	    source=$1
	    ;;
	--compiler)
	    shift
	    COMP=$1
	    ;;
	*)
            break
            ;;
    esac
    shift
done

if [ $# -eq 1 ]; then
  target=$1
fi
if [ $# -eq 2 ]; then
  target=$1
  COMP=$2
fi

if [ ${target:0:1} != "/" ]; then
   target=`pwd`/${target}
   echo setting target to \'$target\'
fi

TARGET_DIRS="System Tools FVMAdapt include include/Tools include/Config include/MPI_stubb include/FVMAdapt"
TOOLS_DIRS="lpp FVMtools FVMtools/libadf FVMMod sprng sprng/include sprng/include/sprng"

DO_METIS_INSTALL=${INSTALL_METIS:-1}
if [ ! ${DO_METIS_INSTALL} == 0 ]; then
    TOOLS_DIRS=$TOOLS_DIRS" ParMetis-4.0 ParMetis-4.0/GKLib ParMetis-4.0/METISLib ParMetis-4.0/ParMETISLib"
fi

echo TOOLS_DIR=${TOOLS_DIRS}

echo -n "Making Directory Structure: "
mkdir -p $target

if [ "$subdir" == "" ]; then
    fulltarget=$target
else
    fulltarget=$target/$subdir
    mkdir -p $fulltarget
fi

mkdir -p $fulltarget/lib
mkdir -p $fulltarget/bin

if [ ! -h $fulltarget/Tutorial ]; then 
    ln -s $source/Tutorial $fulltarget/Tutorial
fi

for i in $TARGET_DIRS ; do
  echo -n $i " "
  mkdir -p $fulltarget/$i
done
echo

# remove Makefile link if it already exists in case external metis option
# changed
rm -f $fulltarget/ParMetis-4.0/Makefile
for i in $TOOLS_DIRS ; do
  echo -n $i " "
  mkdir -p $fulltarget/$i
done
echo

if [ ! -h $fulltarget/FVMtools/extract_movie ]; then
    cp FVMtools/extract_movie $fulltarget/FVMtools/extract_movie
    chmod a+rx $fulltarget/FVMtools/extract_movie
fi

cd $fulltarget
if [ ! -h Makefile ]; then
  echo ln -s $source/src/Makefile Makefile
  ln -s $source/src/Makefile Makefile
fi
if [ ! -h Loci.conf ]; then
  echo ln -s $source/src/conf/Loci.conf Loci.conf
  ln -s $source/src/conf/Loci.conf Loci.conf
fi
if [ ! -h Install.bash ]; then
  echo ln -s $source/src/Install.bash Install.bash
  ln -s $source/src/Install.bash Install.bash
fi

if [ ! -e comp.conf ]; then
  echo cp $source/src/conf/${COMP}.conf comp.conf
  cp $source/src/conf/${COMP}.conf comp.conf
fi
if [ ! -e sys.conf ]; then
    if [ -e $source/src/conf/${ARCH}.conf ]; then
	echo cp $source/src/conf/${ARCH}.conf sys.conf
	cp $source/src/conf/${ARCH}.conf sys.conf
    fi
fi

cd lib
#if [ ! -h libTools.a ]; then
#  ln -s ../Tools/libTools.a libTools.a
#fi
#if [ ! -h libLoci.a ]; then
#  ln -s ../System/libLoci.a libLoci.a
#fi
if [ ! -h libTools.${LIB_POSTFIX} ]; then
  ln -s ../Tools/libTools.${LIB_POSTFIX} libTools.${LIB_POSTFIX}
fi
if [ ! -h libLoci.${LIB_POSTFIX} ]; then
  ln -s ../System/libLoci.${LIB_POSTFIX} libLoci.${LIB_POSTFIX}
fi
if [ ! -h fvm_m.so ]; then
  ln -s ../FVMMod/fvm_m.so fvm_m.so
fi
if [ ! -h libfvmadaptfunc.${LIB_POSTFIX} ]; then
  ln -s ../FVMAdapt/libfvmadaptfunc.${LIB_POSTFIX} libfvmadaptfunc.${LIB_POSTFIX}
fi
if [ ! -h fvmadapt_m.so ]; then
  ln -s ../FVMAdapt/fvmadapt_m.so fvmadapt_m.so
fi
if [ ! ${DO_METIS_INSTALL} == 0 ]; then
    if [ ! -h libgk.${LIB_POSTFIX} ] ; then
	ln -s ../ParMetis-4.0/GKLib/libgk.${LIB_POSTFIX} libgk.${LIB_POSTFIX}
    fi
    if [ ! -h libmetis.${LIB_POSTFIX} ] ; then
	ln -s ../ParMetis-4.0/METISLib/libmetis.${LIB_POSTFIX} libmetis.${LIB_POSTFIX}
    fi
    if [ ! -h libparmetis.${LIB_POSTFIX} ] ; then
	ln -s ../ParMetis-4.0/ParMETISLib/libparmetis.${LIB_POSTFIX} libparmetis.${LIB_POSTFIX}
    fi
else
    rm -f libgk.* libmetis.* libparmetis.*
fi
if [ ! -h libsprng.${LIB_POSTFIX} ] ; then
    ln -s ../sprng/libsprng.${LIB_POSTFIX} libsprng.${LIB_POSTFIX}
fi
cd ../bin
if [ ! -h lpp ] ; then 
  ln -s ../lpp/lpp lpp
fi
if [ ! -h extract ] ; then
  ln -s ../FVMtools/extract extract
fi
if [ ! -h ugrid2vog ] ; then
  ln -s ../FVMtools/ugrid2vog ugrid2vog
fi
if [ ! -h vogcheck ] ; then
  ln -s ../FVMtools/vogcheck vogcheck
fi
if [ ! -h vogmerge ] ; then
  ln -s ../FVMtools/vogmerge vogmerge
fi
if [ ! -h vog2surf ] ; then
  ln -s ../FVMtools/vog2surf vog2surf
fi
if [ ! -h marker ] ; then
  ln -s ../FVMtools/marker marker
fi
if [ ! -h refmesh ] ; then
  ln -s ../FVMtools/refmesh refmesh
fi
if [ ! -h refine ] ; then
  ln -s ../FVMtools/refine refine
fi
if [ ! -h cgns2surf ] ; then
  ln -s ../FVMtools/cgns2surf cgns2surf
fi
if [ ! -h cgns2ensight ] ; then
  ln -s ../FVMtools/cgns2ensight cgns2ensight
fi
if [ ! -h cgns2ugrid ] ; then
  ln -s ../FVMtools/cgns2ugrid cgns2ugrid
fi
if [ ! -h cgns2vog ] ; then
  ln -s ../FVMtools/cgns2vog cgns2vog
fi
if [ ! -h ugrid2cgns ] ; then
  ln -s ../FVMtools/ugrid2cgns ugrid2cgns
fi


for dir in $TARGET_DIRS; do
  echo "Linking" $dir "directory files..."
  cd $source/src/$dir
  for i in *.cc *.h *.hh *.hpp *.c *.loci *.lh Makefile Loci ; do
    if [ -e  $i ] ; then
      if [ ! -h $fulltarget/$dir/$i ] ; then
        echo ln -s $source/src/$dir/$i $fulltarget/$dir/$i
        ln -s $source/src/$dir/$i $fulltarget/$dir/$i
      fi
    fi
  done
done

for dir in $TOOLS_DIRS; do
  echo "Linking" $dir "directory files..."
  cd $source/$dir
  for i in *.cc *.h *.hh *.hpp *.c *.loci *.lh Makefile Loci ; do
    if [ -f  $i ] ; then
      if [ ! -h $fulltarget/$dir/$i ] ; then
        echo ln -s $source/$dir/$i $fulltarget/$dir/$i
        ln -s $source/$dir/$i $fulltarget/$dir/$i
      fi
    fi
  done
done
if [ ! -h $fulltarget/lpp/variable.cc ] ; then
    echo ln -s $source/src/System/variable.cc $fulltarget/lpp/variable.cc
    ln -s $source/src/System/variable.cc $fulltarget/lpp/variable.cc
fi
#if [ ${DO_METIS_INSTALL} == 0 ]; then
#make dummy metis directory
#    mkdir -p $fulltarget/ParMetis-4.0
#    cp $source/ParMetis-4.0/Makefile.dummy $fulltarget/ParMetis-4.0/Makefile
#fi
#echo "** Be sure to set LOCI_BASE=\"$target/Loci\" **"


