#!/bin/sh

########################################################################
##
## This script will read the /etc/{passwd,group,shadow} and create an
## equivalent registry tree under $ROOT.
##
## The correct way to run it is:
##
## # kdb convert-users | sh -e
##
## Make sure that system/users is mounted at a secure location
##
##
## To make tests you can do:
##
## $ ROOT=user/test kdb convert-users | sh -e
##
## Avi Alkalay <avi@unix.sh>
## March 2004
##
## $Id$
##
########################################################################


[ -z "$KDB" ] && KDB="kdb"
[ -z "$ROOT" ] && ROOT="system"


cat /etc/passwd | awk -F : "{
	print \"$KDB set $ROOT/users/\" \$1 \"/uid \'\" \$3 \"\'\";
	print \"$KDB set $ROOT/users/\" \$1 \"/password \'\" \$2 \"\'\";
	print \"$KDB set $ROOT/users/\" \$1 \"/gid \'\" \$4 \"\'\";
	print \"$KDB set $ROOT/users/\" \$1 \"/gecos \'\" \$5 \"\'\";
	print \"$KDB set $ROOT/users/\" \$1 \"/home \'\" \$6 \"\'\";
	print \"$KDB set $ROOT/users/\" \$1 \"/shell \'\" \$7 \"\'\";
	print \"\";
}"

echo
echo

cat /etc/group | awk -F : "{
	print \"$KDB set $ROOT/groups/\" \$1 \"/gid \'\" \$3 \"\'\";
	print \"$KDB set $ROOT/groups/\" \$1 \"/members \'\" \$4 \"\'\";
	print \"\";
}"

echo
echo

if [ -r /etc/shadow ]; then
	cat /etc/shadow | awk -F : "{
		print \"$KDB set $ROOT/users/\" \$1 \"/shadowPassword \'\" \$2 \"\'\";
		print \"$KDB set $ROOT/users/\" \$1 \"/passwdChangeBefore \'\" \$4 \"\'\";
		print \"$KDB set $ROOT/users/\" \$1 \"/passwdChangeAfter \'\" \$5 \"\'\";
		print \"$KDB set $ROOT/users/\" \$1 \"/passwdWarnBefore \'\" \$6 \"\'\";
		print \"$KDB set $ROOT/users/\" \$1 \"/passwdDisableAfter \'\" \$7 \"\'\";
		print \"$KDB set $ROOT/users/\" \$1 \"/passwdDisabledSince \'\" \$8 \"\'\";
		print \"$KDB set $ROOT/users/\" \$1 \"/passwdReserved \'\" \$9 \"\'\";
		print \"\";
	}"
fi










