#!/bin/sh
#
# $Id: finish,v 1.14 1996/08/09 15:36:50 casper Exp $
#
#	finish: install site specific files after auto-installation
#
# Authors:
#
#	Jan Wortelboer	(janw)
# 	Gert Poletiek	(gert)
#	Casper Dik	(casper)
#

# We're read directly by the suninstall script.  Execute all in subshell
# We don't want to influence our parent.

(

PATH=/sbin:/usr/sbin:/usr/bin:/bin:/usr/ucb:/usr/lib:/etc:.
export PATH

#
#  mountpoint of / for the new system
#
newroot=/a
export newroot

#
# set a nice default umask
#
umask 022

#
#  location of files to install
#
install=${SI_CONFIG_DIR}/install
scripts=scripts
files=files
classes=

#
# Switch filesystems to fast
#
$SI_CONFIG_DIR/bin/fastfs -a fast

domainname="`domainname`"
hostname="`uname -n`"

#
# Borrowed from /etc/rcS, needed for the standard patches script
# but added here because some other scripts may want it.
#

readvfstab() {
    _readvfstab "$@" < ${newroot}/etc/vfstab
}

_readvfstab() {
        while read special fsckdev mountp fstype fsckpass automnt mntopts
        do
                case ${special} in
                '#'* | '')      #  Ignore comments, empty lines
                                continue ;;
                '-')            #  Ignore no-action lines
                                continue
                esac

                if [ "${mountp}" = "$1" ]
                then
                        return 0
                fi
        done
        unset read special fsckdev mountp fstype fsckpass automnt mntopts
	return 1
}

#
#  Mount the last fs found by readvfstab (plus options)
#
mountfs ()
{
    if [ "x$mntopts" = x- ]; then mntopts= ; fi
    if [ -z "$fstype" ]
    then
	echo "******** can't mount filessytem, no successfull readvfstab"
	return 1
    fi
    msg=
    if [ "$fstype" = cachefs ]
    then
	msg=' (cachfs, mounted as r/o nfs)'
	fstype=nfs
	mntopts=ro
    elif [ $# = 1 ]
    then
	if [ -z "$mntopts" ]
	then mntopts=$1
	else mntopts=$mntopts,$1
	fi
    fi
    echo "Mounting $special on ${newroot}$mountp, -o${mntopts} $msg"
    /usr/sbin/mount ${mntopts:+-o$mntopts} -F $fstype -m $special ${newroot}$mountp
}

#
# If you change this, don't forget to update install_check.
#
#  findfiles locates a file in the directory ${SI_CONFIG_DIR}/install
#  and returns all matches (the hostname, one match for each class
#  (either class.domainname or class) the domainname.
#  If there are no matches, it returns default.
#
#  findfile returns only the first match
#
# Search order is:
#	${install}/<file name>   if it's a file.
#	${install}/<file name>/<system name>
# Then for each class to which a machine belongs:
#	${install}/<file name>/<class>.<yp domain name>
#	${install}/<file name>/<class>
# And finally:
#	${install}/<file name>/<yp domain name>
#	${install}/<file name>/default  (only returned when no other matches)
#

findfile ()
{
    findfiles -1 "$1"
}

findfiles ()
{
    match=
    if [ x"$1" = x-1 ]
    then
	shift;
	one=true
    else
	one=
    fi
    if [ -f ${install}/$1 ]
    then
	echo ${install}/$1
	return
    elif [ -f ${install}/$1/$hostname ]
    then
	echo ${install}/$1/$hostname
	[ -n "$one" ] && return
	match=true
    fi
    for class in $classes
    do
	if [ -f ${install}/$1/"$class.$domainname" ]
	then
	    echo ${install}/$1/"$class.$domainname"
	    [ -n "$one" ] && return
	    match=true
	elif [ -f ${install}/$1/$class ]
	then
	    echo ${install}/$1/$class
	    [ -n "$one" ] && return
	    match=true
	fi
    done
    if [ -f ${install}/$1/"$domainname" ]
    then
	echo ${install}/$1/"$domainname"
    elif [ -z "$match" ]
    then
	echo ${install}/$1/default
    fi
}

findconf ()
{
    if [ -f ${install}/$1/common ]
    then
	echo ${install}/$1/common
    fi
    set -- `findfiles $1`
    # We must return at least one file.
    if [ ! -r "$1" ]
    then
	echo /dev/null
    else
	echo $@
    fi
}

# Strip comments and blank lines from conf files.
readconf ()
{
    conf=`findconf $1`
    [ -n "$confverbose" ] && echo "$1	=" $conf 1>&2
    egrep -h -v '^#|^[	 ]*$' $conf
}

cd $install
confverbose=true

classfile=`findfile class`
if [ ! -f "$classfile" ]
then
    classfile=/dev/null
fi
classes=`cat "$classfile"`
export classes
for f in ${install}/class/S[0-9]*.sh
do
    if [ -x $f ]
    then
	classes="$classes `. $f`"
    fi
done
>${newroot}/etc/INSTALL_CLASS
chmod 644 ${newroot}/etc/INSTALL_CLASS
for class in $classes
do
    echo $class >> ${newroot}/etc/INSTALL_CLASS
done

#
#	remove files in the rm.conf file
#
readconf rm.conf | while read opt target
do
	if [ -f "${newroot}$target" -o \
	     -h "${newroot}$target" -o \
	     -d "${newroot}$target" ]
	then
		echo removing ${newroot}/$target
		dir=`dirname ${newroot}/$target`
		fil=`basename ${newroot}/$target`
		( cd $dir && rm -$opt $fil )
	else
		echo "******* can't remove $target"
	fi
done


#
#	Create directories in the mkdir.conf file
#
readconf mkdir.conf | while read owner group mode dirname
do
	echo creating $dirname
	mkdir -p ${newroot}/$dirname
	chown $owner ${newroot}/$dirname
	chgrp $group ${newroot}/$dirname
	chmod $mode ${newroot}/$dirname
done


#
#	move files in the mv.conf file
#
readconf mv.conf | while read oldname newname
do
	if [ -f "${newroot}/$oldname" -o \
	     -h "${newroot}/$oldname" -o \
	     -d "${newroot}/$oldname" ]
	then
		echo renaming ${newroot}/$oldname to ${newroot}/$newname
		mv ${newroot}/$oldname ${newroot}/$newname
	else
		echo "******* can't move $oldname to $newname"
	fi
done

#
#	touch files in the touch.conf file
#
readconf touch.conf | while read target owner group mode comment
do
	echo "creating empty ${newroot}/$target ($comment)"
	touch ${newroot}/$target
	chmod $mode ${newroot}/$target
	chown $owner ${newroot}/$target
	chgrp $group ${newroot}/$target
done

#
#	Copy files in the copy.conf file
#
readconf copy.conf | while read source destdir owner group mode comment
do
    src=`findfile ${files}/$source`
    target=${newroot}/${destdir}/$source
    targetdir=`dirname "$target"`
    if [ -d "$targetdir" -a -r "$src" ]
    then
	echo installing $comment
	if [ -f $target -a ! -f $target.FCS ]
	then
	    # keep linking relationships.
	    cp -p $target $target.FCS; chmod ug-s,a-w,og-x,-t $target.FCS
	fi
	cp -p "$src" $target
	chmod $mode $target
	chown $owner $target
	chgrp $group $target
    else
	echo "******* can't install $destdir/$source ($comment)"
    fi
done

#
#	create symbolic links in the slink.conf file
#
readconf slink.conf | while read filename linkname
do
	targetdir=`dirname ${newroot}/$linkname`
	if [ -d $targetdir ]
	then
	    echo sym-linking $linkname
	    ln -s $filename ${newroot}/$linkname
	else
	    echo "******* can't symlink $filename to $linkname"
	fi
done

#
#	Create hard links in the link.conf file
#
readconf link.conf | while read filename linkname
do
	targetdir=`dirname ${newroot}/$linkname`
	if [ -d $targetdir -a -f "${newroot}/$filename" ]
	then
		echo linking $linkname
		ln ${newroot}/$filename ${newroot}/$linkname
	else
		echo "******* can't link $filename to $linkname"
	fi
done

#
#	append to files in the append.conf file
#
readconf append.conf | while read file target
do
	if [ -f "${newroot}/$target" ]
	then
		file=`findfile ${files}/$file`
		echo appending $file to ${newroot}/$target
		cat $file >> ${newroot}/$target
	else
		echo "******* can't append to $target"
	fi
done

# Run the scripts last. So you can do everything you want there.
#
#	execute scripts to do things that cannot be done with the .conf files
#
readconf scripts.conf | while read script rest
do
	script=`findfile ${scripts}/$script`
	echo running $script
	. $script < /dev/null
done

#
# Switch filesystems back to slow
#
$SI_CONFIG_DIR/bin/fastfs -a slow

echo installation complete

# Subshell end
)
