#!/bin/bash

# License: GPL
# (C) Peter Poeml <poeml@suse.de>

usage() {
	
	cat >&2 <<-EOF
	Usage: $(basename $0) [options] <device>
	
	All other parameters are passed through to dhcpcd.
	EOF

}

device=$1; shift
if [ -z "$device" ]; then 
	usage
	exit 1
fi

tmpdir=$(mktemp -d /var/tmp/dhcpcd.XXXXXX)
trap 'rm -rf $tmpdir' EXIT
infofile=$tmpdir/dhcpcd-$device.info
dhcpcd -Td -t 10 -NYRG -L $tmpdir -c /bin/true $device "$@"

pidfile=$tmpdir/dhcpcd-$device.pid


# dhcpcd keeps running, even if in test mode
test -f $pidfile && kill $(<$pidfile)

# make sure dhcpcd is really stopped
sleep 1
if test -f $pidfile; then
	logger -s -t dhcpcd-test dhcpcd does not seem to terminate on SIGTERM, sending SIGKILL
	ps l -p $(<$pidfile) 2>&1 | logger -s -t dhcpcd-test
	kill -KILL $(<$pidfile)
fi


if test -s $infofile; then
        cat $infofile
	exit 0
else
	exit 1
fi

