# *************************************************************
#  
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#  
#    http://www.apache.org/licenses/LICENSE-2.0
#  
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.
#  
# *************************************************************

if test -z "${SOLARENV}"; then
    if test -f ./source_soenv.sh; then . ./source_soenv.sh; fi
fi
if test -z "${SOLARENV}"; then
    echo "bootstrap: No environment set!"
    exit 1
fi

# remove aliases set by *Env.Set.sh
unalias mkout
unalias deliver
unalias build
unalias build_client
unalias zipdep
unalias nmake
unalias gunzip

# executables are *.exe for WNT. This variable is necessary since Cygwin 1.5.x
# Use spawn instead of fork when building dmake on cygwin. 
if test "$GUI" = "WNT"; then
    EXEEXT=".exe"
else
    EXEEXT=""
fi
export EXEEXT

mkdir -p "$SOLARENV/$INPATH/bin"

#make sure build.pl is executable 

chmod +x "$SRC_ROOT/solenv/bin/build.pl"
chmod +x "$SRC_ROOT/solenv/bin/build_client.pl"
chmod +x "$SRC_ROOT/solenv/bin/zipdep.pl"
chmod +x "$SRC_ROOT/solenv/bin/gccinstlib.pl"

# fetch or update external tarballs
if [ "$DO_FETCH_TARBALLS" = "yes" ]; then
# check perl include locations
    "$PERL" -e 'print "\nInclude locations: @INC\n\n"';
    "$PERL" "$SOLARENV/bin/download_external_dependencies.pl" $SRC_ROOT/external_deps.lst
    if [ "$?" != "0" ]; then
        echo "*** Error downloading external dependencies, please fix the previous problems and try again ***"
        exit 1
    fi
fi

# ------------------------------------------------------------------------------
# Build dmake

if test -n "$DMAKE_URL" -a  ! -x "$SOLARENV/$INPATH/bin/dmake$EXEEXT"; then

    # Assume that the dmake archive has been downloaded
    # Determine the name of the downloaded file.
    dmake_package_name=`echo $DMAKE_URL | sed "s/^\(.*\/\)//"`

    tmp_build_dir="$SOLARENV/$INPATH/misc/build/"
    echo "making and entering $tmp_build_dir"
    # Clean up any residues from a previous and unsuccessful build.
    rm -rf "$tmp_build_dir"
    mkdir -p "$tmp_build_dir"
    cd "$tmp_build_dir" || exit

    dmake_full_package_name=$(find $TARFILE_LOCATION -type f -name "*-$dmake_package_name")
    if [ -z "$dmake_full_package_name" ]; then
        echo "can not find the dmake package"
        exit
    fi
    if test "$GUI" = "WNT"; then
        dmake_full_package_name=`cygpath -u "$dmake_full_package_name"`
    fi
    echo "unpacking $dmake_full_package_name"

    # Unpack it.
    case $dmake_package_name in
        *.tar.gz)
            tar -xzf "$dmake_full_package_name"
            dmake_directory_name=`echo $dmake_package_name | sed "s/\(\.tar.gz\)//"`
            ;;
        *.tgz)
            tar -xzf "$dmake_full_package_name"
            dmake_directory_name=`echo $dmake_package_name | sed "s/\(\.tgz\)//"`
            ;;
        *.tar.bz2)
            tar -xjf "$dmake_full_package_name"
            dmake_directory_name=`echo $dmake_package_name | sed "s/\(\.tar.bz2\)//"`
            ;;
        *.zip)
            unzip "$dmake_full_package_name"
            dmake_directory_name=`echo $dmake_package_name | sed "s/\(\.zip\)//"`
            ;;
        *)
            echo "can not unpack the dmake source"
            dmake_directory_name=
            exit 1
            ;;
    esac

    echo "entering $dmake_directory_name"
    cd "$dmake_directory_name" || exit

    # Special case! The w32/tcsh build needs CC pointing to the MSVC++ compiler
    # but we need a cygwin/gcc build dmake to understand the posix paths
    if test "$GUI" = "WNT"; then
        CC=""
        CXX=""
        export CC
        export CXX
        DMAKE_CONF="--enable-spawn"
    else
        DMAKE_CONF=""
    fi

    # For unixy systems
    if test -f "Makefile" ; then
        $GNUMAKE distclean || exit
    fi

    # config.guess and config.sub for dmake are too old, overwrite with ours:
    cp $SOLARSRC/config.guess .
    cp $SOLARSRC/config.sub .

    ./configure $DMAKE_CONF || exit

    ## invoke the gnu make command set by configure.
    $GNUMAKE || exit


    # Deploy the dmake executable to solenv
    cp -f "$tmp_build_dir/$dmake_directory_name/dmake$EXEEXT" "$SOLARENV/$INPATH/bin/dmake$EXEEXT" || exit
    echo ""
    echo "dmake successfully built and copied to $SOLARENV/$INPATH/bin/dmake$EXEEXT"
    echo ""

    # Clean up. Note that this is skipped when one of the exits is executed above.
    rm -rf "$tmp_build_dir"

elif test "$IS_SYSTEM_DMAKE" = "YES"; then

    echo ""
    echo "dmake is located in search path"
    echo ""

elif test -n "$DMAKE_PATH" -a -x "$DMAKE_PATH" -a  ! -x "$SOLARENV/$INPATH/bin/dmake$EXEEXT"; then

    cp -f "$DMAKE_PATH" "$SOLARENV/$INPATH/bin/dmake$EXEEXT" || exit

    echo ""
    echo "dmake copied to $SOLARENV/$INPATH/bin/dmake$EXEEXT"
    echo ""

else

    if test -x "$SOLARENV/$INPATH/bin/dmake$EXEEXT"; then
        echo ""
        echo "dmake present in $SOLARENV/$INPATH/bin/dmake$EXEEXT"
    fi
    
fi

# Download missing extensions.
"$PERL" "$SOLARENV/bin/download_missing_extensions.pl"