#!/usr/bin/bash

fetch () {
    local src=$1 dst=$2
    local tmp=$2.tmp
    local ret

    if [[ ! -d $(dirname "$dst") ]]; then
        echo >&2 "Destination directory does not exist"
        return 1
    fi
    rm -f "$tmp"
    wget "$src" -q --content-on-error -O "$tmp"; ret=$?
    if [[ $ret != 0 ]]; then
        echo >&2 "Error fetching $src"
        if [[ -s $tmp ]]; then
            echo >&2 "File contents:"
            cat >&2 "$tmp"
        fi
        return 1
    else
        mv -f "$tmp" "$dst"
    fi
}

die_with_usage () {
    echo >&2 "Usage: $(basename "$0") --cache|--origin"
    echo >&2 "   Or: $(basename "$0") stash-cache|stash-cache-auth|stash-origin|stash-origin-auth"
    echo >&2 "Environment variables used:"
    echo >&2 "CACHE_FQDN        FQDN used for cache authfile query (default `hostname -f`)"
    echo >&2 "ORIGIN_FQDN       FQDN used for origin authfile query (default `hostname -f`)"
    echo >&2 "TOPOLOGY          Topology server to get the data from (default https://topology.opensciencegrid.org)"
    exit 2
}

if [[ $# -ne 1 ]]; then
    die_with_usage
fi

TOPOLOGY=${TOPOLOGY:-https://topology.opensciencegrid.org}
CACHE_FQDN=${CACHE_FQDN:-$(hostname -f)}
ORIGIN_FQDN=${ORIGIN_FQDN:-$(hostname -f)}

DESTDIR=${DESTDIR:-/run}


prepend_local_additions () {
    local base="$1"
    local tmp="${base}.$$"
    local ret=0
    if [[ -s $base && -f ${base}.local ]]; then
        echo -e "\n# The following lines are from $(basename "${base}.local"):" > "$tmp"  && \
            cat "${base}.local" >> "$tmp"  && \
            echo -e "\n# The following lines are from OSG Topology:" >> "$tmp"  && \
            cat "${base}" >> "$tmp"  && \
            mv -f "$tmp" "$base"
        ret=$?
    fi
    rm -f "$tmp"
    return $ret
}


append_local_additions () {
    local base="$1"
    if [[ -s $base && -f ${base}.local ]]; then
        echo -e "\n# The following lines are from $(basename "${base}.local"):" >> "$base"
        cat "${base}.local" >> "$base"
    fi
}


fetch_cache_data () {
    mkdir -p "$DESTDIR/stash-cache"
    fetch "${TOPOLOGY}/cache/Authfile-public?fqdn=${CACHE_FQDN}" "$DESTDIR/stash-cache/Authfile" && \
        append_local_additions "$DESTDIR/stash-cache/Authfile"
}


fetch_cache_auth_data () {
    mkdir -p "$DESTDIR/stash-cache-auth"
    local ret=0
    fetch "${TOPOLOGY}/cache/Authfile?fqdn=${CACHE_FQDN}" "$DESTDIR/stash-cache-auth/Authfile" && \
        append_local_additions "$DESTDIR/stash-cache-auth/Authfile"
    ret=$(( $ret | $? ))
    fetch "${TOPOLOGY}/cache/scitokens.conf?fqdn=${CACHE_FQDN}" "$DESTDIR/stash-cache-auth/scitokens.conf" && \
        append_local_additions "$DESTDIR/stash-cache-auth/scitokens.conf"
    ret=$(( $ret | $? ))
    fetch "${TOPOLOGY}/cache/grid-mapfile?fqdn=${CACHE_FQDN}" "$DESTDIR/stash-cache-auth/grid-mapfile" && \
        prepend_local_additions "$DESTDIR/stash-cache-auth/grid-mapfile"
    ret=$(( $ret | $? ))
    return $ret
}


fetch_origin_data () {
    mkdir -p "$DESTDIR/stash-origin"
    fetch "${TOPOLOGY}/origin/Authfile-public?fqdn=${ORIGIN_FQDN}" "$DESTDIR/stash-origin/Authfile" && \
        append_local_additions "$DESTDIR/stash-origin/Authfile"
}


fetch_origin_auth_data () {
    mkdir -p "$DESTDIR/stash-origin-auth"
    local ret=0
    fetch "${TOPOLOGY}/origin/Authfile?fqdn=${ORIGIN_FQDN}" "$DESTDIR/stash-origin-auth/Authfile" && \
        append_local_additions "$DESTDIR/stash-origin-auth/Authfile"
    ret=$(( $ret | $? ))
    fetch "${TOPOLOGY}/origin/scitokens.conf?fqdn=${ORIGIN_FQDN}" "$DESTDIR/stash-origin-auth/scitokens.conf" && \
        append_local_additions "$DESTDIR/stash-origin-auth/scitokens.conf"
    ret=$(( $ret | $? ))
    fetch "${TOPOLOGY}/origin/grid-mapfile?fqdn=${ORIGIN_FQDN}" "$DESTDIR/stash-origin-auth/grid-mapfile" && \
        prepend_local_additions "$DESTDIR/stash-origin-auth/grid-mapfile"
    ret=$(( $ret | $? ))
    return $ret
}


case $1 in
    --cache)
        ret=0
        fetch_cache_data
        ret=$(( $ret | $? ))
        fetch_cache_auth_data
        ret=$(( $ret | $? ))
        ;;
    --origin)
        ret=0
        fetch_origin_data
        ret=$(( $ret | $? ))
        fetch_origin_auth_data
        ret=$(( $ret | $? ))
        ;;
    stash-cache)
        fetch_cache_data
        ret=$?
        ;;
    stash-cache-auth)
        fetch_cache_auth_data
        ret=$?
        ;;
    stash-origin)
        fetch_origin_data
        ret=$?
        ;;
    stash-origin-auth)
        fetch_origin_auth_data
        ret=$?
        ;;
    *)
        die_with_usage
esac
exit $ret
