#!/bin/bash

# Set ABUILD_UID and ABUILD_GID from saved data
. /.build/build.data

# Xen needs the / remounted rw in SB2
if [[ -e /proc/sys/xen/independent_wallclock ]]; then
    mount -o remount,rw -t ext4 /dev/hda1 /
fi

# Workaround for bug https://bugs.merproject.org/show_bug.cgi?id=555
sed -i '/^abuild:/d' /etc/passwd

echo "Setting up abuild user in $BUILD_ROOT for SB2"
echo "abuild::${ABUILD_UID}:${ABUILD_GID}:Autobuild:/home/abuild:/bin/bash" >> /etc/passwd
echo 'abuild:*:::::::' >>/etc/shadow # This is needed on Mandriva 2009
echo 'abuild:*::' >>/etc/gshadow # This is needed on Ubuntu
echo "abuild::${ABUILD_GID}:" >>/etc/group
mkdir -p /target /home/abuild
chmod 775 /target
chown -R "$ABUILD_UID:$ABUILD_GID" /target /home/abuild
if [ -e /home/abuild/sb2-session-needroot ]; then
	rm -rf /home/abuild/sb2-session-needroot
fi
if [ -e /home/abuild/sb2-session-needuser ]; then
	rm -rf /home/abuild/sb2-session-needuser
fi
if [ -e /target/.sb2inited ]; then
	rm -rf /target/.sb2inited
fi
unset BUILD_DIR BUILD_ROOT
exec /.build/build
