# Get the NBD device path to detect NBD/NFS setups
nbd_device=$(cat /proc/mounts|grep /dev/nbd|cut -d " " -f1) 2>/dev/null
if [ -n "$nbd_device" ]; then

    #Find the NBD process and guess the server and port
    nbd_pid=$(pgrep -f $nbd_device)
    nbd_command=$(ps -o cmd $nbd_pid|grep -v CMD)
    nbd_server=$(echo $nbd_command | cut -d " " -f2)
    nbd_port=$(echo $nbd_command | cut -d " " -f3)

    #Connect and get the squashfs header (contains the creation date)
    nbd-client $nbd_server $nbd_port /dev/nbd9
    NEW=$(dd if=/dev/nbd9 of=/dev/stdout bs=64 count=1 2> /dev/zero | sha1sum -)
    nbd-client -d /dev/nbd9
else
    # It's NFS, so just return
    return
fi

#Get the current squashfs information
CURRENT=$(dd if=$nbd_device of=/dev/stdout bs=64 count=1 2> /dev/zero | sha1sum -)

#Exit if we're already running on the latest squashfs
[ "$NEW" = "$CURRENT" ] && return

(
for i in $(seq 10 10 100);
do
    echo $i
    sleep 1s
done
) | ldm-dialog --progress --auto-close "`eval_gettext "A new version of the system is available, rebooting in 10s."`"

if [ "$?" = "1" ]; then
    return
else
    reboot
fi
