#!/bin/sh -efu

for mp; do
	if [ -e "$mp" ] && mountpoint -q "$mp"; then
		continue
	fi
	mkdir -p "$mp"
	mount --bind /mnt/9p/"$mp" "$mp"
done

for f in /etc/host.conf /etc/hosts /etc/resolv.conf; do
	path=/mnt/9p/"$f"
	if [ -r "$path" ]; then
		cat "$path" > "$f"
	fi
done

rpm --rebuilddb
if rpm -q hasher-priv >/dev/null 2>&1; then
	rpm --setperms hasher-priv
	rpm --setugids hasher-priv
fi >/dev/null 2>&1

gpasswd -a builder wheel
if [ -w /etc/sudoers ]; then
	control sudo wheelonly
	sed -E 's/^# (WHEEL_USERS ALL=\(ALL:ALL\) NOPASSWD: ALL)/\1/' -i /etc/sudoers
fi

echo 0 >/proc/sys/fs/protected_hardlinks

echo allowed_mountpoints=/proc >>/etc/hasher-priv/system
hasher-useradd builder >/dev/null 2>&1

mkdir -p /var/lock/subsys
service hasher-privd start >/dev/null 2>&1
