#! /bin/sh
#
#
# Copyright 1998 - 2025 S. Varshavchik.  See COPYING for
# distribution information.
#
# Generic wrapper for makedat.
#
# Usage: makedat.sh -src=src -file=file -tmp=tmpfile -hup=hupfile [ -cidr ]
#
# Create [file] from [src].  [src] can be either a plain text file, or a
# directory. [tmpfile] is used.  if [hupfile] is specified and it exists,
# this file contains a PID, which is SIGHUPed.
#
# -cidr - [src] contains a list of IP netblocks.  Expand entries in [src]
#         that use CIDR or start-end notation using the Net::CIDR Perl
#         module.  Download the Net::CIDR module from
#         http://www.cpan.org/authors/id/M/MR/MRSAM/

prefix="/usr";
exec_prefix="${prefix}";

srcfile=""
dstfile=""
tmpfile=""
hupfile=""

usage() {
	echo "Usage: $0 -src=src -file=file -tmp=tmpfile -hup=hupfile [-cidr]" >&2
	exit 1
}

cidr=""

while test $# -gt 0
do
	case $1 in
	-cidr)
		cidr=1
		shift
		;;
	-src=*)
		srcfile=`echo "$1" | sed 's/-src=//'`;
		shift
		;;
	-tmp=*)
		tmpfile=`echo "$1" | sed 's/-tmp=//'`;
		shift
		;;
	-file=*)
		dstfile=`echo "$1" | sed 's/-file=//'`;
		shift
		;;
	-hup=*)
		hupfile=`echo "$1" | sed 's/-hup=//'`;
		shift
		;;
	*)
		usage
		;;
	esac
done

if test "$dstfile" = ""
then
	usage
fi

if test "$srcfile" = ""
then
	usage
fi

if test "$tmpfile" = ""
then
	usage
fi

FILE_LIST="$(mktemp /tmp/makedat.filelist.XXXXXX)" || exit 1
COMBINED_LIST="$(mktemp /tmp/makedat.combinedlist.XXXXXX)" || exit 1
trap 'rm -f $FILE_LIST $COMBINED_LIST' EXIT

get_access() {

	if test -d "$srcfile"
	then
	    find -L "$srcfile" -maxdepth 1 -type f -not -name "*~" -print >$FILE_LIST

	    exec <$FILE_LIST
	    while read F
	    do
		case "$F" in
		    *.dpkg-[a-z]*)
			OF="`echo \"$F\" | sed 's/.dpkg-[a-z]*$//'`"
			echo "$F found, please investigate and remove it, and" >&2
			echo "make any needed changes to $OF" >&2
			exit 1
			;;
		    *.rpmsave)
			OF="`echo \"$F\" | sed 's/.rpmsave$//'`"
			echo "$F found, please investigate and remove it, and" >&2
			echo "make any needed changes to $OF" >&2
			exit 1
			;;
		    *.rpmnew)
			OF="`echo \"$F\" | sed 's/.rpmnew$//'`"
			echo "$F found, please investigate and remove it, and" >&2
			OF="`echo \"$F\" | sed 's/.rpmnew$//'`" >&2
			exit 1
			;;
		    *.dist)
			OF="`echo \"$F\" | sed 's/.dist$//'`"
			if test ! -f "$OF"
			then
			    echo "$F exists, please copy to $OF" >&2
			    exit 1
			else
			    if "$OF" -ot "$F"
			    then
				echo "$F exists, please copy to $OF," >&2
				echo "or edit it to have a newer timestamp" >&2
				exit 1
			    fi
			fi
			continue
			;;
		esac
		/usr/sbin/cat "$F"
		echo
	    done
	    exec </dev/null
	else
	    /usr/sbin/cat "$srcfile" || return
	    echo
	fi
	echo "."
}

docidr() {
	if test "$cidr" = 1
	then
		/usr/sbin/perl -e '
			eval "use Net::CIDR;";

			while (<>)
			{
			    unless ( $Net::CIDR::VERSION &&
			     /^([a-fA-F0-9:\.]+[\-\/][a-fA-F0-9:\.]+)\s+(.*)$/)
			    {
				print;
				next;
			    }

			    my ($net, $line)=($1, $2);

			    foreach ( Net::CIDR::cidr2octets(
					Net::CIDR::range2cidr($net)))
			    {
				print ":" if $net =~ /:/;
				print "$_\t$line\n";
			    }
			}
		'
	else
		/usr/sbin/cat
	fi
}

get_access >$COMBINED_LIST || exit 1
docidr <$COMBINED_LIST | /usr/lib/courier/makedatprog - "$tmpfile" "$dstfile" || exit 1

if test "$hupfile" != ""
then
	if test -f $hupfile
	then
		kill -HUP `cat "$hupfile"`
	fi
fi
