#!/bin/sh
# wrapper to make sure that a program (such as xlock) is only run on
# the local display

cut=/usr/bin/cut
sed=/usr/bin/sed
uname=/bin/uname
nslookup=/usr/sbin/nslookup

if [ -z "$DISPLAY" ]; then
    echo "DISPLAY not set!" 1>&2
    exit 1
fi

displayhost=`echo $DISPLAY | $cut -d: -f1`
displaynum=`echo $DISPLAY | $cut -d: -f2 | $cut -d. -f1`

thishost=`$uname -n`
if [ -n "$displayhost" ]; then
    # hostname part of DISPLAY was not null, so it may not be local

    # assume that only display 0 is local (because of ssh)
    if [ "$displaynum" -gt 0 ]; then
	echo "Cannot run $0 over remote ssh connection." 1>&2
	echo "Please run it on your local machine." 1>&2
	exit 1
    fi

    # get canonical names
    c_displayhost=`$nslookup $displayhost | $sed -ne '/^Name:/ s/Name: *//p'`
    c_thishost=`$nslookup $thishost | $sed -ne '/^Name:/ s/Name: *//p'`

    if [ "$c_displayhost" != "$c_thishost" ]; then
	echo "Cannot run $0 over remote X connection." 1>&2
	echo "Please run it on your local machine." 1>&2
	exit 1
    fi
fi

exec $0 "$@"
