#!/usr/bin/muawk
# adduser.awk: used by /usr/binadduser  
# m.andreoli

BEGIN	{
	# configurable
	gid=100 # users group
	#  passed by shell-script wrapper
		# user, full_name, 
	FS=":";
	}

	{
	Uid[NR]=$3;
	}


END	{ 
	isort(Uid,NR);
	last_uid=Uid[NR];
	new_uid=last_uid + 1;
	OFS=":";
	home="/home/"user;
	shell="/bin/sh";
	print user,"",new_uid,gid,full_name,home,shell;
	}

# LIBRARY


function isort(A,n,  i,j,hold) 
{
	for (i=2; i<=n; i++)
	{
	hold=A[j=i];
	while ( compare(A[j-1],hold,field) )  { j--; A[j+1]=A[j] }
	A[j]=hold;
	}

}

function compare(x,y,f)
{
if (f==0 )  return (x>y)
else
	{
	split(x,X); split(y,Y);
	return ( X[f] > Y[f] )
	}
}
