description="Client and server to keep the time synchronized between various computers over a network."
url="http://www.ntp.org/"

packager="tnut <tnut@nutyx.org>"
contributors="Pierre,Minzords,Spiky"

makedepends=(libcap perl-io-socket-ssl perl-net-ssleay)
run=(wget shadow)

name=ntp
version=4.2.8p15
release=4
runyx_version=20201011

source=(https://downloads.tuxfamily.org/nutyx/files/runyx-${runyx_version}.tar.xz
	https://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/$name-4.2/$name-$version.tar.gz)

prepare() {
UN=ntp
if ! (getent group $UN > /dev/null); then
   groupadd -g 87 $UN
fi
if ! (getent passwd $UN > /dev/null); then
   useradd  -c "Network Time Protocol" -d /var/lib/ntp \
   -g $UN -s /bin/false -u 87 $UN
fi
}
build() {

cd $name-$version

sed -e 's/"(\\S+)"/"?([^\\s"]+)"?/' \
    -i scripts/update-leap/update-leap.in

sed -e 's/#ifndef __sun/#if !defined(__sun) \&\& !defined(__GLIBC__)/' \
    -i libntp/work_thread.c

./configure --prefix=/usr \
--bindir=/usr/sbin \
--sysconfdir=/etc \
--mandir=/usr/share/man \
--infodir=/usr/share/info \
--enable-linuxcaps \
--with-lineeditlibs=readline \
--docdir=/usr/share/doc/$name-$version

make 
make DESTDIR=$PKG install
mkdir -p $PKG/var/lib/ntp
install -v -o ntp -g ntp -d $PKG/var/lib/ntp
mkdir -p $PKG/etc

cat > $PKG/etc/ntp.conf << "EOF"

# Europe
server 0.fr.pool.ntp.org
server 1.fr.pool.ntp.org
server 2.fr.pool.ntp.org
server 3.fr.pool.ntp.org
server 0.europe.pool.ntp.org


# Africa
server tock.nml.csir.co.za

# Asia
server 0.asia.pool.ntp.org

# Australia
server 0.oceania.pool.ntp.org

# North America
server 0.north-america.pool.ntp.org

# South America
server 2.south-america.pool.ntp.org

driftfile /var/lib/ntp/ntp.drift
pidfile   /var/run/ntpd.pid

# Security session
restrict    default limited kod nomodify notrap nopeer noquery
restrict -6 default limited kod nomodify notrap nopeer noquery

restrict 127.0.0.1
restrict ::1
EOF

# service
cd ../runyx-${runyx_version}
make DESTDIR=$PKG install-sysv-ntpd
make DESTDIR=$PKG install-sysd-ntpd
make DESTDIR=$PKG install-runyx-ntpd
}
service() {
cd $PKG
bsdtar -r -f \
$PKGMK_PACKAGE_DIR/${name}.${group}${PKGMK_BUILDVER}any.${PKGMK_PACKAGE_EXT} \
etc/rc.d etc/init lib/init lib/systemd
rm -r etc/rc.d etc/init lib/init lib/systemd
}
