#!/usr/bin/ruby

require "qt2"
include Qt2

SERVERS = [ 192.31.216.38, "ntp.tcp-udp.net", 128.105.39.11, 128.105.37.11 ]

class DHCPCDApp < QApplication
	def initialize args
		super
		start = "Connect"
		start = "Disconnect" if File.exists? "/var/run/dhcpcd-eth0.pid"
		@button = QPushButton.new(start)
		setMainWidget(@button)
		connect( @button, QSIGNAL("clicked()"), self, "dhcpcd_handle()")
		@button.show
	end
	def dhcpcd_handle
		if @button.text.ascii == "Connect"
			@button.setText( "Disconnect" )
			`dhcpcd`
			`ntpdate #{SERVERS.join ' '}`
			`hwclock --systohc --utc`
		else
			@button.setText( "Connect" )
			pid = 0
			f = File.open("/var/run/dhcpcd-eth0.pid") { |fin| pid = fin.readline.to_i }
			`kill -HUP #{pid}` unless pid == 0
		end
	end
end

application = DHCPCDApp.new ["DHCPCD Control"]
application.exec
