#! /bin/bash
#
# ps1test
# Tests your bash PS1
# Copyright (C) 2020 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#

main() {
	. ~/.profile
	. ~/.bashrc
	printf "Default:\n"
	printps1						# Test a default ps1
	false || printps1					# Test after command failure
	PWD=~/Documents printps1				# Test in a subdir of ~
	PWD=/tmp printps1					# Test in a world-writeable dir
	PWD=/usr/local printps1					# Test in unwriteable dir
	PWD=/dir/that/doesnt/exist printps1			# Test in dir that doesn't exist
	printf "\nDefault, different user:\n"
	USER=notme printps1					# Test as another user
	printf "\nSame user, different machine:\n"
	SSH_CLIENT=notme HOSTNAME=notme printps1		# Test over fake SSH
	printf "\nForeign machine, foreign user:\n"
	USER=notme SSH_CLIENT=notme HOSTNAME=notme printps1	# Test both of the above
}

hostname() {
	# We have this little stub here to intercept calls to hostname
	echo "$HOSTNAME"
}

printps1() {
	# Print the PS1, with some substitutions
	"$PROMPT_COMMAND" > /dev/null 2>&1
	PS1="${PS1//\\[/}"
	PS1="${PS1//\\]/}"
	PS1="${PS1//\\w/$PWD}"
	PS1="${PS1//$HOME/\~}"
	printf "\t$PS1\n"
}


main "$@"