#!/bin/bash
source "${0%/*}/../lib/zrancid.env" || exit 2

PROGNAME=${0##*/}
OIFS=$IFS
shopt -s nullglob
set -o pipefail

function fatal() { echo "FATAL: $PROGNAME: $*" >&2; exit 2; }
function error() { echo "ERROR: $PROGNAME: $*" >&2; }
function info() { echo "INFO: $PROGNAME: $*" >&2; }
function warning() { echo "WARNING: $PROGNAME: $*" >&2; }

[[ -z $ZRANCID_ETC_DIR ]] && fatal 'Bad env! ZRANCID_ETC_DIR not set'
[[ -z $TMPDIR ]] && fatal 'Bad env! rancid TMPDIR not set'

GROUP=${ZRANCID_GROUP:-default} # used by zrancid-collect only

# instance specific
OUR_TMP_DIR="$TMPDIR/$PROGNAME.$RANDOM.$RANDOM"
trap '[[ -d $OUR_TMP_DIR && -z $KEEP_OUR_TMP_DIR ]] && {
            rm -f "$OUR_TMP_DIR"/*
            rmdir "$OUR_TMP_DIR"
}' EXIT

function exit_usage() {
    local status=${1:-0}
    [[ $status != 0 ]] && exec >&2
    echo "Usage: ${PROGNAME%-*}-<COMMAND> [OPTION...] ARG...
RANCiD helper program

Commands:
  run device-regex-pattern
        Call rancid-run for the matching devices. This is the classic
        execution of RANCiD, with lock, config save, git update, etc.
        Special pattern @all is an alias to select all devices, use it
        to mimic the rancid-run command without -r argument otherwise
        you will get a commit per changed device.

  debug device-regex-pattern
        Call rancid script for the matching devices. It produces
        <device>.raw and <device>.new files in the current directory.
        The .new file corresponds to the final config, produced by
        rancid parsers from the .raw file.

  test device-regex-pattern
        Same as debug but dump the .new file on stdout. Both .new and
        .raw files get cleaned-up on exit, unless -K, --keep is set.

  replay device raw-file
        Call rancid script with a .raw file in order to regenerate the
        .new file. The device will not be contacted. This is useful to
        debug when writing RANCiD parsers, ie: the process that creates
        a final config (the .new file) from a .raw file.

  login device
        Call plogin. It opens a shell to the device. If a pattern is
        given for the device argument, the first matching device is
        used as target.

  collect device-name
        Trigger a real RANCiD collection for exactly this device (the
        name is matched exactly, not as a pattern: unlike the other
        commands here, a dot in device-name is NOT a regex wildcard)
        and print the resulting config file on stdout, nothing else.
        Meant to be called from an SSH forced command so yana can
        request an on-demand collection. Exit status: 0 collected; 3
        device not registered in RANCiD; 4 rancid-run ran but did not
        refresh the config file (unreachable device, parser failure,
        or the group's rancid-run lock held by another run).

  exec device-regex-pattern commands
        Call plogin to connect to the matching devices and executes the
        given commands. Multiple matching devices are processed in
        sequence. Note: commands must be given as a single argument and
        multiple commands must be separated by semi-colon \";\".

  cloginrc device-regex-pattern
        Dump generated cloginrc file for the matching devices. Use
        option -P, --passwords to display password, otherwise they get
        replaced by string <rest-of-line-removed>

Options:
  -P, --passwords   Do not hide password in cloginrc file
  -K, --keep        Keep tmpdir on exit
  -h, --help        Display this usage help
"
    exit "$status"
}

function prun() {
    local cmdstr
    if (( BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4) )); then
        cmdstr=${*@Q}
    else
        # fallback
        cmdstr=$(q="'"; set -- "${@/#/$q}"; set -- "${@/%/$q}"; echo "$*")
    fi
    info "EXEC: $cmdstr"
    ${DO_EXEC:+exec} "$@"
}

function generate_cloginrc() {
    local i
    {   for i in "$@"; do
            if [[ ! -f $ZRANCID_ETC_DIR/hosts/$i/cloginrc ]]; then
                warning "No cloginrc template for device $i"
                continue
            fi
            zrancid-template2cloginrc "$i" "$ZRANCID_ETC_DIR/hosts/$i"/cloginrc{,.*}
        done
        echo 'add timeout * 10'
        echo 'add sshcmd * zrancid-ssh'
        echo 'add telnetcmd * zrancid-telnet'
    } >> "$CLOGINRC"
}

function zrancid-run() {
    local ret=0
    local cmd=(
        rancid-run
        -f "$ZRANCID_ETC_DIR/rancid.conf"
        -c "Updates from RANCiD run on $(date +%Y-%m-%dT%H:%M:%S%:z)"
    )
    if [[ ${ARGS[1]} == '@all' ]]; then
        if ! prun "${cmd[@]}"; then
            error "${ARGS[1]}: rancid-run failed"
            ret=1
        fi
    else
        for (( i = 0; i < ${#DEVICES[@]}; i++ )); do
            info "DEVICE: ${DEVICES[i]}"
            if ! prun "${cmd[@]}" -r "${DEVICES[i]}"; then
                error "${DEVICES[i]}: rancid-run failed"
                ret=1
            fi
        done
    fi
    return "$ret"
}

function _zrancid_debug_device() {
    local device=$1 type=$2
    local script
    info "DEVICE: $device"
    script=$(prun zrancid-type2script "$type")
    if [[ $? != 0 || -z $script ]]; then
        error "Failed to get rancid script command for device $device"
        return 1
    fi
    (   set -f
        set -- $script # yup...
        prun "$@" -d "$device" )
}

function zrancid-debug() {
    local i
    for (( i = 0; i < ${#DEVICES[@]}; i++ )); do
        _zrancid_debug_device "${DEVICES[i]}" "${TYPES[i]}"
    done
}

function zrancid-test() {( # in subshell
    cd "$OUR_TMP_DIR" || fatal "Something is wrong with our tmpdir"
    for (( i = 0; i < ${#DEVICES[@]}; i++ )); do
        _zrancid_debug_device "${DEVICES[i]}" "${TYPES[i]}" || continue
        if [[ -f "${DEVICES[i]}.new" ]]; then
            cat "${DEVICES[i]}.new"
        else
            error "${DEVICES[i]}.new NOT FOUND!"
        fi
    done
)}

function zrancid-replay() {
    [[ -n ${ARGS[2]} ]] || exit_usage 1
    local script=$(prun zrancid-type2script "${TYPES[0]}")
    if [[ $? != 0 || -z $script ]]; then
        fatal "Failed to get rancid script command for device ${DEVICE[0]}"
    fi
    (   set -f
        set -- $script # yup...
        prun "$@" -d -f "${ARGS[2]}" "${DEVICES[0]}" )
}

function zrancid-login() {
    prun plogin -z "${TYPES[0]}" "${DEVICES[0]}"
}

function zrancid-collect() {
    local device=${DEVICES[0]}
    local file="$BASEDIR/$GROUP/configs/$device"
    local mtime_before= mtime_after=
    local cmd=(
        rancid-run
        -f "$ZRANCID_ETC_DIR/rancid.conf"
        -c "On-demand collection via yana on $(date +%Y-%m-%dT%H:%M:%S%:z)"
    )

    [[ -e $file ]] && mtime_before=$(stat -c '%Y' "$file")

    info "DEVICE: $device"
    # stdout carries the config only: rancid-run output goes to stderr
    prun "${cmd[@]}" -r "$device" >&2 || error "$device: rancid-run failed"

    if [[ ! -e $file ]]; then
        error "$device: config file not found after collection: $file"
        return 4
    fi

    mtime_after=$(stat -c '%Y' "$file")
    if [[ -n $mtime_before && $mtime_before == "$mtime_after" ]]; then
        error "$device: config file not refreshed (unreachable device, parser failure, or the group lock held by another run)"
        return 4
    fi

    cat "$file"
}

function zrancid-exec() {
    [[ -n ${ARGS[2]} ]] || exit_usage 1
    for (( i = 0; i < ${#DEVICES[@]}; i++ )); do
        info "DEVICE: ${DEVICES[i]}"
        prun plogin -z "${TYPES[i]}" "${DEVICES[i]}" ${ARGS[2]:+-c "${ARGS[2]}"}
    done
}

function zrancid-cloginrc() {
    while read -r; do
        re='^(add[[:space:]]+password[[:space:]]+[^[:space:]]+[[:space:]]+)'
        if [[ -n $HIDE_PASSWORDS && $REPLY =~ $re ]]; then
            REPLY="${BASH_REMATCH[1]}<rest-of-line-removed>"
        fi
        echo "$REPLY"
    done < "$CLOGINRC"
}

HIDE_PASSWORDS=1
KEEP_OUR_TMP_DIR=
ARGS=()

shopt -s extglob
while (( $# > 0 )); do
    case "$1" in
        -P|--passwords) HIDE_PASSWORDS= ;;
        -K|--keep) KEEP_OUR_TMP_DIR=1 ;;
        -h|--help) exit_usage 0 ;;
        -+([PKh]))
            for (( i = 1; i < ${#1}; i++ )); do
                case "${1:i:1}" in
                    P) HIDE_PASSWORDS= ;;
                    K) KEEP_OUR_TMP_DIR=1 ;;
                    h) exit_usage 0 ;;
                esac
            done
            ;;
       --) shift; ARGS+=( "$@" ); break ;;
       -*) exit_usage 1 ;;
        *) ARGS+=( "$1" ) ;;
    esac
    shift
done
shopt -u extglob

# XXX extract command from program name for consistency with the other tools
ARGS=( "${PROGNAME##*-}" "${ARGS[@]}" )

declare -F -f "zrancid-${ARGS[0]}" > /dev/null || exit_usage 1
[[ -n ${ARGS[1]} ]] || exit_usage 1

DEVICES=()
TYPES=()
LS_PATTERN=${ARGS[1]}
if [[ ${ARGS[0]} == collect ]]; then
    # collect requires an EXACT device name, not a pattern: zrancid-ls
    # applies an unanchored regex ("[[ $device =~ $i ]]", see
    # bin/zrancid-ls), so eg. name "sw1.demo" would also match
    # "sw1xdemo". Anchor the pattern and escape the only regex
    # metacharacter allowed in the name charset (the dot).
    LS_PATTERN="^${ARGS[1]//./\\.}\$"
fi
while IFS=$'\x16' read -r device x y type; do
    DEVICES+=( "$device" )
    TYPES+=( "$type" )
done < <(zrancid-ls -s $'\x16' -v -- "$LS_PATTERN")

if [[ ${ARGS[0]} == collect ]]; then
    # belt and suspenders: the anchored pattern above should already
    # guarantee a single exact match, double check it really is one.
    if (( ${#DEVICES[@]} != 1 )) || [[ ${DEVICES[0]} != "${ARGS[1]}" ]]; then
        error "${ARGS[1]}: device not registered in RANCiD"
        exit 3
    fi
else
    (( ${#DEVICES[@]} > 0 )) || fatal 'Device(s) not found!'
fi

# make sure we have a tmpdir
mkdir -p "$OUR_TMP_DIR" || fatal 'Failed to create tmpdir'

# build cloginrc
export CLOGINRC="$OUR_TMP_DIR/cloginrc"
info "ENV: $(declare -p CLOGINRC)"
generate_cloginrc "${DEVICES[@]}"

# run command
"zrancid-${ARGS[0]}"
