shithub: cc65-plan9

ref: 7870bfc34bbd6c2cf918749490ee8fc8dd0177db
dir: /overlay/

View raw version
#!/bin/rc -e
# Overlays a directory tree over another

fn usage {
	echo usage: $0 [-u] source target >[1=2]
	exit 'usage'
}

mode=bind

while(~ $1 -*) {
	switch($1) {
	case -u
		mode=unbind
	case *
		usage
	}
	shift
}

source=$1
target=$2

if(! test -d $source || ! test -d $target)
	usage

files=`{walk -f $source}
mtpts=`{{for(file in $files) basename -d $file} | sort | uniq | sed 's@^'$source@$target@g}

for(mtpt in $mtpts)
	switch($mode) {
	case bind
		if(test -d $mtpt)
			bind -cq $mtpt $mtpt || status=''
		if not { # This might not be desirable
			aux/stub -d $mtpt
			ramfs -m $mtpt
		}
	case unbind
		unmount $mtpt >/dev/null >[2=1] || status=''
	}

if(~ $mode bind)
	for(file in $files) {
		dest=`{echo $file | sed s@$source@$target@g}
		aux/stub $dest
		bind -c $file $dest
	}
if not
	status=''