#!/bin/sh -
#
# $Id: mk-home-maps,v 5.1 89/11/17 18:24:18 jsp Exp Locker: jsp $
#
# Copyright (C) 1989 by Jan-Simon Pendry
# All Rights Reserved.
#
# Take a list of user-name/directory pairs and a list of
# block-device/mount-point pairs and construct the information
# required for amd to automount the whole lot.
#
# The two mandatory arguments are the names of the files containing
# the required information.
# The output is placed in a file of the same name with .cf appended
#
usage="Usage: $0 dirs part"
if [ $# -ne 2 ]; then
	echo $usage 2>&1
	exit 1
fi
autoroot=/a
ufstype=ufs
opts=rw,grpid,nosuid

dirs="$1"
part="$2"
sfx=".cf"
(
cat $part | sed -e 's/#.*//' -e '/^$/d'
echo ""
cat $dirs | sed -e 's/#.*//' -e '/^$/d'
) |
awk '
BEGIN {
	dirs = "'"$dirs"$sfx'"; part = "'"$part"$sfx'"; fstab = "'"$part"'.fstab"
	seen_blank = -1
	npart = 0
	status = 0
	print "# /home" > part
	printf "/defaults \\\n\ttype=nfs;opts=%s\n", "'"$opts"'" >> part
	print "# /homes" > dirs
	printf "/defaults \\\n\ttype=nfs;opts=%s\n", "'"$opts"'" >> dirs
	printf "" > fstab
}
seen_blank < 0 {
	if (NF == 0) {
		seen_blank = NR
		next
	} else {
		if (split($1, dev, ":") != 2) {
			printf "line %d, $s is not host:/dev/disk\n", NR
			status = 1
		}
#
# Generate an /etc/fstab line
# dev	/a/dom/host/whatever	type	opts	n n
# or
# dev	/a/host/whatever	type	opts	n n
#
		if (NF != 3)
			fstype = "'"$ufstype"'"
		else
			fstype = $3

		printf "#\n# Fstab entry for %s\n#\n", dev[1] >> fstab
		printf "%s\t%s/%s%s\t%s\t%s\t0 1\n", dev[2], "'"$autoroot"'", dev[1], $2, fstype, "'"$opts"'" >> fstab

		device[npart] = dev[2]
		partition[npart] = $2
		hostname[$2] = dev[1]
		n = split($2, v, "/")
#		printf "%d: dev = %s, part = %s, n = %d\n", npart, dev[2], $2, n
		mc = ""
		if (n < 3 || v[2] != "home") {
			printf "line %d, %s is not of the form /home/machine/...", NR, $2
			status = 1
		} else {
			mc = v[3]
			for (p = 3; p < n; p++) {
				if (automap[mc] == "") {
					automap[mc] = mc;
					printf "%s", mc >> part
					if (length(mc) > 7) print " \\" >> part
					printf "\ttype=auto;fs=${map};pref=%s/\n", mc >> part
				}
				mc = mc "/" v[p+1]
			}
			printf "%s", mc >> part
			if (length(mc) > 7) print " \\" >> part
			printf "\trfs=%s;rhost=%s\n", $2, dev[1] >> part
			npart++
		}
	}
}

seen_blank >= 0 {
	user = $1
	dir = $2
	n = split(dir, v, "/")
#	printf "n = %d, user = %s, dir = %s\n", n, user, dir
	if (n < 4 || v[2] != "home") {
		printf "Warning: %s (~%s) is not of the form /home/machine/...\n", dir, user
		printf "%s", user >> dirs
		if (length(user) > 7) print " \\" >> dirs
		printf "\ttype=link;fs=%s\n", dir >> dirs
	} else {
		fs = ""
		other = ""
		path = "/home"
		i = 3;
		while (i < n) {
			path = path "/" v[i];
			for (p = 0; p < npart; p++) {
#				printf "checking path %s against partition %s\n", path, partition[p]
				if (partition[p] == path) {
					fs = path
					bdev = device[p]
					machine = hostname[fs]
#					printf "machine = %s\n", machine
					break
				}
			}
			i++
			if (fs != "")
				break
		}
		if (fs != "") {
			rest = ""
			while (i <= n) {
				rest = rest v[i]
#				printf "i = %d, n = %d, rest = %s\n", i, n, rest
				if (i != n)
					rest = rest "/"
				i++
			}

#			printf "user=%s, filesys=%s, rest=%s, machine=%s\n", user, fs, rest, machine
			printf "%s", user >> dirs
			if (length(user) > 7) print " \\" >> dirs
			printf "\trfs=%s;rhost=%s;sublink=%s\n", fs, machine, rest >> dirs
		} else {
			printf "ERROR: Cannot find a partition for %s (~%s)\n", dir, user
			status = 1
		}
	}
}
END {
	exit(status)
}
' >&2

