#!/bin/sh
# builds the pindel file, using the samtools directory as an argument
# script adapted by Eric-Wubbo Lameijer, e.m.w.lameijer@lumc.nl, based on http://www.kingcomputerservices.com/unix_101/tips_on_good_shell_programming_practices.htm

#  test if we have a argument on the command line

if [ "$#" != "1" ]; then
    echo " "
    echo "INSTALL"
    echo "    purpose:  builds the pindel executable "
    echo "    usage:    ./INSTALL [samtoolsdirectory]"
    echo "    example:  ./INSTALL /home/user/tools/samtools-0.1.16"
    echo " "
    exit 1
fi

# we are ok at this point so continue processing here

# to make sure, convert relative path to absolute path

ABSPATH=`cd $1; pwd`

ABSPATH="$ABSPATH/"
echo "path is now: $ABSPATH"
if [ -e "Makefile.local" ];
  then LATERRUN="1";
fi 

make SAMTOOLS=$ABSPATH

if [ "$LATERRUN" != "1" ]; then
   echo "If this is the first time you're running this install script please wait a moment as we create the Makefile.local"
   sleep 10
   make SAMTOOLS=$ABSPATH
fi

if [ "$?" -ne 0 ]; then 
    echo "  "
    echo "INSTALL failed" 
    echo "Possible reasons: "
    echo "1. 'cannot cd to [path]"
    echo "->the samtools path provided was incorrect"
    echo "(so '../samtools/' was used instead of '/home/user/samtools/')"
    echo " "
    echo "2. 'cannot find -lbam'"
    echo "->samtools was not properly installed, in that case go to the samtools directory "
    echo "and run 'make'."
    echo " "
    echo "For futher help, see the pindel wiki and its FAQ on https://trac.nbic.nl/pindel/wiki/PindelFaq"
    echo "Or contact us on k.ye@lumc.nl" 
    exit 1
else
    echo "  "
    echo "Creating pindel2vcf utility..."
    g++ -O3 src/vcfcreator.cpp -o pindel2vcf
    echo "pindel2vcf created."
    echo " "
    echo "Creating sam2pindel utility..."
    g++ -O3 src/SAM_2_PINDEL_cin_2011Sept26.cpp -o sam2pindel
    echo "sam2pindel created."
    mv src/pindel .
    echo " "
    echo "Pindel successfully compiled. The pindel executable can be found in this directory."
    echo " "
    echo "You can test pindel with the example files in the demo directory; example commands are given in the RUNME in that directory."
    echo " " 
    echo "For help and background information, see the pindel wiki and its FAQ on https://trac.nbic.nl/pindel/wiki/PindelFaq"
    echo "Or contact us on k.ye@lumc.nl" 
    echo " "
fi 
