#!/bin/sh
# Copyright (C) 2007-2015, Stefan Lippers-Hollmann <s.l-h@gmx.de>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 of the
# License.
#
# This program 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 General Public License for more details.

# override tool behaviour through distro-defaults
FLL_LIVE_USER="siduction"

[ -r /etc/default/distro ] && . /etc/default/distro

bailout()
{
        ERROR_LEVEL="${1}"
        REASON="${2}"

	printf "ERROR $ERROR_LEVEL:\t$REASON\n\n" >&2

	exit "$ERROR_LEVEL"
}

print_copyright()
{
        cat \
<<EOF

Copyright (C) 2007-2013 Stefan Lippers-Hollmann <s.l-h@gmx.de>

F.U.L.L.S.T.O.R.Y Project Homepage:
https://github.com/fullstory

F.U.L.L.S.T.O.R.Y Subversion Archive:
svn://svn.berlios.de/fullstory/trunk
http://svn.berlios.de/svnroot/repos/fullstory
http://svn.berlios.de/viewcvs/fullstory (viewcvs)
http://svn.berlios.de/wsvn/fullstory (websvn)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of the
License only.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this package; if not, see <http://www.gnu.org/licenses>

On Debian GNU/Linux systems, the text of the GPL license can be
found in /usr/share/common-licenses/GPL-2.

EOF
}

print_help()
{
        cat \
<<EOF

Usage: $(basename $0) [options]
  $(basename $0) is frontend to set user passwords, primarily targeted at 
  live CD usages. Unless explicitly overridden with -u|--user, the password
  dialogues for the executing user and root will be started.

Options:
  -C|--copyright	copyright information
  -h|--help		information about using this program
  -r|--no-root		don't change the root password.
  -u|--user		specify username for setting a password

EOF
}

change_passwd()
{
	clear
	printf "Change system password for \"${1}\":\n"

	passwd "${1}" || bailout 4 "failed to set password for system account ${USERNAME}."
}

change_smbpasswd()
{
	clear
	printf "Change/ add samba password for \"${1}\":\n"

	smbpasswd -x "${1}"

	smbpasswd -a "${1}" || bailout 5 "Failed to set samba password for ${USERNAME}."
}

ARGS=$(	getopt --name "$(basename $0)" \
	--options Chru: \
	--long copyright,help,no-root,user:,dpkg-parsechangelog \
	-- $@ )

[ "$?" -ne 0 ] && bailout 1 "getopt failed, terminating."

eval set -- "$ARGS"

NO_ROOT=""
while [ "$#" -gt 1 ]; do
	case $1 in
		-C|--copyright)
			print_copyright
			exit 0
			;;
		-h|--help)
			print_help
			exit 0
			;;
		-r|--no-root)
			NO_ROOT="yes"
			;;
		-u|--user)
			shift
			USERNAME="$1"
			;;
		--dpkg-parsechangelog)
			# ugly hack to make help2man happy
			if [ -x /usr/bin/dpkg-parsechangelog ] && [ -f debian/changelog ]; then
				printf "$(basename $0) $(dpkg-parsechangelog | awk '/^Version\:/{print $2}')\n"
			else
				bailout 4 "not run within debian buildd"
			fi
			exit 0
			;;
		*)
			printf "$(basename $0): invalid command line option\n" >&2
			print_help
			exit 2
			;;
	esac
	shift
done

# determine username
if [ -z "$USERNAME" ]; then
	if [ "$(id -u)" -ne 0 ]; then
		USERNAME="$(id -un)"
	else
		USERNAME="$FLL_LIVE_USER"
	fi
fi

if ! getent passwd "$USERNAME" >/dev/null 2>&1; then
	bailout 2 "$USERNAME is not a valid system account."
fi

# run me as root
if [ "$(id -u)" -ne 0 ]; then
	[ -x "$(which su-to-root)" ] && exec su-to-root -X -c "$0" "$@"
	bailout 3 "$0 needs root capabilities, please start it as root."
fi

# change user password
change_passwd "$USERNAME"

# add samba user
if [ -x /usr/bin/smbpasswd ] && pidof smbd >/dev/null 2>&1; then
	change_smbpasswd "$USERNAME"
fi

[ ! "$NO_ROOT" = "yes" ] && change_passwd root

