#!/bin/vcli -f
# Copyright (c) 2017-2021, AT&T Intellectual Property.
# Copyright (c) 2014-2017 by Brocade Communications Systems, Inc.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-only
#
# implement "generate tech-support archive"
# usage: tech-support archive [ <filename> | <scp://> | <ftp://> ]

TS_SCRIPT=$vyatta_bindir/tech-support
TS_FNS=$vyatta_datadir/vyatta-op/functions/tech-support.functions

# TSA_TEMP_ROOT is the directory in which tech support
# archive creates a temporary directory TEMP_DIR to collect various
# tech support files for archiving later. TSA_TEMP_ROOT should be
# protected with at least a sticky bit or should be writable only by root
#
TSA_TEMP_ROOT="/var/tmp"
TEMP_DIR=""


# creates various tech support archive files in current directory
tsa_create_files() {
	echo "Collecting /config ..."
	tar --exclude="*tech-support-archive*" -zcf config.tgz /config >& /dev/null

	echo "Collecting /etc ..."
	tar -zcf etc.tgz /etc >& /dev/null

	echo "Collecting /home ..."
	tar --exclude=.bash_history -zcf home.tgz /home >& /dev/null

	echo "Collecting /var/log ..."
	tar -zcf var-log.tgz /var/log >& /dev/null

	echo "Collecting /root ..."
	tar --exclude=.bash_history -zcf root.tgz /root >& /dev/null

	echo "Collecting /tmp ..."
	tar -zcf tmp.tgz /tmp >& /dev/null

	echo "Collecting core dumps ..."
	tar -zcf core-dump.tgz /var/core /var/lib/systemd/coredump >& /dev/null

	if [ -d /var/crash ]; then
		echo "Collecting kernel crash dumps ..."
		tar -zcf var-crash.tgz /var/crash >& /dev/null
	fi

	echo

	"$TS_SCRIPT" archive save "$(pwd)"/state
	mv state* current-state.gz
}

tsa_mktemp_dir() {
	# set for file creation with permissions set to 640
	local d="$1"
	local t="${2}.XXXXXX"

	[[ -d $d ]] || ts_err 1 "cannot create tech support archive: no such directory ${d}"
	if ! TEMP_DIR="$(mktemp -d --tmpdir="${d}" -t "${t}")"; then
		ts_err 1 "Cannot create archive temporary directory in $d"
	fi
	ts_push_cleanup_dir "$TEMP_DIR"
}

# tsa_populate_dir <directory>
tsa_populate_dir() (
	# Run in a subshell and change directory.
	builtin cd "$1" || ts_err 1 "Cannot change directory to ${1}"
	umask 0137 >& /dev/null
	tsa_create_files || ts_err 1 "Failed to generate archive"
	chgrp "$DEFAULT_GROUP" "$1" >& /dev/null
	chmod 'g+rx' "$1"
)

tsa_encrypt() {
	if [[ $ENCRYPT -eq 1 ]]; then
		if ! encrypt_stdin "$FILEPASS"; then
			echo "Failed to encrypt tech support archive" >&2
			return 1
		fi
	else
		cat
	fi
}

tsa_tar() {
	if ! tar -C "${1}" -czf - . --transform "s|^\.|${2}|"; then
		echo "Failed to generate tech support archive ${3}" >&2
		return 1
	fi
}


# Generate and Dumps the archive to stdout
# tsa_generate <root_dir> <archive_content_dir> [<topdir_name in archive>]
tsa_generate() (
	local dir="$1" # top directory name
	local name="$2" # top directory name

	if ! tsa_tar "$dir" "$name" | tsa_encrypt; then
		return 1
	fi
)

# shellcheck source=../functions/tech-support.functions
source "$TS_FNS"

create_scratch_dir || exit 1
builtin shopt -s -o pipefail

DEST="$1"
OUT="$(ts_target_name "$DEST" archive)" || ts_err 1 "Invalid path name ${DEST}"

FILENAME="$(basename "$OUT")"
TEMPLATE="${OUT}.XXXXXX.tgz"
[[ $ENCRYPT -eq 1 ]] && TEMPLATE="${TEMPLATE}.gpg"

tsa_mktemp_dir "$TSA_TEMP_ROOT" "$FILENAME" || \
	ts_err 1 "Cannot create temporary directory."

[[ -n $TEMP_DIR && -d $TEMP_DIR ]] || \
	ts_err 1 "No tech support temporary directory"

tsa_populate_dir "$TEMP_DIR" || \
	ts_err 1 "Failed create tech support files."

ts_save_file "$DEST" "$TEMPLATE" tsa_generate "$TEMP_DIR" "$FILENAME"
echo "Done"
