#!/bin/bash

skip=()

inst()
{
	src="$1"
	dest="$2"

	mkdir -p "${dest%/*}"
	rm -f "$dest"
	cp -Rp "$src" "$dest"
	echo "installed $src"
}

make
rm -fr skel
mkdir skel

git ls-files rc/ | while read f; do
	[ -n "${f##*.gitignore*}" ] || continue
	[ -e "$f" ] || continue
	for s in "${skip[@]}"; do
		[ "$s" = "$f" ] && break
	done
	[ "$s" = "$f" ] && continue
	inst "$f" "skel/.${f#*/}"
done

sys=`uname`

if [ -d opt/noarch ]; then
	mkdir -p skel/opt/noarch/misc/bin
	git ls-files opt/noarch | xargs -r cp -Rp -t skel/opt/noarch/misc/bin
fi

if [ -d opt/$sys/noarch ]; then
	mkdir -p skel/opt/noarch/$sys/bin
	git ls-files opt/$sys/noarch | xargs -r cp -Rp -t skel/opt/noarch/$sys/bin
fi

find skel -name .gitignore -print0 | xargs -r0 rm -f
find skel ! -type l -print0 | xargs -r0 chmod g-w

for h; do
	d="${h#*:}"
	[ "$d" != "$h" ] || d=.
	h="${h%%:*}"
	rsync -rltpv skel/ $h:${d:-.}/
done

