#!/usr/bin/env bash

# Very basic random password generator: insecure, but lightweight and
# dependency-free.
# WARNING: Unsuitable for high-security applications;
# opt for more sophisticated solutions.

PGM=$(basename "$0")

usage() { echo "usage: $PGM [ <length> | -<length> ]" >&2; }

for o; do
	case $o in
	-h | --help)
		usage
		exit
		;;
	*)
		length=$(echo "${1:-16}" | sed "s/^-*//" | egrep "^[0-9]+$")
		[ -z "$length" ] && usage && exit 1
		;;
	esac
done

length=${length:-16}

echo $(LC_ALL=C tr -dc "[:alnum:]" </dev/urandom | head -c$length)
