#!/bin/sh

##############################################################################
# Name:		exists_in
# Author:	Ola Lundqvist <ola.lundqvist@euronetics.se>
# Date:		2001-08-05
# Description:	Tells what id names that exists in a password db file.
# Changes:	$EXISTS_IN
# Arguments:	MATCH
#		FILE
##############################################################################

exists_in() {
    EXISTS_IN=$(grep "^[^:]*:[^:]*:$1:" $2 | sed -e "s|^\([^:]*\):.*|\1|")
}

##############################################################################
# Name:		remove_updtmp
# Author:	Ola Lundqvist <ola.lundqvist@euronetics.se>
# Date:		2001-08-05
# Description:	Removes a tmp file if the original file is not empty.
#		If it is empty it will be restored.
# Arguments:	FILE (.tmp will be added for removal)
##############################################################################

remove_updtmp() {
    if [ -s $1 ] ; then
	rm "$1.tmp"
    else
	echo "ERROR in update script, truncates $1, restore from backup."
	cp -a "$1.tmp" $1
	exit 0
    fi
}

##############################################################################
# Name:		remove_name
# Author:	Ola Lundqvist <ola.lundqvist@euronetics.se>
# Date:		2001-08-05
# Description:	Removes a name from a passwd file.
# Arguments:	NAME
#		FILE
#		TYPE
##############################################################################

remove_name() {
    if [ -f $2 ] ; then
	if grep "^$1:" $2 > /dev/null 2>&1 ; then
	    echo "Remove $3 $1."
	    cp -a $2 "$2.tmp"
	    grep -v "^$1:" "$2.tmp" > $2
	    remove_updtmp $2
	fi
    fi
}

##############################################################################
# Name:		update_name
# Author:	Ola Lundqvist <ola.lundqvist@euronetics.se>
# Date:		2001-08-05
# Description:	Updates a name from one passwd file to another.
# Changes:	$TSL
#		$TDL
# Arguments:	NAME
#		SRCFILE
#		DSTFILE
#		TYPE
##############################################################################

update_name() {
    if [ -f $2 -a -f $3 ] ; then
	TSL=$(grep "^$1:" $2)
	TDL=$(grep "^$1:" $3)
	if [ -z "$TDL" ] ; then
	    echo "Adding $4 $1."
	    echo "$TSL" >> $3
	elif [ "$TSL" != "$TDL" ] ; then
	    echo "Change information for $4 $1."
	    cp -a $3 "$3.tmp"
	    echo "sed -e 's|^$1:.*|$TSL|;' '$3.tmp' > $3"
	    sed -e "s|^$1:.*|$TSL|;" "$3.tmp" > $3
	    remove_updtmp $3
	fi
    fi
}

##############################################################################
# Name:		sync_pwddb
# Author:	Ola Lundqvist <ola.lundqvist@euronetics.se>
# Date:		2001-08-05
# Description:	Syncronizes two password files (and its shadow file).
# Uses:		$IMG
#		$SRCIMAGE
# Changes:	$TIMGF (probably not useful)
#		$TSRCF (probably not useful)
#		$SIMGF (probably not useful)
#		$SSRCF (probably not useful)
# Arguments:	MATCH
#		FILE (file to check)
#		SFILE
#		TYPE
##############################################################################

sync_pwddb() {
    TSRCF="$SRCIMAGE/$2"
    TIMGF="$IMG/$2"
    exists_in $1 $TSRCF
    ESRC=$EXISTS_IN
    SIMGF="$IMG/$3"
    SSRCF="$SRCIMAGE/$3"
    exists_in $1 $TIMGF
    EIMG=$EXISTS_IN
    # Which ones to remove.
    for N in $EIMG ; do
	if ! echo "$ESRC" | grep "^$N$" > /dev/null 2>&1 ; then
	    # If it does not exist in the source file it should be
	    # removed.
	    remove_name $N $TIMGF $4
	    remove_name $N $SIMGF "$4(shadow)"
	fi
    done
    # Which ones to add or change.
    for N in $ESRC ; do
	update_name $N $TSRCF $TIMGF $4
	update_name $N $SSRCF $SIMGF "$4(shadow)"
    done
}

##############################################################################
# Author:	Ola Lundqvist <ola.lundqvist@euronetics.se>
# Date:		2001-08-05
##############################################################################

find /etc/nfsboot -maxdepth 1 -name "*.conf" | {
    while read -e CONFF ; do
	. $CONFF
	for IMG in $DESTIMAGES ; do
	    # Passwd
	    if [ -f "$SRCIMAGE/$PWDF" -a -f "$IMG/$PWDF" ] ; then
		for UIM in $UIDM ; do
		    sync_pwddb $UIM $PWDF $SHDF user
		done
	    fi
	    # Group
	    if [ -f "$SRCIMAGE/$GRPF" -a -f "$IMG/$GRPF" ] ; then
		for GIM in $GIDM ; do
		    sync_pwddb $GIM $GRPF $SGRF group
		done
	    fi
	done
    done
}
