#!/bin/sh
################################################################################
# A script to ease the generation of NUT device dumps for NUT Devices Dumps Library
################################################################################
# Authors:
#   Copyright (C) 2015 - 2017 by Arnaud Quette <ArnaudQuette@Eaton.com>
#   Copyright (C) 2020 - 2023 by Jim Klimov <jimklimov+nut@gmail.com>
# License: GPL v2+
################################################################################
# FIXME:
#  - check if a previous report exists, and increase report number
#  - we currently use the .dev format ; but also consider the NDS format
#     https://www.networkupstools.org/ddl/
################################################################################

strUsage="Usage: $0 <device_name>"

# Check command line parameter (<device_name>)
if [ -z "$1" ]; then
	echo "$strUsage"
	exit
else
	DDL_DEVICE_NAME="$1"
fi

# Test communication with the device
upscResult="`upsc "${DDL_DEVICE_NAME}" 2> /dev/null`"
if [ $? -gt 0 ]; then
	echo "Can't communicate with ${DDL_DEVICE_NAME}" >&2
	exit
fi

# Collect more information
dumpDate="`date -R`"
upsrwResult="`upsrw "${DDL_DEVICE_NAME}" 2> /dev/null`"
upscmdResult="`upscmd -l "${DDL_DEVICE_NAME}" 2> /dev/null`"

# Build the filename
# <manufacturer>__<model>__<driver-name>__<nut-version>__<report-number>.<extension>
# Process the Manufacturer name
RAW_DDL_MFR="`upsc "${DDL_DEVICE_NAME}" device.mfr 2>/dev/null`"
if [ "${RAW_DDL_MFR}" = "EATON" ]; then
	RAW_DDL_MFR="Eaton"
fi
# Replace spaces with underscores
DDL_MFR="`echo "${RAW_DDL_MFR}" | sed s/\ /_/g`"
# Process the Model name
# Replace spaces with underscores
RAW_DDL_MODEL="`upsc "${DDL_DEVICE_NAME}" device.model 2>/dev/null`"
DDL_MODEL="`echo "${RAW_DDL_MODEL}" | sed s/\ /_/g`"
# Process the driver name and NUT version
DDL_DRIVER_NAME="`upsc "${DDL_DEVICE_NAME}" driver.name 2>/dev/null`"
DDL_NUT_VERSION="`upsc "${DDL_DEVICE_NAME}" driver.version 2>/dev/null`"
# TODO: check if a similar file exists in nut-ddl repo, to update Report nb
DDL_REPORT_NUMBER="01"
DDL_FILENAME="${DDL_MFR}__${DDL_MODEL}__${DDL_DRIVER_NAME}__${DDL_NUT_VERSION}__${DDL_REPORT_NUMBER}.dev"

# Dump device data into the file
(
	echo "# Please add if relevant: DEVICE:URL:REPORT: <GitHub issue/PR or Mailing list archive entry URL>"
	echo "# Please add if relevant: DEVICE:URL:VENDOR: <Vendor/Manufacturer/Product page URL>"
	echo ""
	echo "# Please add comments for humans here and perhaps raise concerns about the data points,"
	echo "# see https://networkupstools.org/ddl/#devseq-files for comment-tag syntax"
	echo "# strip sensitive data (passwords, SNMP community, serial number, IP address, host name...)"
	echo "# and post as a pull request to https://github.com/networkupstools/nut-ddl/"
	echo "# DEVICE:COMMENT:"
	echo "# Device dump generated by $0 on ${dumpDate}"
	echo "# DEVICE:EOC"
	echo ""
	echo "# :; upsc ${DDL_DEVICE_NAME}"
	echo "${upscResult}"
	echo ""
	echo "# DEVICE:COMMENT-BLOCK:FIXME:UPSRW: ${DDL_DEVICE_NAME}"
	echo "${upsrwResult}" | sed -e 's/^/# /' -e 's/^# $/#/'
	echo "# DEVICE:EOC"
	echo ""
	echo "# DEVICE:COMMENT-BLOCK:FIXME:UPSCMD: ${DDL_DEVICE_NAME}"
	echo "${upscmdResult}" | sed -e 's/^/# /' -e 's/^# $/#/'
	echo "# DEVICE:EOC"
	echo ""
) > ${DDL_FILENAME}

echo "${DDL_FILENAME} generated using ${DDL_DEVICE_NAME} on ${dumpDate}"
echo "Please revise and post as a pull request to https://github.com/networkupstools/nut-ddl/"
