#!/bin/bash
#######################
# University of Wisconsin Madison,  Space Science Engineering Center (SSEC)
#file_Date = '$Date: 2016-07-05 15:12:24 -0400 (Tue, 05 Jul 2016) $'
#file_Revision = '$Revision: 775 $'
#file_Author = '$Author: scottm $'
#file_HeadURL = '$HeadURL: https://svn.ssec.wisc.edu/repos/jpss_adl_distribution/trunk/httpsFiles/SSEC-Support/ADL_BLOCK_2/scripts/adl_cots_install.sh $'
#file_Id = '$Id: adl_cots_install.sh 775 2016-07-05 19:12:24Z scottm $'
#######################
# University of Wisconsin Madison,  Space Science Engineering Center (SSEC)
# script to install ADL cots from server at UW-Madison
#ADL_COTS/adl_cots.sh wipe all 2>&1 | tee ADL_COTS/java.log
gcc --version

# gcc netos6  Comment these out for buildbucket
export CC="gcc"
export CPP="cpp"
export CXX="g++"
export FORTRAN="gfortran"
export FC="gfortran"
# gcc netos6  Comment above out for buildbucket

#CC, FC, CXX, F90, F77, F9X
#LINUXCPUFLAGS="-msse4a -fPIC"
#gcc buildbucket
export FORTRAN=${FC}

export F77=${FORTRAN}
export FC=${FORTRAN}
export LOADER=${FORTRAN}
export F77_COMPILER=${FORTRAN}
export F90_COMPILER=${FORTRAN}
export F95_COMPILER=${FORTRAN}
export CPP_COMPILER=${FORTRAN}
export FFLAGS=" -O3  -msse4a  -fPIC -Wall -funderscoring -x f77"

###"-msse4a -msse4a LINUXCPUFLAGS="-march=ivybridge"

echo "CPPFLAGS" $CPPFLAGS
echo "CPATH" $CPATH

export F77_FLAGS=${FFLAGS}
export CFLAGS="  -msse4a -g -O3 -fPIC -Wall -Wno-unknown-pragmas -Wunused-variable -Wunused-but-set-parameter "
export CPPFLAGS="  -msse4a -g -O3 -fPIC -Wall -Wno-unknown-pragmas -Wunused-variable  -fpermissive -Wunused-but-set-parameter "
export CXXFLAGS=$CPPFLAGS

echo "CPPFLAGS" $CPPFLAGS
echo "CPATH" $CPATH

COTS_HOME=/csppbinaries/cspp-sdr-binary/distro/
COTSBASE=/CSPP_SDR_4_0
mkdir -p ${COTSBASE}
echo $COTSBASE
echo $COTS_HOME


export ADL_COTS=${COTS_HOME}/tars
export USER_COTS_PATH=${COTSBASE}/libexec
export BUILD=${COTSBASE}/build

echo "LD_RUN_PATH" $LD_RUN_PATH
echo "LD_LIBRARY_PATH" $LD_LIBRARY_PATH
echo "PATH" $PATH

export LD_LIBRARY_PATH=${USER_COTS_PATH}/lib64:${USER_COTS_PATH}/lib:${LD_LIBRARY_PATH}
export LD_RUN_PATH=${USER_COTS_PATH}/lib64:${USER_COTS_PATH}/lib:${LD_RUN_PATH}
export PATH=${USER_COTS_PATH}/bin:$PATH

install_the_source (){
    echo ${FUNCNAME[0]}
    # This will download and unpack the requested COT.  If the cot is already present it will not be downloaded.
    tar_name=$1
    
    destination=${BUILD}"/"
    
    echo "install " ${tar_name} "to "${destination}
    
    if [ -e ${ADL_COTS}/${tar_name} ] ;
    then
        cd ${BUILD}
        tar -xf ${ADL_COTS}/${tar_name}
        if [ $? == "0" ] ;
        then
            echo "Package unpacked: " ${tar_name}
            return 0
        else
            unzip ${ADL_COTS}/${tar_name}
            return 0
        fi
    else
        echo "No source " ${ADL_COTS}/${tar_name}
    fi
    return 1
}

echo_var()
{
    var=${1}
    echo $1
    echo "$1="${${var}}
}

echo_vars()
{
    echo_var "COTS_HOME"
    echo_var "DOWNLOAD"
    echo_var "BUILD"
    echo_var "USER_COTS_PATH"
    
    echo_var "CC"
    echo_var "CPP"
    echo_var "CXX"
    
    echo_var "FORTRAN"
    echo_var "F77"
    echo_var "FC"
    echo_var "LOADER"
    echo_var "F77_COMPILER"
    echo_var "F90_COMPILER"
    echo_var "F95_COMPILER"
    echo_var "CPP_COMPILER"
    echo_var "FFLAGS"
    echo_var "F77_FLAGS"
    
    echo_var "LD_RUN_PATH"
    echo_var "LD_LIBRARY_PATH"
    
    gcc -version
    gfortran --version
    
}



mk_xerces() {
    echo ${FUNCNAME[0]}
    tar_name=xerces-c-3.2.3.tar.gz && \
    install_the_source ${tar_name} && \
    cd ${BUILD}/xerces-c-3.2.3 && \
    ./configure  --enable-static --enable-shared   --prefix=${USER_COTS_PATH} && \
    make && \
    make install && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

mk_png() {
    echo ${FUNCNAME[0]}
    tar_name=libpng-1.6.37.tar.gz && \
    install_the_source ${tar_name} && \
    cd ${BUILD}/libpng* && \
    ./configure   --prefix=${USER_COTS_PATH} && \
    make && \
    make install && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}


mk_zlib() {
    echo ${FUNCNAME[0]}
    tar_name=zlib-1.2.7.tar.gz && \
    install_the_source ${tar_name} && \
    cd ${BUILD}/zlib* && \
    ./configure   --prefix=${USER_COTS_PATH} && \
    make && \
    make install && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

mk_bz2() {
    echo ${FUNCNAME[0]}
    tar_name=bzip2-1.0.6.tar.gz && /
    install_the_source ${tar_name} && /
    cd ${BUILD}/bz* && /
    make clean  && /
    make install PREFIX=${USER_COTS_PATH} CFLAGS=" ${CFLAGS} -Winline  -D_FILE_OFFSET_BITS=64" CC="${CC}" && \
rm bzless; ln -sr bzmore bzless  && / 
rm bzegrep; ln -sr bzgrep bzegrep  && / 
rm bzfgrep; ln -sr bzgrep bzfgrep  && /

    echo "${FUNCNAME[0]} finished" && \
    return 0
}
# -D_GLIBCXX_USE_CXX11_ABI=0
mk_boost(){
    echo ${FUNCNAME[0]}
    tar_name=boost_1_57_0.tar.gz  && \
    rm -fr ${USER_COTS_PATH}/lib/*boost* && \
    rm -fr ${USER_COTS_PATH}/include/boost && \
    install_the_source ${tar_name} && \
    cd ${BUILD}/boost* && \
    ./bootstrap.sh --help
    ./bootstrap.sh --with-toolset=gcc --with-libraries="filesystem","iostreams","regex","thread","serialization","system" --prefix="${USER_COTS_PATH}" && \
    ./b2 --help-options
    ./b2  install  toolset=gcc -q --build-dir=tmp --debug-configuration -d+2 --prefix="${USER_COTS_PATH}"  linkflags="-L${USER_COTS_PATH}/lib" cxxflags="${CXXFLAGS}  -Wunused-but-set-parameter "  && \
    #./b2  install --debug-configuration -d+2 --prefix="'${USER_COTS_PATH}'" --build-dir=tmp/build-boost optimization=speed linkflags="-L${USER_COTS_PATH}/lib" cxxflags="'${CXXFLAGS}' "  && \
    
    echo "${FUNCNAME[0]} finished" && \
    return 0
}


mk_wgrib2() {
    echo ${FUNCNAME[0]}
    tar_name=wgrib2.v2.0.2.tgz  && \
    rm -fr ${BUILD}/grib2  && \
    install_the_source ${tar_name}  && \
    cd ${BUILD}/g*  && \
    # do this twice
    make USE_G2CLIB=1  && \
    cp -f ./g2clib-1.4.0/libgrib2c.a ${USER_COTS_PATH}/lib   && \
    cp -f --preserve=links  ./libpng*/.libs/*  ${USER_COTS_PATH}/lib   && \
    cp -f  --preserve=links ./jasp*/src/libjasper/.libs/* ${USER_COTS_PATH}/lib  && \
    cp -f ./g2clib-1.4.0/grib2.h ${USER_COTS_PATH}/include && \
    cp ${BUILD}/grib2/wgrib2/wgrib2 ${USER_COTS_PATH}/bin && \
    #ln -fs ${USER_COTS_PATH}/lib ${USER_COTS_PATH}/g2clib-1.4.0 && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

mk_hdf5() {
    echo ${FUNCNAME[0]}
    gcc --version
    tar_name=hdf5-1.10.4.tar.gz  && \
    install_the_source ${tar_name} && \
    cd ${BUILD}/hdf*  && \
    ./configure  --enable-static=yes --enable-shared=yes --with-zlib=${USER_COTS_PATH} --with-slib=${USER_COTS_PATH} --with-szlib=${USER_COTS_PATH} --prefix=${USER_COTS_PATH} CPPFLAGS="${CPPFLAGS}" CXXFLAGS="${CXXFLAGS}" CPATH="${CPATH}" C_INCLUDE="${C_INCLUDE}" CFLAGS="${CFLALGS}"
    CC="${CC}"  && \
    make clean && \
    make -j 3  && \
    make install && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

mk_log4cplus() {
    echo ${FUNCNAME[0]}
    tar_name=log4cplus-1.0.4.3.tar.gz  && \
    install_the_source ${tar_name}  && \
    cd ${BUILD}/log4c*  && \
    ./configure  --prefix=${USER_COTS_PATH}  && \
    make  && \
    make install && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

mk_szip()
{
    echo ${FUNCNAME[0]}
    tar_name=szip-2.1.tar.gz && \
    rm -fr ${BUILD}/szip* && \
    #rm ${USER_COTS_PATH}/lib/*slibc*
    install_the_source ${tar_name} && \
    cd ${BUILD}/szip* && \
    ./configure  --prefix=${USER_COTS_PATH} && \
    make && \
    make install && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

mk_slibc()
{
    echo ${FUNCNAME[0]}
    tar_name=slibc-0.9.3.tar.gz && \
    
    rm -fr ${BUILD}/slibc* && \
    rm -fr ${USER_COTS_PATH}/lib/*slibc* && \
    install_the_source ${tar_name} && \
    cd ${BUILD}/slibc* && \
    export PATH=${USER_COTS_PATH}/bin:${PATH} && \
    cd src && \
    cat stdio_s.c | sed "s#inline void get_s_cleanup#void get_s_cleanup#" > test.c && \
    mv test.c stdio_s.c && \
    cd .. && \
    make  && \
    cp  -r  include/* ${USER_COTS_PATH}/include && \
    cp  src/libslibc.so.0.9.3  ${USER_COTS_PATH}/lib/ && \
    cd  ${USER_COTS_PATH}/lib && \
    ln -s   ./libslibc.so.0.9.3 libslibc.so && \
    ln -s   ./libslibc.so.0.9.3 libslibc++.so && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

mk_sqlite()
{
    echo ${FUNCNAME[0]}
    tar_name=sqlite-autoconf-3330000.tar.gz  && \
    rm -fr ${BUILD}/SQ* && \
    cd ${BUILD} && \
    install_the_source ${tar_name}  && \
    cd ${BUILD}/sqlite* && \
    ./configure  --prefix=${USER_COTS_PATH} --exec-prefix=${USER_COTS_PATH} --disable-tcl && \
    make && \
    make install && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

#liblapack.a
mk_lapack(){
    echo ${FUNCNAME[0]}
    tar_name=lapack-3.5.0.tgz && \
    rm -fr ${BUILD}/lapack* && \
    install_the_source $tar_name && \
    cd ${BUILD}/lapack* && \
    cp make.inc.example make.inc && \
    echo "Building BLAS / LAPACK" && \
    cd BLAS/SRC && \
    export LD_RUN_PATH=${USER_COTS_PATH}/lib64:${USER_COTS_PATH}/lib && \
    echo $LD_RUN_PATH && \
    export FORTRAN=${FC} && \
    export LOADER=${FC} && \
    
    
    make OPTS="   -msse4a  -O3 -fPIC -Wall  -funderscoring  "  LD_RUN_PATH=${LD_RUN_PATH} FORTRAN="${FORTRAN} -funderscoring  -g " LOADER="${LOADER}" && \
    cd ../.. && \
    make OPTS="  -msse4a  -fPIC -Wall  " LD_RUN_PATH=${LD_RUN_PATH}  FORTRAN="${FORTRAN} -fimplicit-none  -funderscoring  -g" LOADER="${LOADER} -g" && \
    cp librefblas.a  libblas.a && \
    cp *.a  ${USER_COTS_PATH}/lib && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}


mk_gtest()
{
    echo ${FUNCNAME[0]}
    tar_name=gtest-1.7.0.zip  && \
    rm -fr ${BUILD}/gtest*  && \
    cd ${BUILD} && \
    install_the_source ${tar_name}  && \
    cd ${BUILD}/gtest*  && \
    ./configure  --prefix=${USER_COTS_PATH}  && \
    
    echo "${FUNCNAME[0]} finished" && \
    return 0
    
}

mk_proto()
{
    echo ${FUNCNAME[0]}
    export PATH=/CSPP_SDR_4_0/JAVA_COTS/jdk1.8.0_73/bin/:$PATH

    tar_name=protobuf-3.8.x.zip  && \
    rm -fr ${BUILD}/protobuf*  && \
    rm -fr ${USER_COTS_PATH}//google/protobuf  && \
    rm -fr ${USER_COTS_PATH}/lib/*proto*  && \
    rm -fr ${USER_COTS_PATH}/lib/*proto*  && \
    cd ${BUILD} && \
    install_the_source  ${tar_name}  && \
    cd ${BUILD}/protobuf*  && \
    echo ${USER_COTS_PATH}  && \
    
    libtoolize --force  && \
    ./autogen.sh --prefix=${USER_COTS_PATH}  && \
    ./configure  --prefix=${USER_COTS_PATH}  && \
    make -j 3 && \
    make install  && \
    LIB=${BUILD}/protobuf-3.8/lib  && \
    echo "Now java part at line 366"  && \
    cd ${BUILD}/protobuf*/java  && \
    export LD_LIBRARY_PATH=${USER_COTS_PATH}/lib  && \
    export LD_RUN_PATH=${USER_COTS_PATH}/lib  && \
    ${USER_COTS_PATH}/bin/protoc --java_out=core/src/main/java -I../src ../src/google/protobuf/descriptor.proto  && \
    cd ${BUILD}/protobuf*  && \
    cd java/core/src/main/java/com/google/protobuf  && \
    ls -al /CSPP_SDR_4_0/JAVA_COTS/jdk1.8.0_73/bin/ && \
    /CSPP_SDR_4_0/JAVA_COTS/jdk1.8.0_73/bin/javac *.java  && \
    cd ${BUILD}/protobuf*  && \
    cd java/core/src/main/java  && \
    jar cf  ${USER_COTS_PATH}/lib/protobuf-java-3.8.0.jar com/google/protobuf/*.class && \
    echo "mk_proto complete"
    
}

mk_fftw() {
    echo ${FUNCNAME[0]}
    tar_name=fftw-3.3.4.tar.gz && \
    install_the_source $tar_name && \
    cd ${BUILD}/fftw* && \
    CFLAGS=-msse4a ./configure --with-gcc-arch=pentium4 --enable-shared=yes --enable-static=yes --enable-portable-binary --prefix=${USER_COTS_PATH} && \
    make && \
    make install
}


mk_slatec4 ()
{
    echo ${FUNCNAME[0]}
    tar_name=slatec.tar.gz && \
    
    install_the_source $tar_name && \
    cd ${BUILD}/slatec/src && \
    make CXXFLAGS="  -msse4a  -funderscoring  -O3 -fPIC -Wall -Wno-unknown-pragmas " && \
    mv static/libslatec.a ${USER_COTS_PATH}/lib
}

mk_pppack() {
    echo ${FUNCNAME[0]}
    tar_name=pppack.tar.gz && \
    rm -fr ${BUILD}/pppack* && \
    install_the_source ${tar_name} && \
    # this must use underscores
    cd ${BUILD}/PPPack && \
    echo "Building pppack" && \
    export F77_COMPILER="${FC}" && \
    export F90_COMPILER="${FC}" && \
    export F95_COMPILER="${FC}" && \
    export CPP_COMPILER="${FC}" && \
    
    make FCompile_Optn=" -m64 -fPIC -ffree-form -fimplicit-none -funderscoring"  F77Compile_Optn=" -m64 -fPIC -ffixed-form -fimplicit-none -funderscoring -x f77" F77_FLAGS="-m64 -fPIC -ffixed-form -funderscoring -x f77" && \
    mv libpppack.a ${USER_COTS_PATH}/lib
}

mk_libtool()
{
    echo ${FUNCNAME[0]}
    tar_name=libtool-2.4.6.tar.gz  && \
    rm -fr ${BUILD}/libtool*  && \
    install_the_source ${tar_name}  && \
    cd ${BUILD}/libtool*  && \
    ./configure  --prefix=${USER_COTS_PATH}  && \
    make  && \
    make install && \
    echo "${FUNCNAME[0]} finished"
}

mk_cminpak(){
    echo ${FUNCNAME[0]}
    tar_name=cminpack-1.3.6.zip  && \
    cd ${BUILD} && \
    rm -fr ${BUILD}/cminpack-1.3.6 && \
    unzip ${ADL_COTS}/$tar_name  && \
    cd ${BUILD}/cminpack-1.3.6 && \
    make -f Makefile && \
    cp cminpack.h ${USER_COTS_PATH}/include && \
    cp libcminpack.a ${USER_COTS_PATH}/lib && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}



where()
{
    echo "${FUNCNAME[0]}"
    cd ${BUILD}  
    rm -fr where  
    mkdir where  && \
    cd where  && \
    cp ${ADL_COTS}/getday.c .  && \
    cp ${ADL_COTS}/gettim.c .  && \
    cp ${ADL_COTS}/where.f .  && \
    gfortran where.f gettim.c getday.c -o where  && \
    cp where ${USER_COTS_PATH}/bin  && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

mk_hl_region()
{
    echo ${FUNCNAME[0]}
    rm -fr ${BUILD}/hdf5_HL* && \
    tar_name=hdf5_HL_REGION-1.1.5.tar.gz  && \
    install_the_source $tar_name   && \
    export HDF5_INSTALL_DIR=${USER_COTS_PATH} && \
    export CPPFLAGS="-I${USER_COTS_PATH}/include"  && \
    export LDFLAGS="-static -L${USER_COTS_PATH}/lib64 -L${USER_COTS_PATH}/lib"  && \
    cd ${BUILD}/hdf5_HL*  && \
    linc=`pwd`  && \
    make clean  && \
    make CC=$CC INCLUDES="$CPPFLAGS -I$linc/src $LDFLAGS -lhdf5 -lhdf5_hl -lm"  && \
    make PREFIX=${USER_COTS_PATH} install  && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

mk_nagg(){
    echo "${FUNCNAME[0]}"
    # export CPPFLAGS="-I${USER_COTS_PATH}/include"  && \
    # export CFLAGS="-I${USER_COTS_PATH}/include"  && \
    # export LDFLAGS=" -static -L${USER_COTS_PATH}/lib64 -L${USER_COTS_PATH}/lib"  && \
    # export LIBS="-lhdf5"

    cd  ${BUILD}  
    tar_name=nagg-1.6.2.tar.gz  
    rm -fr ${BUILD}/nagg*  
    install_the_source ${tar_name}  && \
    cd nagg-1.6.2  && \
    patch -p1 <  ${COTS_HOME}/scripts/revised_nagg.patch   && \
    ./configure  --prefix=${USER_COTS_PATH} --exec-prefix="${USER_COTS_PATH}" --with-hlregion="${USER_COTS_PATH}/lib" CFLAGS="${CFLAGS}" CPPFLAGS="${CPPFLAGS}" LDFLAGS=" ${LDFLAGS}"  && \
    make   && \
    make install   && \
    unset LDFLAGS   && \
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

wipe()
{
    rm -fr ${BUILD}
    rm -fr ${USER_COTS_PATH}
    mkdir -p ${BUILD}
    mkdir -p ${USER_COTS_PATH}
    mkdir -p ${USER_COTS_PATH}/lib64
    mkdir -p ${USER_COTS_PATH}/bin/
    mkdir -p ${USER_COTS_PATH}/include
}

for_container()
{
    echo ${FUNCNAME[0]}
    MYLIBS=/usr/lib64/
    
    
    # rm ${USER_COTS_PATH}/lib/libgfortran.*
    # rm ${USER_COTS_PATH}/lib/libquadmath.*
    # rm ${USER_COTS_PATH}/lib/libgcc*
    # rm ${USER_COTS_PATH}/lib/libstdc++*
    
    
    # rm ${USER_COTS_PATH}/lib64/libgfortran.*
    # rm ${USER_COTS_PATH}/lib64/libquadmath.*
    # rm ${USER_COTS_PATH}/lib64/libgcc*
    # rm ${USER_COTS_PATH}/lib64/libstdc++*
    mkdir -p ${USER_COTS_PATH}/lib64
    cp --preserve=links ${MYLIBS}/libgfortran.* ${USER_COTS_PATH}/lib64
    cp --preserve=links ${MYLIBS}/libquadmath.* ${USER_COTS_PATH}/lib64
    cp --preserve=links ${MYLIBS}/libgcc* ${USER_COTS_PATH}/lib64
    cp --preserve=links ${MYLIBS}/libstdc++* ${USER_COTS_PATH}/lib64
    
    cmd="ls -al ${USER_COTS_PATH}/lib64"
    echo $cmd
    $cmd
    
}
fix_links()
{

    cd ${USER_COTS_PATH}/bin  && \
    rm -f bzcmp  && \
    ln -s ./bzcmp bzdiff  && \
    rm -f bzegrep  && \
    ln -s ./bzegrep bzgrep  && \
    rm -f bzfgrep  && \
    ln -s ./bzfgrep bzgrep  && \
    rm -f bzless  && \
    ln -s ./bzless bzmore  && \  
    echo "${FUNCNAME[0]} finished" && \
    return 0
}

_rpath_setter() {

    DESIRED_RPATH=$1
    ORIGIN_DIRECTORY=$2
    cd "${ORIGIN_DIRECTORY}"
    #EXES=$(ls *)
    EXES=$(find "${ORIGIN_DIRECTORY}" -type f -perm -700)

    printf "Desired %s\n" $DESIRED_RPATH
    for exe in ${EXES}; do
        printf "Start for %s\n" $exe
        #patchelf --remove-rpath $exe
        patchelf --set-rpath ${DESIRED_RPATH} ${exe}
        patchelf --shrink-rpath ${exe}
        printf "Final for %s\n" ${exe}
        patchelf --print-rpath ${exe}
        ldd ${exe}
    done
}
export ADL_HOME=${COTSBASE}/libexec/ADL
patch_adl_rpaths() {

    DESIRED_RPATH='$ORIGIN:$ORIGIN/../../bin:$ORIGIN/../../lib:$ORIGIN/../../lib64:$ORIGIN/../bin:$ORIGIN/../lib:'
    _rpath_setter $DESIRED_RPATH ${ADL_HOME}/lib
    _rpath_setter $DESIRED_RPATH ${ADL_HOME}/bin
    DESIRED_RPATH='$ORIGIN:$ORIGIN/../../../bin:$ORIGIN/../../../lib:$ORIGIN/../../../lib64:$ORIGIN/../../bin:$ORIGIN/../../lib:'

    _rpath_setter $DESIRED_RPATH ${ADL_HOME}/tools/bin

    cd ${ADL_HOME}/bin
    special=$(ls *.exe)
    for spec in $special; do
        patchelf --replace-needed /SDR_x_x/libexec/ADL/lib/libProSdrAtmsController.so libProSdrAtmsController.so $spec
        patchelf --replace-needed /SDR_x_x/libexec/ADL/lib/libProSdrViirsGeoController.so libProSdrViirsGeoController.so $spec
        patchelf --replace-needed /SDR_x_x/libexec/ADL/lib/libProSdrViirsCalController.so libProSdrViirsCalController.so $spec
        patchelf --replace-needed /SDR_x_x/libexec/ADL/lib/libProSdrCrisFSController.so libProSdrCrisFSController.so $spec
        patchelf --replace-needed /SDR_x_x/libexec/ADL/lib/libProSdrOmpsNpEarthController.so libProSdrOmpsNpEarthController.so $spec
        patchelf --replace-needed /SDR_x_x/libexec/ADL/lib/libProSdrOmpsTcEarthController.so libProSdrOmpsTcEarthController.so $spec

    done

}

all()
{
    
    DESIRED_RPATH='$ORIGIN:$ORIGIN/../bin:$ORIGIN/../lib:$ORIGIN/../lib64:'
    wipe
    for_container && \
    mk_xerces && \
    mk_png  && \
    mk_zlib && \
    mk_bz2 && \
    mk_boost && \
    mk_wgrib2 && \
    mk_szip && \
    mk_hdf5 && \
    mk_log4cplus && \
    mk_slibc && \
    mk_sqlite && \
    mk_fftw && \
    mk_slatec4 && \
    mk_lapack && \
    mk_pppack && \
    mk_libtool && \
    mk_cminpak && \
    mk_proto && \
    where  && \
    mk_hl_region && \
    mk_nagg && \
    fix_links && \
    echo "COTS Built" && \
    _rpath_setter ${DESIRED_RPATH} ${USER_COTS_PATH}/lib && \
    _rpath_setter ${DESIRED_RPATH} ${USER_COTS_PATH}/lib64 && \
    _rpath_setter ${DESIRED_RPATH} ${USER_COTS_PATH}/bin && \
    echo "Rpaths patched"
}

adl()
{
    echo ${FUNCNAME[0]}
    export PATH=/CSPP_SDR_4_0/JAVA_COTS/jdk1.8.0_73/bin/:$PATH

    cd ${BUILD}
    tar_name=cspp_sdr_4_0_ADL.tar && \
    rm -fr ${BUILD}/ADL && \
    install_the_source $tar_name && \
    cd ${BUILD}/ADL && \
     export JAVA_COTS=${COTSBASE}/JAVA_COTS && \
    export ADL_HOME=${BUILD}/ADL && \
    export COTS_PYTHON_HOME=/user/libexec && \
    echo "COTS_HOME"  ${COTS_HOME}   && \
    echo "COTSBASE"  ${COTSBASE}  && \
    echo "ADL_HOME"  ${ADL_HOME}  && \
    export LUSER=/ && \
    export USER=/ && \
    export ADL_INSTALL=${ADL_HOME}  && \
    #ln -s  /usr/bin/python3 /usr/local/bin/python3 && \
    cd ${ADL_HOME} && \
    export PATH=${ADL_HOME}/bin:/usr/libexec/bin:$PATH && \
    source build/envSetup.sh && \
    export COTS_PYTHON=/usr/local/ && \
    ./buildAdl.sh 2>&1 | tee ${COTS_HOME}/build_adl.log
}

if [ "$1" == "all" ] ; then
    
    all
else
    adl
fi



