#!/bin/sh
# $Id: make-profile.sh,v 1.4 2003/12/28 23:51:21 lreiter Exp $
#
#	make-profile.sh
#		Profile-based provisioning technology
#		Master profile creation script
#
#	Copyright 2003 NeTraverse, Inc.
#
#
# 	return values:
#		0:	success
#		1:	invalid usage
#		2:	must run as root
#		3:	invalid user specified/cannot determine home directory
#		4:	already profile, and -f not specified
#		5:	user does not contain a Windows installation
#		6:	failed to generate/update profile information
#

. /etc/default/merge

PROGNAME=`basename $0`

USERID=`$STATICMERGE/bin/print_uid`
if [ "$USERID" != "0" ]; then
	echo "$PROGNAME: you must be root to run this tool"
	exit 2
fi

FORCE_PROFILE=""
if [ $# = 2 -a "$1" = "-f" ]; then
	FORCE_PROFILE="true"
	shift
elif [ $# != 1 -o "$1" = "-f" ]; then
	echo "usage: $PROGNAME [-f] <master-username>"
	echo "       (use -f flag to force recreation of profile)"
	exit 1
fi

MASTERUSERNAME=$1
HOMEDIR=`su - $1 -c pwd`
if [ "$HOMEDIR" = "" ]; then
	echo "$PROGNAME: unable to determine home directory for user $1"
	exit 3
fi

PROFILEDIR=$HOMEDIR/.merge-profile
WINDIR=$HOMEDIR/win
MERGEDIR=$HOMEDIR/.merge

if [ -e $PROFILEDIR -a "$FORCE_PROFILE" = "" ]; then
	echo "$PROGNAME: $HOMEDIR already contains profile information"
	echo "If you feel this is a mistake, remove $PROFILEDIR"
	echo "or run this tool again using the -f flag"
	exit 4
fi

if [ ! -f $WINDIR/windows/inf/layout.inf -o ! -d $MERGEDIR ]; then
	echo "$PROGNAME: $HOMEDIR does not contain a Windows installation"
	exit 5
fi

echo -n "Writing profile information..."
if [ -e $PROFILEDIR ]; then
	rm -rf $PROFILEDIR
fi
mkdir -m 755 $PROFILEDIR
mkdir -m 755 $PROFILEDIR/default
chown $MASTERUSERNAME $PROFILEDIR
chown $MASTERUSERNAME $PROFILEDIR/default
echo "MERGE_PROFILE_TYPE=\"master\"" >$PROFILEDIR/profile-info
echo "MERGE_PROFILE_NAME=\"generic master profile\"" >>$PROFILEDIR/profile-info
echo "done"
chmod 644 $PROFILEDIR/profile-info
chown $MASTERUSERNAME $PROFILEDIR/profile-info


echo -n "Generating meta-data and fixing permissions..."
chmod 755 $HOMEDIR $MERGEDIR $WINDIR $PROFILEDIR
chmod -R a+r $MERGEDIR
chmod -R a+r $PROFILEDIR
$STATICMERGE/adminbin/profile-update $WINDIR >$PROFILEDIR/profile-meta.tmp
if [ $? != 0 ]; then
	exit 6
fi
chmod 644 $PROFILEDIR/profile-meta.tmp
chown $MASTERUSERNAME $PROFILEDIR/profile-meta.tmp
mv $PROFILEDIR/profile-meta.tmp $PROFILEDIR/profile-meta
echo "done"
exit 0

