#!/bin/bash
#
# Copyright (C) 2015-2016 Red Hat, Inc.
#
# This file is part of atomic-devmode.
#
# atomic-devmode is free software: you can redistribute it
# and/or modify it under the terms of the GNU Lesser General
# Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# atomic-devmode is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Lesser General Public License for
# more details.
#
# You should have received a copy of the GNU Lesser General
# Public License along with atomic-devmode. If not, see
# <http://www.gnu.org/licenses/>.

set -euo pipefail

# This is a thin wrapper around pwmake which ensures that the password generated
# only has friendly chars. We do this the "dumb" way: just keep re-running
# pwmake until every char in the pw is friendly.

# A nice set adapted from http://stackoverflow.com/questions/55556
FRIENDLY_CHARS="ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789@#\$%^&*()-_=+[]{}:;\"',<.>/?:\\"

main() {
	pw=$(pwmake "$@")
	until good_pw "$pw"; do
		pw=$(pwmake "$@")
	done
	echo "$pw"
}

good_pw() {
	pw="$1"
	for (( i = 0; i < ${#pw}; i++ )); do
		if [[ "$FRIENDLY_CHARS" != *"${pw:$i:1}"* ]]; then
			return 1
		fi
	done
}

main "$@"
