#!/bin/sh -e
#
# $Id: hasher-useradd,v 1.5 2005/08/19 00:17:17 ldv Exp $
# Copyright (C) 2003, 2004  Dmitry V. Levin <ldv@altlinux.org>
# 
# The useradd wrapper for the hasher-priv project
#
# This file 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; either version 2 of the License, or
# (at your option) any later version.
# 
# 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 program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

PROG="${0##*/}"

Fatal()
{
	echo "${0##*/}: $*" >&2
	exit 1
}

Usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
hasher-useradd - adds hasher satellite users for already existing user,
and creates hasher-priv config file.

Usage: $PROG [options] <user>

Valid options are:
  --names=USER1:USER2   names for satellite users;
  --number=NUMBER       subconfig identifier;
  -V, --version         print program version and exit;
  -h, --help            show this text and exit.

By default, satellite user names will be choosen based on <user>.
EOF
	[ -n "$1" ] && exit "$1" || exit
}

print_version()
{
	local revision=`printf %s '$Revision: 1.5 $' |sed -ne 's/^\$Revision: \+\([^ ]\+\).*/\1/p'` ||:
	cat <<EOF
hasher-useradd version $revision
Written by Dmitry V. Levin <ldv@altlinux.org>

Copyright (C) 2003, 2004  Dmitry V. Levin <ldv@altlinux.org>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
	exit
}

TEMP=`getopt -n $PROG -o h,V -l names:,number:,help,version -- "$@"` || Usage
eval set -- "$TEMP"

names=
number=
while :; do
	case "$1" in
		--names) shift; names="$1"
			;;
		--number) shift; number="$1"
			;;
		-h|--help) Usage 0
			;;
		-V|--version) print_version
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1" >&2; exit 1
			;;
	esac
	shift
done

[ "$#" -eq 1 ] || Usage

caller="$1"
shift

if [ -n "$number" ]; then
	if ! [ "$number" -ge 1 ] 2>/dev/null; then
		Fatal "invalid subconfig identifier: $number"
	fi
fi

if [ -n "$names" ]; then
	user1="${names%%:*}"
	user2="${names##*:}"
	if [ -z "$user1" -o -z "$user2" -o "$user1" = "$user2"  -o "$caller" = "$user1"  -o "$caller" = "$user2" ]; then
		Fatal "invalid names for satellite users: $names"
	fi
else
	user1="$caller"_a
	user2="$caller"_b
fi

id "$caller" >/dev/null
! id "$user1" >/dev/null 2>&1 ||
	Fatal "error creating satellite users: $user1 already exists"
! id "$user2" >/dev/null 2>&1 ||
	Fatal "error creating satellite users: $user2 already exists"

cd /etc/hasher-priv/user.d

if [ -z "$number" ]; then
	config="$caller"
else
	config="$caller:$number"
fi

! [ -e "$config" ] ||
	Fatal "config file '$config' for user '$caller' already exists"

# First satellite.
/usr/sbin/useradd -M -c "hasher satellite for $caller" -d /dev/null -s /dev/null "$user1" ||
	Fatal "error creating satellite user $user1"

# Second satellite.
/usr/sbin/useradd -M -c "hasher satellite for $caller" -d /dev/null -s /dev/null "$user2" ||
	Fatal "error creating satellite user $user2"

# Add caller to hashman group and first satellite group.
glist="$((id -Gn "$caller" |tr ' ' '\n' && id -gn "$user1" && id -gn "$user2" && echo hashman) |
	sort -u |
	xargs echo |
	tr ' ' ,)"
/usr/sbin/usermod -G "$glist" "$caller" ||
	Fatal "error updating groups for user $caller"

# Create config.
umask 037
cat >"$config" <<__EOF__
user1=$user1
user2=$user2
__EOF__

chgrp -- "$(id -g "$caller")" "$config"
