#!/bin/bash
#
# PWVault (20141211), was written by Grant Taylor and
# meant to be a wrapper script to manage passwords
# encrypted with a functional gpg configuration.
#

checkPWVault () {
	if [ ! -d ${PWVAULT} ]; then
		echo "Creating ${PWVAULT} directory."
		mkdir -p ${PWVAULT}
	fi
	if [ ! -d ${PWVAULT}/.all ]; then
		echo "Creating ${PWVAULT}/.all directory."
		mkdir -p ${PWVAULT}/.all
	fi
}

createPWVaultrc () {
	echo "Creating ~/.pwvaultrc"
	echo '# PWVaultrc' > ${HOME}/.pwvaultrc
	echo '# Values below will override environment variables' >> ${HOME}/.pwvaultrc
	echo '# PWGEN="pwgen"' >> ${HOME}/.pwvaultrc
	echo '# PWVAULT="${HOME}/.pwvault"' >> ${HOME}/.pwvaultrc
	echo '# TREE="tree"' >> ${HOME}/.pwvaultrc
}

delete () {
	checkPWVault
	echo rm "${PWVAULT}/${1}.gpg" "${PWVAULT}/.all/$(basename ${1}).gpg"
	if ( [ -e "${PWVAULT}/${1}.gpg" ] && [ -e "${PWVAULT}/.all/$(basename ${1}).gpg" ] ); then
		rm "${PWVAULT}/${1}.gpg" "${PWVAULT}/.all/$(basename ${1}).gpg"
	else
		echo "Error: ${1} doesn't exists."
		exit 1
	fi
}

generate () {
	checkPWVault
	${PWGEN}
}

init () {
	PWGEN=${PWGEN:="pwgen"}
	PWVAULT=${PWVAULT:="${HOME}/.pwvault"}
	TREE=${TREE:="tree"}
	if [ -f ${HOME}/.pwvaultrc ]; then
		source ${HOME}/.pwvaultrc	# PWVault Runtime Config file to override defaults listed above.
	else
		createPWVaultrc
	fi
}

insert () {
	checkPWVault
	if ( [ ! -e "${PWVAULT}/${1}.gpg" ] && [ ! -e "${PWVAULT}/.all/$(basename ${1}).gpg" ] ); then
		mkdir -p "$(dirname "${PWVAULT}/${1}")"
		read PASSWORD
		echo ${PASSWORD} | gpg -e -q -o "${PWVAULT}/${1}.gpg"
		ln -sf "../${1}.gpg" "${PWVAULT}/.all/$(basename ${1}.gpg)"
	else
		echo "Error: ${1} already exists"
		exit 1
	fi
}

list () {
	checkPWVault
	if [ ${1} ]; then
		if [ -d ${PWVAULT}/${1} ]; then
			$TREE ${PWVAULT}/${1} | sed 's/\.gpg$//'
		elif [ -f ${PWVAULT}/${1}.gpg ]; then
			gpg -d -q --batch ${PWVAULT}/${1}.gpg
		fi
	else
		$TREE ${PWVAULT} | sed 's/\.gpg$//'
	fi
}

main () {
	case ${1} in
		delete)				delete ${2} ;;
		g|generate)			generate ;;
		-h|--h|-help|--help|h|help)	syntax ;;	# -h|--h|-help|--help are hidden options.
		i|insert)			insert ${2};;
		l|list)				list ${2} ;;
		"")				list ;;
		r|rename)			rename ${2} ${3} ;;
		u|update)			update ${2} ;;
		v|vault)			vault ${@} ;;
		*)				list ${1} ;;	# Default action
	esac
}

rename () {
	checkPWVault
	if ( [ -e "${PWVAULT}/${1}.gpg" ] && [ -e "${PWVAULT}/.all/$(basename ${1}).gpg" ] && [ ! -e "${PWVAULT}/${2}.gpg" ] && [ ! -e "${PWVAULT}/.all/$(basename ${2}).gpg" ]); then
		mv "${PWVAULT}/${1}.gpg" "${PWVAULT}/${2}.gpg"
		rm "${PWVAULT}/.all/$(basename ${1}).gpg"
		ln -sf "../${2}.gpg" "${PWVAULT}/.all/$(basename ${2}.gpg)"
	else
		echo "Error: ${2} already exists"
		exit 1
	fi
}

syntax () {
	echo -e "usage:  pwvault [v[ault] <path>] [delete|g[enerate]|h[elp]|i[nsert]|l[ist]|r[ename]|u[pdate]]"
	echo -e "	\033[1mdelete\033[0m		\033[1mDelete\033[0m an existing password."
	echo -e "	\033[1mg\033[0m|\033[1mgenerate\033[0m	\033[1mGenerate\033[0m a new password using pwgen."
	echo -e "	\033[1mi\033[0m|\033[1minsert\033[0m	\033[1mInsert\033[0m a new password."
	echo -e "	\033[1ml\033[0m|\033[1mlist\033[0m		\033[1mList\033[0m existing password(s)."
	echo -e "	\033[1mr\033[0m|\033[1mrename\033[0m	\033[1mRename\033[0m an existing password."
	echo -e "	\033[1mh\033[0m|\033[1mhelp\033[0m		Display the \033[1mhelp\033[0m / syntax."
	echo -e "	\033[1mu\033[0m|\033[1mupdate\033[0m	\033[1mUpdate\033[0m an existing password."
	echo -e "	\033[1mv\033[0m|\033[1mvault\033[0m		Specify which pw\033[1mvault\033[0m to use."
}

update () {
	checkPWVault
	if ( [ -e "${PWVAULT}/${1}.gpg" ] && [ -e "${PWVAULT}/.all/$(basename ${1}).gpg" ] ); then
		read PASSWORD
		echo ${PASSWORD} | gpg -e -q --yes -o ${PWVAULT}/${1}.gpg
	else
		echo "Error: ${2} doesn't exists."
		exit 1
	fi
}

vault () {
	PWVAULT=${2}
	shift 2
	main ${@}
}

init
main ${@}
