# @(#)build:BOS/.profile	35.4
# BOS Chroot(1M) User Dot Profile Shell Script
# Copyright 1988, Motorola, Inc.

ProductName=SVC
export ProductName

BlocksNeeded=80000			# Free blocks required for build;
InodesNeeded=15000			# Free inodes required for build;

ChrootDirectory=`cd ../../../..; pwd`
DotDirectory=`expr \`pwd\` : "${ChrootDirectory}/\(.*\)"`
SourceDirectory=usr/src


# Define basic sh(1) environment variables.
#
TERM=vt100; export TERM
PATH=.:${PATH}:/etc:/local/bin; export PATH
ROOT=/; export ROOT
TMPDIR=/usr/tmp; export TMPDIR

umask 000


BuildRunningPath=${SourceDirectory}/BuildRunning

if [ -f ${ChrootDirectory}/${BuildRunningPath} ]
then
	date=`ls -l ${ChrootDirectory}/${BuildRunningPath}`
	set -- ${date}
	month=${6}
	day=${7}
	time=${8}

	0<<!!! 1>&2 cat -

	${ChrootDirectory}/${BuildRunningPath} exists, indicating that:

	*  A build is currently executing in ${ChrootDirectory}.
	*  The build was begun: ${month} ${day} at ${time}.


	Please log-in later, after the build is completed.


!!!
	exit 1
fi

case ${LOGNAME} in
mk* )
	# Check for prerequisite list files;

	anErrorOccurred=false
	for listPath in		\
		list/notbuilt	\
		list/notobjs	\
		list/notsrcs	\
		list/notused	\
		list/sources	\
		list/srcobjs	\
		list/objsrcs
	do
		if [ ! -f ${listPath} ]
		then
			0<<!!! 1>&2 cat -
${LOGNAME}: ${listPath} is not found;
!!!
			anErrorOccurred=true
		fi
	done


	# Check for enough free space;
	set -- `df ${ChrootDirectory}`
	blocksAvailable=${4}
	inodesAvailable=${6}

	if [ ${blocksAvailable} -lt ${BlocksNeeded} ] 
	then
		0<<!!! 1>&2 cat -
${LOGNAME}: have ${blocksAvailable}, need ${BlocksNeeded} free blocks;"
!!!
		anErrorOccurred=true
	fi

	if [ ${inodesAvailable} -lt ${InodesNeeded} ]
	then
		0<<!!! 1>&2 cat -
${LOGNAME}: have ${inodesAvailable}, need ${InodesNeeded} free inodes;"
!!!
		anErrorOccurred=true
	fi


	# If errors then stop;

	if ${anErrorOccurred}
	then
		1>&2 echo "${LOGNAME}: cannot proceed."
		exit 1
	fi


	1>${ChrootDirectory}/${BuildRunningPath}


	# Determine the build date and time.
	#
	set -- `date '+%y %m %d %H %M'`
	year=${1}
	month=${2}
	day=${3}
	hour=${4}
	minute=${5}

	LogPath=${DotDirectory}/Log.${year}${month}${day}${hour}${minute}
	BuildScript=${TMPDIR}/build.${$}

	SourceList=${DotDirectory}/SourceList
	SourceMissing=${DotDirectory}/SourceMissing
	SourceCreated=${DotDirectory}/SourceCreated
	SourceNotUsed=${DotDirectory}/SourceNotUsed
	SourceModified=${DotDirectory}/SourceModified
	SourceNotDistributed=${DotDirectory}/SourceNotDistr
	SourceNotFound=${DotDirectory}/SourceNotFound

	ObjectList=${DotDirectory}/ObjectList
	ObjectNotBuilt=${DotDirectory}/ObjectNotBuilt
	ObjectNotDistributed=${DotDirectory}/ObjectNotDistr
	ObjectNotFound=${DotDirectory}/ObjectNotFound


	# Execute the build in backgroup;

	0<<!!! 1>${ChrootDirectory}/${BuildScript} cat -
# Clean-up from last build;

0<<DONE cat -
${LOGNAME}: Cleaning up from last build;
DONE

( cd ${DotDirectory}; make -f ${ProductName}.mk clobber )

rm -f /sysV68.rel /master.rel

rm -f ${SourceList} ${SourceMissing} ${SourceCreated} ${SourceNotUsed} \
	${SourceModified} ${SourceNotDistributed} ${SourceNotFound}

rm -f ${ObjectList} ${ObjectNotBuilt} ${ObjectNotDistributed} ${ObjectNotFound}


# Define temporary file names;

NotSourceList=${TMPDIR}/dob.0.\${$}
TemporarySourceList=${TMPDIR}/dob.1.\${$}
SourceListDifferences=${TMPDIR}/dob.2.\${$}
SourceMissingList=${TMPDIR}/dob.3.\${$}
SourceCreatedList=${TMPDIR}/dob.4.\${$}
SourceNotAccessedList=${TMPDIR}/dob.5.\${$}
SourceModifiedList=${TMPDIR}/dob.6.\${$}
SourceNotUsedDirectory=${TMPDIR}/dob.7.\${$}
SourceObjectList=${TMPDIR}/dob.8.\${$}
NewSourceObjectList=${TMPDIR}/dob.9.\${$}
NewObjectList=${TMPDIR}/dob.10.\${$}
ObjectNotGeneratedList=${TMPDIR}/dob.11.\${$}
NotObjectList=${TMPDIR}/dob.12.\${$}
ObjectNotBuiltList=${TMPDIR}/dob.13.\${$}
ObjectListDifferences=${TMPDIR}/dob.14.\${$}
TemporaryObjectList=${TMPDIR}/dob.15.\${$}
TemporaryUsedList=${TMPDIR}/dob.16.\${$}
TemporaryTouchList=${TMPDIR}/dob.17.\${$}
SourceDistributionList=${TMPDIR}/dob.18.\${$}
SourceNotDistributedList=${TMPDIR}/dob.19.\${$}
SourceNotFoundList=${TMPDIR}/dob.20.\${$}
ObjectDistributionList=${TMPDIR}/dob.21.\${$}
ObjectNotDistributedList=${TMPDIR}/dob.22.\${$}
ObjectNotFoundList=${TMPDIR}/dob.23.\${$}
TemporaryFileList=${TMPDIR}/dob.24.\${$}
ObjectSourceList=${TMPDIR}/dob.25.\${$}

trap "
	# Remove the build temporaries.
	#
	rm -rf \${SourceNotUsedDirectory}
	rm -f	${BuildRunningPath}		\
		${BuildScript}			\
		\${NotSourceList}		\
		\${TemporarySourceList}		\
		\${SourceListDifferences}	\
		\${SourceMissingList}		\
		\${SourceCreatedList}		\
		\${SourceNotAccessedList}	\
		\${SourceModifiedList}		\
		\${SourceObjectList}		\
		\${NewSourceObjectList}		\
		\${NewObjectList}		\
		\${ObjectNotGeneratedList}	\
		\${NotObjectList}		\
		\${ObjectNotBuiltList}		\
		\${ObjectListDifferences}	\
		\${TemporaryObjectList}		\
		\${TemporaryUsedList}		\
		\${TemporaryTouchList}		\
		\${SourceDistributionList}	\
		\${SourceNotDistributedList}	\
		\${SourceNotFoundList}		\
		\${ObjectDistributionList}	\
		\${ObjectNotDistributedList}	\
		\${ObjectNotFoundList}		\
		\${TemporaryFileList}		\
		\${ObjectSourceList}
	exit 0
" 0 1 2 3 15

# Define new function: create temporary list generator;

removeComments ()
{
	sed -e '/^#/d' -e 's/\\([^\\\\]\\)#.*$/\\1/' -e '/^[ 	]*$/d'
}

generateList ()
{
	thePredicate="\${1} -print"

	removeComments |
	while read pathList
	do
		2>/dev/null find \${pathList} \${thePredicate}
	done
}


# Take a snap-shot of the source directory hierarchy
# for comparison's sake after the build;

0<<DONE cat -
${LOGNAME}: Taking a snap shot of the source space;
DONE

0<${DotDirectory}/list/notsrcs 1>\${NotSourceList} generateList

find ${SourceDirectory} -print |
fgrep -v -x '\
${BuildRunningPath}
${BuildScript}
${LogPath}
${DotDirectory}/BuildBegun
${DotDirectory}/BuildDone
${SourceList}'	|
fgrep -v -x -f \${NotSourceList} |
1>\${TemporarySourceList} sort


# Touch all source files to an old date
# so that un-accessed sources may be located;

0<<DONE cat -
${LOGNAME}: Touching last access date back on all sources;
DONE

mkdir \${SourceNotUsedDirectory}
0<${DotDirectory}/list/notused generateList |
split -300 - \${SourceNotUsedDirectory}/

0<${DotDirectory}/list/srcobjs removeComments |
while read pathList
do
	ls -d ${pathList}

done 1>\${SourceObjectList} 2>/dev/null

find ${SourceDirectory} -type f -print |
fgrep -v -x "\
${BuildRunningPath}
${BuildScript}
${LogPath}
${DotDirectory}/BuildBegun
${DotDirectory}/BuildDone
${SourceList}"	|
fgrep -v -x -f \${SourceObjectList} |
1>\${TemporaryTouchList} fgrep -v -x -f \${NotSourceList}

ls \${SourceNotUsedDirectory}/* |
while read notusedlist
do
	0<\${TemporaryTouchList} 1>\${TemporaryUsedList} \
		fgrep -v -x -f \${notusedlist}
	mv \${TemporaryUsedList} \${TemporaryTouchList}
done

0<\${TemporaryTouchList} xargs touch -ac 0101000070


# Execute the build script;

0<<DONE cat -
${LOGNAME}: Executing source build;
DONE

1>${DotDirectory}/BuildBegun
( cd ${SourceDirectory}; time sh -x :mk )
1>${DotDirectory}/BuildDone

0<<DONE cat -
${LOGNAME}: Source build is completed;
DONE


# Prepare installation memory-based file-system;

0<<DONE cat -
${LOGNAME}: Building installation scripts;
DONE

(
	cd ${SourceDirectory}/install
	make -e -f install.mk install
	make -e -f install.mk clobber
)


# Look for differences in the source space;

0<<DONE cat -
${LOGNAME}: Checking for source space changes;
DONE

0<${DotDirectory}/list/notsrcs 1>\${NotSourceList} generateList
0<${DotDirectory}/list/objsrcs 1>\${ObjectSourceList} generateList

find ${SourceDirectory} -print |
cat \${ObjectSourceList} - |
fgrep -v -x -f \${NotSourceList} |
fgrep -v -x '\
${BuildRunningPath}
${BuildScript}
${LogPath}
${DotDirectory}/BuildBegun
${DotDirectory}/BuildDone
${SourceList}'	|
1>${SourceList} sort

1>\${SourceListDifferences} diff \${TemporarySourceList} ${SourceList}

0<\${SourceListDifferences} 1>\${SourceMissingList} sed -n -e 's/^< //p'
0<\${SourceListDifferences} 1>\${SourceCreatedList} sed -n -e 's/^> //p'

if [ -s \${SourceMissingList} ]
then
	0<<DONE 1>&2 cat -
${LOGNAME}: Detected possible deleted source(s);
DONE
	0<<DONE 1>${SourceMissing} cat - \${SourceMissingList}
The following files did exist prior to the build;
but after the build are NOT found:

DONE
fi

if [ -s \${SourceCreatedList} ]
then
	0<<DONE 1>&2 cat -
${LOGNAME}: Detected possible created source(s);
DONE
	0<<DONE 1>${SourceCreated} cat - \${SourceCreatedList}
The following files did NOT exist prior to the build;
but ARE found after the build was competed:

DONE
fi


# Locate unaccessed source files;

0<<DONE cat -
${LOGNAME}: Checking for unaccessed sources;
DONE

rm -rf \${SourceNotUsedDirectory}
mkdir \${SourceNotUsedDirectory}

0<${DotDirectory}/list/notused generateList |
split -300 - \${SourceNotUsedDirectory}/

find ${SourceDirectory} -type f -atime +360 -print |
fgrep -v -x -f \${NotSourceList} |
1>\${SourceNotAccessedList} sort

ls \${SourceNotUsedDirectory}/* |
while read notusedlist
do
	0<\${SourceNotAccessedList} 1>\${TemporaryUsedList} \
		fgrep -v -x -f \${notusedlist}
	mv \${TemporaryUsedList} \${SourceNotAccessedList}
done

if [ -s \${SourceNotAccessedList} ]
then
	0<<DONE 1>&2 cat -
${LOGNAME}: Possible unaccessed source(s) detected;
DONE
	0<<DONE 1>${SourceNotUsed} cat - \${SourceNotAccessedList}
The following files have NOT been accessed
since the build was begun:

DONE
fi


# Locate modified sources;

0<<DONE cat -
${LOGNAME}: Checking for modified sources;
DONE

find ${SourceDirectory} -type f -newer ${DotDirectory}/BuildBegun -print |
fgrep -v -x -f \${NotSourceList} |
fgrep -v -x '\
${LogPath}
${DotDirectory}/BuildDone
${SourceCreated}
${SourceList}
${SourceMissing}
${SourceModified}
${SourceNotUsed}' |
1>\${SourceModifiedList} sort

if [ -s \${SourceModifiedList} ]
then
	0<<DONE 1>&2 cat -
${LOGNAME}: Possible modified source(s) detected;
DONE
	0<<DONE 1>${SourceModified} cat - \${SourceModifiedList}
The following files have been modified since
the build was begun:

DONE
fi


# Locate undistributed sources;

0<<DONE cat -
${LOGNAME}: Checking for undistributed sources;
DONE

0<${DotDirectory}/list/sources 1>\${SourceDistributionList} removeComments
1>\${SourceListDifferences} diff ${SourceList} \${SourceDistributionList}

0<\${SourceListDifferences} 1>\${SourceNotDistributedList} sed -n -e 's/^< //p'
0<\${SourceListDifferences} 1>\${SourceNotFoundList} sed -n -e 's/^> //p'

if [ -s \${SourceNotDistributedList} ]
then
	0<<DONE 1>&2 cat -
${LOGNAME}: Possible undistributed source(s) detected;
DONE
	0<<DONE 1>${SourceNotDistributed} cat - \${SourceNotDistributedList}
The following files were found in the source space but
were not found on the source distribution list:

DONE
fi

if [ -s \${SourceNotFoundList} ]
then
	0<<DONE cat -
${LOGNAME}: Possible missing source(s) detected;
DONE
	0<<DONE 1>${SourceNotFound} cat - \${SourceNotFoundList}
The following files were found in the source distribution list but
were not found in the source space:

DONE
fi


# Locate object files;

0<<DONE cat -
${LOGNAME}: Locating object files;
DONE

0<${DotDirectory}/list/notobjs 1>\${NotObjectList} generateList

0<${DotDirectory}/list/srcobjs removeComments |
1>\${SourceObjectList} 2>/dev/null xargs ls -d

0<<DONE 1>\${TemporaryFileList} sed -e 's:^/::'
\${NotSourceList}
\${TemporarySourceList}
\${SourceListDifferences}
\${SourceMissingList}
\${SourceCreatedList}
\${SourceNotAccessedList}
\${SourceModifiedList}
\${SourceNotUsedDirectory}
\`2>/dev/null ls \${SourceNotUsedDirectory}/*\`
\${SourceObjectList}
\${NewSourceObjectList}
\${NewObjectList}
\${ObjectNotGeneratedList}
\${NotObjectList}
\${ObjectNotBuiltList}
\${ObjectListDifferences}
\${TemporaryObjectList}
\${TemporaryUsedList}
\${TemporaryTouchList}
\${SourceDistributionList}
\${SourceNotDistributedList}
\${SourceNotFoundList}
\${ObjectDistributionList}
\${ObjectNotDistributedList}
\${ObjectNotFoundList}
\${TemporaryFileList}
\${ObjectSourceList}
DONE

find . -mount -print |
sed -e '/^\\.$/d' -e 's:^\\./::' -e '/^`
	echo ${SourceDirectory} |
	sed -e 's:^/::' -e 's:/:\\\/:g'`/d' |
fgrep -v -x -f \${NotObjectList} |
fgrep -v -x -f \${TemporaryFileList} |
1>${ObjectList} sort -u - \${SourceObjectList}


# Locate ungenerated object files;

0<<DONE cat -
${LOGNAME}: Checking for ungenerated objects;
DONE

while read pathList
do
	if [ -f \${pathList} ]
	then
		echo \${pathList}
	fi
done 0<\${SourceObjectList} 1>\${NewSourceObjectList}

mv \${NewSourceObjectList} \${SourceObjectList}

find . -mount -type f -print |
sed -e '/^\\.$/d' -e 's:^\\./::' -e '/^`
	echo ${SourceDirectory} |
	sed -e 's:^/::' -e 's:/:\\\/:g'`/d' |
fgrep -v -x -f \${NotObjectList} |
fgrep -v -x -f \${TemporaryFileList} |
1>\${TemporaryObjectList} sort -u - \${SourceObjectList}


0<\${SourceObjectList} 1>\${NewSourceObjectList} \
	generateList "-type f -newer ${DotDirectory}/BuildBegun"

0<${DotDirectory}/list/notbuilt 1>\${ObjectNotBuiltList} generateList

find . -type f -mount -newer ${DotDirectory}/BuildBegun -print |
sed -e '/^\\.$/d' -e 's:^\\./::' -e '/^`
	echo ${SourceDirectory} |
	sed -e 's:^/::' -e 's:/:\\\/:g'`/d' |
1>\${NewObjectList} sort -u - \${NewSourceObjectList} \${ObjectNotBuiltList}

diff \${NewObjectList} \${TemporaryObjectList} |
1>\${ObjectNotGeneratedList} sed -n -e 's/^> //p'

if [ -s \${ObjectNotGeneratedList} ]
then
	0<<DONE 1>&2 cat -
${LOGNAME}: Possible ungenerated object(s) detected;
DONE
	0<<DONE 1>${ObjectNotBuilt} cat - \${ObjectNotGeneratedList}
The following object files have not been modified
since the last build was executed:

DONE
fi


# Locate undistributed objects;

0<<DONE cat -
${LOGNAME}: Checking for undistributed or missing object(s);
DONE

case ${ProductName} in
SVC )
	cat ${DotDirectory}/list/system ${DotDirectory}/list/user |
	removeComments |
	1>\${ObjectDistributionList} sort

	1>\${ObjectListDifferences} diff \
			${ObjectList} \${ObjectDistributionList}

	0<\${ObjectListDifferences} 1>\${ObjectNotDistributedList} \
		sed -n -e 's/^< //p'
	0<\${ObjectListDifferences} 1>\${ObjectNotFoundList} \
		sed -n -e '/^> master\\.rel\$/d' -e '/^> sysV68\\.rel\$/d' \
			-e 's/^> //p'

	if [ -s \${ObjectNotDistributedList} ]
	then
		0<<DONE 1>&2 cat -
${LOGNAME}: Possible undistributed object(s) detected;
DONE
		0<<DONE 1>${ObjectNotDistributed} cat - \
				\${ObjectNotDistributedList}
The following files were found in the chroot environment but
were not on the distribution list:

DONE
	fi

	if [ -s \${ObjectNotFoundList} ]
	then
		0<<DONE 1>&2 cat -
${LOGNAME}: Possible distributed object(s) missing;
DONE
		0<<DONE 1>${ObjectNotFound} cat - \${ObjectNotFoundList}
The following files were found in the object distribution list but
were not found in the chroot(1M) environment:

DONE
	fi
	;;
esac


# All done.
#
exit 0
!!!

	1>${ChrootDirectory}/${LogPath} 2>&1 \
		nohup chroot ${ChrootDirectory} /bin/sh ${BuildScript} &

	0<<!!! cat -
${LOGNAME}: Build executing at ${month}/${day}/${year} ${hour}:${minute}.
!!!
	exit 0
	;;
* )
	HOME=/; export HOME
	PS1="`expr //${ChrootDirectory} : '.*/\(.*\)'`_root: ";export PS1
	exec chroot ${ChrootDirectory} /bin/sh
	;;
esac
