#!/bin/bash
# A bash rc file for use inside a wcdo image.


PS1="[$wcdo_simg]\u@\h:\W> "

# some general helpers
goto () {
    pushd $1 >& /dev/null
}
goback () {
    popd >& /dev/null
}
path-remove () {
    local IFS=':'
    local NEWPATH
    local DIR
    local PATHVARIABLE=${2:-PATH}
    for DIR in ${!PATHVARIABLE} ; do
        if [ "$DIR" != "$1" ] ; then
            NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
        fi
    done
    export $PATHVARIABLE="$NEWPATH"
}

path-prepend () {
    path-remove "$1" "$2"
    local PATHVARIABLE="${2:-PATH}"
    export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"
}

path-append () {
    path-remove "$1" "$2"
    local PATHVARIABLE="${2:-PATH}"
    export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1"
}

path-find () {
    local IFS=':'
    local DIR=''
    local PATHVARIABLE="${2:-PATH}"
    for DIR in ${!PATHVARIABLE}
    do
        local maybe="$DIR/$1"
        if [ -f "$maybe" ] ; then
            echo "$maybe"
        fi
    done
    
}

fhicl-find () {
    path-find $1 FHICL_FILE_PATH
}




wcdo-wirecell-path-help () {
    cat 1>&2 <<EOF 
Set or display the WIRECELL_PATH

usage: wcdo-wirecell-path [ default <path> <path> ... ]

With no argument, print the current path.

A special "<path>" given as the literal string "default" will cause
the default directories for the container to be used.

Otherwise a <path> argument is prepended.

Note: the UPS product "wirecell" destroys the WIRECELL_PATH so this
function will be useful after any "setup" of "wirecell" UPS product.

EOF
}
wcdo-wirecell-path () {
    if [ -z "$1" ] ; then
        echo "WIRECELL_PATH=$WIRECELL_PATH"
        return
    fi

    # set this, but warning that calling "setup" with the "wirecell"
    # UPS product will likely destroy it.
    for one in $@
    do
        if [ "$one" = "wcdo" -o "$one" = "default" ] ; then
            for one in ${wcdo_wct_data} ${wcdo_wct_cfg}
            do
                path-prepend $one WIRECELL_PATH 
            done
        else
            path-prepend $one WIRECELL_PATH
        fi
    done
    export WIRECELL_PATH
}


##
## UPS helpers
## 


wcdo-ups-init-help () {
    cat 1>&2 <<EOF
Try to initialize local UPS products area.

usage wcdo-ups-init

This will hunt for the UPS "setup" script in a few places.  It will
also try to prime the $wcdo_ups_products area if needed.

EOF
}
wcdo-ups-init () {
    if [ -n "$SETUP_UPS" ] ; then
        return
    fi

    for maybe in "$wcdo_ups_products" /cvmfs/larsoft.opensciencegrid.org/products
    do
        if [ -f "$maybe/setup" ] ; then
            echo "Using UPS from: $maybe" 1>&2
            source "$maybe/setup"
            return
        fi
    done
    echo "unable to initialize UPS products area" 1>&2
}


# Return a version for newest package
wcdo-ups-version () {
    local pkg="${1:-larsoft}"
    ups list -aK+ "$pkg" | tail -1 | awk '{print $2}' | sed -e 's/"//g'
}

# Return the quals for a most reacent package
wcdo-ups-quals () {
    local pkg="${1:-larsoft}"
    ups list -aK+ "$pkg" | tail -1 | awk '{print $4}' | sed -e 's/"//g'
}
    
# declare a UPS product
wcdo-ups-declare-help () {
    cat 1>&2 <<EOF
Prepare an empty UPS product area based on an existing one.

usage: wcdo-ups-declare package newversion

An example:

  wcdo-ups-declare wirecell DEVEL

Note, an existing version of this package must already be "setup".

To find a suitable version

  ups list -aK+ wirecell
  setup <package> <version> -q <quals>


EOF
}
wcdo-ups-declare () {
    local pkg="${1}"
    local ver="${2}"
    if [ -z "$ver" ] ; then
        wcdo-ups-declare-help
        return
    fi


    local PKG=$(echo $pkg | awk '{print toupper($0)}')
    envstr=$(env | grep "^SETUP_${PKG}=")

    if [ -z "$envstr" ] ; then
        echo "Must first 'setup' and existing version of ${pkg}" 1>&2
        return
    fi

    path-prepend $wcdo_ups_products PRODUCTS

    local relver=$(echo "$envstr" | awk '{print $2}')
    local relquals=$(echo "$envstr" | awk '{print $8}')
    local reldir=$(env | grep "^${PKG}_DIR" | tr '=' ' ' | awk '{print $2}')

    local tabfile="$reldir/ups/${pkg}.table"

    if [ ! -f "$tabfile" ] ; then
        echo "Can not find table file: $tabfile"
        return
    fi

    local upsdir="${wcdo_ups_products}/${pkg}/${ver}/ups"

    if [ ! -d "$upsdir" ] ; then
        mkdir -p "$upsdir"
    fi
    cp "$tabfile" "$upsdir"

    set -x
    ups declare wirecell ${ver} \
        -f $(ups flavor) \
        -q ${relquals} \
        -r "${pkg}/${ver}" \
        -z "$wcdo_ups_products" \
        -U ups  \
        -m "${pkg}.table"
    set +x
    dirname $upsdir
}


# configure WCT source for building against UPS externals.
wcdo-ups-wct-configure-source-help () {
    cat 1>&2 <<EOF
Configure WCT source using UPS environment.

  wcdo-ups-wct-configure

You must first set up a UPS environment which includes all externals
needed by WCT.  There are two recomended ways to do this:

1) No installation: use "ups list -aK+ wirecell" and pick a recent
release to "setup".

2) With install: use wcdo-ups-declare to make a UPS product in which
to install and then "setup" this.

EOF
}
wcdo-ups-wct-configure-source () {
    if [ -z "$UPS_DIR" ] ; then
        echo "Must first setup UPS"
        return
    fi
    if [ -z "$WIRECELL_VERSION" ] ; then
        echo "Must first 'setup wirecell' to a version declared in the local UPS products area"
        echo 'try: ups list -aK+ wirecell | grep $(ups flavor)'
        echo "try: wcdo-ups-declare wirecell <version> ..."
        return
    fi
    if [ ! -d "$wcdo_ups_products/wirecell/$WIRECELL_VERSION" ] ; then
        echo "Must first 'setup wirecell' to a version declared in the local UPS products area"
        echo "try wcdo-ups-declare wirecell <version> ..."
        return
    fi
    
    goto $wcdo_wct_dev
    if [ ! -f wcb ] ; then
        echo "wcdo-ups-wct-configure-source: no WCT source: $(wcdo_wct_dev)"
        return
    fi

    # no better way?
    if [ -n "$SETUP_CLANG" ] ; then
        export CC=clang
        export CXX=clang++
    fi
    ./wcb configure \
          --with-tbb=no \
          --with-jsoncpp="$JSONCPP_FQ_DIR" \
          --with-jsonnet="$JSONNET_FQ_DIR" \
          --with-eigen-include="$EIGEN_DIR/include/eigen3" \
          --with-root="$ROOTSYS" \
          --with-fftw="$FFTW_FQ_DIR" \
          --with-fftw-include="$FFTW_INC" \
          --with-fftw-lib="$FFTW_LIBRARY" \
          --boost-includes="$BOOST_INC" \
          --boost-libs="$BOOST_LIB" \
          --boost-mt \
          --prefix=$WIRECELL_FQ_DIR || return 1
    echo "wcdo-ups-wct-configure-source:"
    echo -e "\twill install to $WIRECELL_FQ_DIR"
    echo -e "\tleaving you in source directory.\n\ttype 'goback' to return to your previous directory"
    echo -e "\tready to run:\n\t./wcb -p --notests install"

}



# Modify all product_deps files to set a different version for a package.
wcdo-mrb-fsck-ups-help () {
    cat 1>&2 <<EOF 
Fix the version for a package in all ups/products_deps file in all MRB
source packages.

  usage: wcdo-mrb-fsck-ups package version

EOF
}
wcdo-mrb-fsck-ups () {
    local pkg="$1"; shift;
    local ver="$1"; shift

    if [ -z "$SETUP_UPS" ] ; then
        echo "must setup ups first"
        return
    fi

    wcdo-mrb-init
    local base="$MRB_TOP"

    if [ ! -d "$base/srcs/" ] ; then
        echo "wcdo-mrb-fsck-ups: no sources in $base"
        return
    fi

    for pd in $base/srcs/*/ups/product_deps
    do
        echo -e "\t$pd"
        sed  -e "s/^${pkg}\b.*/${pkg}\t${ver}/" -i $pd
    done

    echo "wcdo-mrb-fsck-ups: before running 'mrbsetenv' you must run:"
    echo -e "\tunsetup $pkg"

}    

wcdo-ups-pullproducts-help () {
    cat 1>&2 <<EOF
Download and install UPS products.

usage: wcdo-ups-pullproducts [pullProducts args] 

The arguments are identical to the "pullProducts" script.  This script
will download pullProducts if needed.  

The nominal location for UPS products in a wcdo container is

  /wcdo/lib/ups

Example:

  wcdo-ups-pullproducts /wcdo/lib/ups slf7 larsoft-v07_07_01 s70-e17 prof

EOF
}
wcdo-ups-pullproducts () {
    local ppurl="http://scisoft.fnal.gov/scisoft/bundles/tools/pullProducts"
    if [ -d /cvmfs ] ; then
        echo "Warning: you have /cvmfs are you sure you want to pull UPS products?"
        echo "Hit Ctrl-c to cancel"
        sleep 1
    fi
    if [ "$(readlink -f $1)" != "$(readlink -f $wcdo_ups_products)" ] ; then
        echo "Warning: running pullProducts in unconventional directory: $1"
    fi
    goto "$1"
    if [ ! -f pullProducts ] ; then
        wget "$ppurl"
    fi
    chmod +x pullProducts
    ./pullProducts $@
    goback
}
    


# Initialize an MRB-controlled development area and environment to use it.
wcdo-mrb-init-help () {
    cat 1>&2 <<EOF
Initialize MRB-controlled development area.

usage: wcdo-mrb-init [name [version [quals]]]

If an option is not given then a variable is consulted:

  wcdo_mrb_project_<option>

When successful, this function modifies your environment and leave
your current working directory to be that of the top of the
MRB-controlled development area.

EOF
}
wcdo-mrb-init () {

    if [ -z "$SETUP_UPS" ] ; then
        echo "must setup ups first"
        return
    fi

    local proj="${1:-$wcdo_mrb_project_name}" ; shift
    local pver="${1:-$wcdo_mrb_project_version}" ; shift
    local quals="${1:-$wcdo_mrb_project_quals}" ; shift
    if [ -z "$proj" ] ; then
        echo "Must define wcdo_mrb_* variables in your wcdo project file"
        wcdo-mrb-init-help
        return
    fi
    local quals_="$(echo $quals | tr ':' '_')"

    goto "$wcdo_mrb_dev"

    export MRB_PROJECT="$proj"
    setup mrb
    setup "$proj" "$pver" -q "$quals"
    unsetup git
    setup git v2_20_1

    # first time through
    if [ -z "$MRB_TOP" ] ; then
        PS1="[mrb]$PS1"
    fi

    local localsetup="localProducts_${proj}_${pver}_${quals_}/setup"
    if [ ! -f "$localsetup" ] ; then
        mrb newDev
    fi
    source "$localsetup"

    echo "wcdo-mrb-init: "
    echo -e "\tenvironment modified and cd'ed to: $(pwd)"
    echo -e "\ttype 'goback' to return to your original directory"

}

wcdo-mrb-goto-help () {
    cat 1>&2 <<EOF
Change to MRB-controlled development area, initializing if needed.

usage: wcdo-mrb-goto

EOF
}
wcdo-mrb-goto () {
    if [ -z "$MRB_TOP" ] ; then
        wcdo-mrb-init
    fi
    goto $MRB_TOP
}

# Add a source package to an MRB area
wcdo-mrb-add-source-help () {
    cat 1>&2 <<EOF
Add source packages to the MRB-controlled development area

usage: wcdo-mrb-add-source <package> <branch> <commitish>

        <package> the git repo and local directory name, eg "larwirecell"
        <branch> a local branch name, eg "myuser_myfeature"
        <committish> the git commit or version tag or similar to start your local branch"

        hint: "ups list -aK+ <packagename>"
EOF
}
wcdo-mrb-add-source () {
    local pkg="$1"; shift
    local feature="$1"; shift
    local committish="$1"; shift

    if [ -z "$SETUP_UPS" ] ; then
        echo "must setup ups first"
        return
    fi

    if [ -z "$pkg" ] ; then
        wcdo-mrb-add-source-help
        return
    fi

    if [ -z "$MRB_TOP" ] ; then
        wcdo-mrb-init
    fi

    goto "${MRB_SOURCE}"
    if [ -d "$pkg" ] ; then
        echo "wcdo-mrb-add-source: package source \"$pkg\" already exists"
        return
    fi
    mrb g "$pkg"
    cd "$pkg"
    local branch="${feature}"
    if git branch -a | grep "\b$branch\b" > /dev/null ; then
        echo "wcdo-mrb-add-source: starting branch exists, using it: $branch"
    else
        echo "wcdo-mrb-add-source: creating starting branch $branch from $committish"
        git checkout -b "$branch" "$committish"
    fi
    popd > /dev/null 2>&1
}

