#!/bin/sh

# XXX devfs compatibility junk, see 30copy-dev
CDROMS=/root/cdroms
if [ -d $CDROMS ]; then
    mount -wo remount /dev
    cp -a $CDROMS /dev
    mount -ro remount /dev
fi

# Try to cache everything we're likely to need after ejecting.  This
# is fragile and simple-minded, but our options are limited.
cache_path() {
    path="$1"

    if [ -d "$path" ]; then
        find "$path" -type f | xargs cat > /dev/null 2>&1
    elif [ -f "$path" ]; then
        if [ -x "$path" ]; then
            if file "$path" | grep -q 'dynamically linked'; then
                for lib in $(ldd "$path" | awk '{ print $3 }'); do
                    cache_path "$lib"
                done
            fi
        fi
        cat "$path" >/dev/null 2>&1
    fi
}

for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default; do
    cache_path "$path"
done

eject -p -m /cdrom >/dev/null 2>&1

# XXX - i18n
echo -n "Please remove the disc, close the tray (if any) and press ENTER: "
read x

exit 0
