#Packager: George Vlahavas <vlahavas~at~gmail~dot~com>
#Previous packager: Thorsten Muehlfelder <thenktor(at)gmx.de>

pkgname=ntp
pkgver=4.2.6p5
pkgrel=1gv
source=("http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/$pkgname-$pkgver.tar.gz" "rc.ntpd" "ntp.nano.diff.gz")
dotnew=('etc/ntp.conf' 'etc/ntp/ntp.keys')
url="http://www.ntp.org/"

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"$pkgname (Network Time Protocol daemon)"
"The Network Time Protocol (NTP) is used to synchronize the time of a"
"computer client or server to another server or reference time source,"
"such as a radio or satellite receiver or modem. It provides client"
"accuracies typically within a millisecond on LANs and up to a few tens"
"of milliseconds on WANs relative to a primary server synchronized to"
"Coordinated Universal Time (UTC) via a Global Positioning Service"
"(GPS) receiver, for example."
)

build() {
	cd $startdir/src/${pkgname}-${pkgver}
	cat ../ntp.nano.diff | patch -p1 --verbose || return 1
	./configure --prefix=/usr --libdir=/usr/lib${LIBDIRSUFFIX} --localstatedir=/var --sysconfdir=/etc \
		--bindir=/usr/sbin \
		--sbindir=/usr/sbin \
		--mandir=/usr/man \
		--enable-shared \
		--disable-static \
		--enable-all-clocks \
		--enable-parse-clocks \
		--build=$ARCH-slackware-linux || return 1

	make -j $numjobs || return 1
	make DESTDIR=$startdir/pkg/ install

	# move all the usr/bin files to usr/sbin
	mv $startdir/pkg/usr/bin/* $startdir/pkg/usr/sbin
	rm -rfd $startdir/pkg/usr/bin

	# create config dirs
	mkdir -p $startdir/pkg/etc/rc.d/
	mkdir -p $startdir/pkg/etc/ntp/

	# install the rc file
	install -m 755 $startdir/src/rc.ntpd $startdir/pkg/etc/rc.d/rc.ntpd.new

	# create the doc dirs
	mkdir -p $startdir/pkg/usr/doc/${pkgname}-${pkgver}
	mkdir -p $startdir/pkg/usr/doc/${pkgname}-${pkgver}/util
	mkdir -p $startdir/pkg/usr/doc/${pkgname}-${pkgver}/ntpdate

	# copy the doc files
	cp -r COPYRIGHT ChangeLog* INSTALL NEWS README* TODO WHERE-TO-START *.y2kfixes clockstuff conf \
	$startdir/pkg/usr/doc/${pkgname}-${pkgver}
	cp -a util/README $startdir/pkg/usr/doc/${pkgname}-${pkgver}/util
	cp -a ntpdate/README $startdir/pkg/usr/doc/${pkgname}-${pkgver}/ntpdate

	# create the configuratio files
	cat << "EOC" > $startdir/pkg/etc/ntp.conf
# /etc/ntp.conf:  Configuration file for ntpd.

# External servers: The hostnames '*.pool.ntp.org' point to different
# servers each time, using a round robin system. If your ISP has an ntp
# server you want to replace these ones with it. It is not good
# practice to pick a 'stratum 1' server for the synchronisation of a
# single client, so don't put one here please. Read more about picking
# the right ntp servers to use at: http://www.pool.ntp.org/use.html
server 0.pool.ntp.org iburst
server 1.pool.ntp.org iburst
server 2.pool.ntp.org iburst

# Drift file.  Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
driftfile /etc/ntp/drift

# Access control:
# Default restrictions
# noquery: don't serve server stats
# nomodify: don't let anyone else change the settings remotely
# notrap: don't allow remote event logging
# noserve: deny all packets except ntpq and ntpdc queries
restrict default noquery nomodify notrap

# Allow everything locally.
restrict 127.0.0.1

# Serve time and stats to other computers in your local network.
restrict 10.0.0.0 mask 255.0.0.0 nomodify
restrict 169.254.0.0 mask 255.255.0.0 nomodify
restrict 172.16.0.0 mask 255.240.0.0 nomodify
restrict 192.168.0.0 mask 255.255.0.0 nomodify
EOC

	# create the ntp keys file
	cat << "EOC" > $startdir/pkg/etc/ntp/ntp.keys
65535	M	akey
1		M	pass
EOC

	# and the small rest...
	chmod 600 $startdir/pkg/etc/ntp/ntp.keys
}

doinst() {
	if [ -r etc/rc.d/rc.ntpd -a -r etc/rc.d/rc.ntpd.new ]; then
		chmod --reference=etc/rc.d/rc.ntpd etc/rc.d/rc.ntpd.new
	fi
	mv etc/rc.d/rc.ntpd.new etc/rc.d/rc.ntpd
}
