#!/bin/sh
#
# This file is released under the terms of the Artistic License.
# Please see the file LICENSE, included in this package, for details.
#
# Copyright The DBT-2 Authors
#

while getopts "o:" opt; do
	case ${opt} in
	o)
		OUTPUT_DIR=${OPTARG}
		mkdir -p "$OUTPUT_DIR"
		;;
	\?)
		exit 1
		;;
	esac
done

if [ "${OUTPUT_DIR}" = "" ]; then
	OUTPUT_DIR="."
fi

echo "$(uname -o) $(uname -m) $(uname -r)" > "${OUTPUT_DIR}/uname.txt"

# sysctl is often in a sbin directory that is not on unprivileged PATHs.
if command -v sysctl > /dev/null 2>&1; then
	SYSCTL="sysctl"
elif [ -x "/sbin/sysctl" ]; then
	SYSCTL="/sbin/sysctl"
elif [ -x "/usr/sbin/sysctl" ]; then
	SYSCTL="/usr/sbin/sysctl"
else
	SYSCTL=""
fi

# I don't think capturing error messages is important here.
OS=$(uname -s)
if [ ! "${SYSCTL}" = "" ]; then
	if [ "${OS}" = "Linux" ] || [ "${OS}" = "SunOS" ]; then
		${SYSCTL} -a 2> /dev/null | sort > "${OUTPUT_DIR}/proc.txt"
	else
		echo "Unknown OS: ${OS}"
	fi
fi
