#!/usr/bin/env bash
# get-computer-name -- Extract the computer name out of a mounted Windows partition
#
# Depends on `reged` from the 'chntpw' package.

. lib.bash || exit

for root; do
	root="${root%/}"

	# Case-insensitive search
	for name in 'Windows' 'System32' 'config' 'SYSTEM'; do
		debug "lookup '$name' in '$root'"
		next=$(find "$root" -mindepth 1 -maxdepth 1 -iname "$name") &&
		[[ $next ]] ||
			die "path '$root/$name' not found"
		root=$next
	done

	[[ -f $root ]] || die "hive '$root' not a regular file"

	echo "reading '$root'"

	reged -x "$root" \
		'HKEY_LOCAL_MACHINE\SYSTEM' \
		'\ControlSet001\Control\ComputerName\ComputerName' \
		/dev/stdout |&
	grep -i '"ComputerName"'
done
