#!/bin/sh 
#
# Prelink wrapper script
# Author: Andres Roldan <aroldan@debian.org>

# Recommended minimun free space, 50MB
min_size=50000

will_prelink="`cat /etc/prelink.conf | sed -ne 's/^-l \([a-z]*\)/\1/p' | cut -d/ -f -2 | sort | uniq`"
have_warn=0
scan_root=1

for i in $will_prelink; do
    size=`df -PlT 2>&1 | egrep "% $i$" | awk '{print $5}'`
    if [ -z $size ]; then
        continue
    fi
    
    scan_root=0
    
    if [ "$size" -le "$min_size" ]; then
	echo "Partition `df -PlT 2>&1 | awk '{print $1" "$5}' | egrep "\<$size\>" | awk '{print $1}'` has only $size KB free."
	have_warn=1
    fi
done

if [ "$scan_root" -eq "1" ]; then
    size=`df -PlT 2>&1 | egrep "% /$" | awk '{print $5}'`
    if [ "$size" -le "$min_size" ]; then
        echo "Partition `df -PlT 2>&1 | awk '{print $1" "$5}' | egrep "\<$size\>" | awk '{print $1}'` has only $size KB free."
        have_warn=1
    fi
fi

if [ "$have_warn" -eq "1" ]; then
    echo
    echo "!! WARNING !!"
    echo "It's recommended to have at least $min_size KB of disk space."
    echo "Prelink would _really_ damage the ELF files on those partitions."
    read -p "Do you really want to run prelink? (Yes/No): " answer
    
    if [ "$answer" = "Yes" ]; then
	echo "You were warned. Running prelink..."
	exec /usr/sbin/prelink.bin "$@"
    else
	echo "Aborting prelink."
	exit 0
    fi
fi

exec /usr/sbin/prelink.bin "$@"

