#!/bin/sh
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)merge_shadow.sh	1.3	05/03/23 SMI"
#
# This is the mergescript used to merge in the root entry from the
# miniroot's /etc/shadow file into the installed system's /etc/shadow file.
#

if [ $# != 1 ]; then
	exit 1
fi

INSTALLROOTDIR=$1
SHADOW_FILE=/etc/shadow

# If /etc/shadow does not exist in the installed area,
# then just copy over the one from the miniroot and exit.
if [ ! -f ${INSTALLROOTDIR}/${SHADOW_FILE} ]; then
	cp ${SHADOW_FILE} ${INSTALLROOTDIR}/${SHADOW_FILE} >/dev/null 2>&1
	exit $?
fi

PASSWD=`/usr/bin/grep '^root:' ${SHADOW_FILE} | cut -d: -f2`

nawk -F: '{ 
        if ( $1 == "root" ) 
            printf"%s:%s:%s:%s:%s:%s:%s:%s:%s\n",$1,passwd,$3,$4,$5,$6,$7,$8,$9
        else
            printf"%s:%s:%s:%s:%s:%s:%s:%s:%s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9
        }' passwd="$PASSWD" ${INSTALLROOTDIR}/${SHADOW_FILE} > \
			    ${INSTALLROOTDIR}/${SHADOW_FILE}.new 2>/dev/null

if [ $? != 0 ]; then
	exit 1
fi

cp ${INSTALLROOTDIR}/${SHADOW_FILE}.new ${INSTALLROOTDIR}/${SHADOW_FILE} >/dev/null 2>&1
if [ $? != 0 ]; then
	exit 1
fi

# remove the temporary file
rm -f ${INSTALLROOTDIR}/${SHADOW_FILE}.new >/dev/null 2>&1

exit 0
