#!/bin/vcli -f
# Copyright (c) 2019-2020, AT&T Intellectual Property. All rights reserved.
# Copyright (c) 2014-2016 by Brocade Communications Systems, Inc.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-only
#
# implement "show tech-support"
# usage: tech-support [brief] [ save | save-uncompressed [ <filename> ] ]

# shellcheck source=../functions/tech-support.functions
source "$vyatta_datadir/vyatta-op/functions/tech-support.functions"

# ts_generate runs in a sub-shell
ts_execute() (
	export PATH=/sbin:/usr/sbin:"$PATH"

	header "$OUTPUT_HEADER"
	header "Current Time"
	do_cmd date
	# This section executes the scripts in the SCRIPTS_DIR
	#
	run-parts --arg="$ARG" "$SCRIPTS_DIR"
	header "END OF TECH-SUPPORT FILE"
)


ts_generate() {
	if [[ $COMPRESS -eq 0 && $ENCRYPT -eq 0 ]]; then
		# case 1: uncompressed unencrypted save
		ts_execute 2>&1 || \
			ts_err 1 "Cannot generate tech support file $TS_FILE_NAME"
	elif [[ $COMPRESS -eq 0 && $ENCRYPT -eq 1 ]]; then
		# case 2: uncompressed encrypted save
		ts_execute |& encrypt_stdin "$FILEPASS" || \
			ts_err 1 "Cannot password protect $TS_FILE_NAME"
	elif [[ $COMPRESS -eq 1 && $ENCRYPT -eq 0 ]]; then
		# case 3: compressed unencrypted save
		ts_execute |& gzip -c || \
			ts_err 1 "Failed to compress tech support file $TS_FILE_NAME"
	elif [[ $COMPRESS -eq 1 && $ENCRYPT -eq 1 ]]; then
		# case 4: compressed encrypted save
		ts_execute |& gzip -c | encrypt_stdin "$FILEPASS" || \
			ts_err 1 "Cannot password protect $TS_FILE_NAME"
	fi
}

#
# Process command line parameters
#
SCRIPTS_DIR="$TECH_SUPPORT_SCRIPTS_DIR"
OUTPUT_HEADER="Show Tech-Support"
COMPRESS=""
BRIEF=false
while [ -n "$1" ]; do
	if [ "$1" == "brief" ]; then
		SCRIPTS_DIR="$TECH_SUPPORT_BRIEF_SCRIPTS_DIR"
		OUTPUT_HEADER="Show Tech-Support Brief"
		BRIEF=true
	elif [ "$1" == "save" ]; then
		COMPRESS="1"
		if [ -n "$2" ] && [ "$2" != "password" ]; then
			DEST="$2"
			shift
		fi
	elif [ "$1" == "save-uncompressed" ]; then
		COMPRESS="0"
		if [ -n "$2" ] && [ "$2" != "password" ]; then
			DEST="$2"
			shift
		fi
	elif [ "$1" == "archive" ]; then
		ARG="$1"
	fi
	shift
done

cat <<EOF
Note: Support output may contain plaintext secrets such as passphrases and keys
      Please audit/redact the output to your requirements before transmission

EOF

if [ "$BRIEF" == "false" ]; then
	if [ "$VYATTA_TECH_SUPPORT_EXCL_CMD_HIST" != "1" ]; then
		cat <<EOF
      Shell command history files will be collected in this report
      The "exclude-command-history" option can be used to disable this collection

EOF
	else
		cat <<EOF
      Shell command history files will not be collected in this report

EOF
	fi
fi

create_scratch_dir || exit 1
if [[ -z $COMPRESS ]]; then
	ts_execute 2>&1 || \
		ts_err 1 "Cannot generate tech support output"
else
	outfile="$(ts_target_name "${DEST}")" || \
		ts_err 1 "Invalid path name ${DEST}"

	template="${outfile}.XXXXXX.txt"
	[[ $COMPRESS -eq 1 ]] && template=${template}.gz
	[[ $ENCRYPT -eq 1 ]] && template=${template}.gpg

	ts_save_file "${DEST}" "${template}" ts_generate || exit 1
fi
echo "Done"
