#!/bin/bash set -u metadata_file= metadata_temporary= once=0 destination=${XCATDEST:-} request_pid= usage() { printf '%s\n' 'Usage: nextdestiny [ENDPOINT] [--metadata FILE] [--once]' >&2 exit 2 } while (($# > 0)); do case "$1" in --metadata) (($# >= 2)) || usage metadata_file=$2 shift 2 ;; --once) once=1 shift ;; --) shift (($# <= 1)) || usage if (($# == 1)); then destination=$1 fi break ;; -*) usage ;; *) if [[ -z $destination ]]; then destination=$1 fi shift ;; esac done [[ -n $destination ]] || usage source "$(dirname -- "${BASH_SOURCE[0]}")/../lib/xcat/destiny.sh" || exit 1 temporary_dir=$(mktemp -d "${TMPDIR:-/tmp}/nextdestiny.XXXXXX") || exit 1 request_file=$temporary_dir/request.xml response_file=$temporary_dir/response.xml trap cleanup EXIT trap 'terminate 129' HUP trap 'terminate 130' INT trap 'terminate 143' TERM printf '%s\n' \ '' \ 'nextdestiny' \ '300' \ '' >"$request_file" request_destiny() { send_destiny_request || true [[ -s $response_file ]] } if ((once)); then request_destiny \ && { grep -q '' "$response_file" \ || grep -q '' "$response_file"; } \ || exit 1 else request_destiny || true fi destiny=$(extract_element destiny) if [[ -z $destiny ]]; then error=$(extract_element error) [[ -n $error ]] || error='No destiny command received' destiny="error=$error" fi if [[ -n $metadata_file ]]; then write_metadata nextdestiny fi printf '%s\n' "$destiny"