#!/bin/sh -e
if test \! -d package || test \! -d bsd
then
	echo "You are not in the right directory." 1>&2
	exit 100
fi
if [ $# -lt 2 ]
then
	echo "usage: $0 name staging-dir" 1>&2
	exit 100
fi
if ! pkggencontrol="$(command -v pkg-gencontrol)"
then
	pkg-gencontrol() { bsd/tmp/command/pkg-gencontrol "$@" ; }
fi
gen_scripts() {
	local name="$1"
	local dir="$2"
	for j in pre- post-
	do
		for k in install deinstall upgrade
		do
			s="$j$k"
			if test -r bsd/"${name}"."$s"
			then
				install -m 0755 -p bsd/"${name}"."$s" "${dir}"/"$s"
			fi
		done
	done
}
gen_pkgng() {
	local name="$1"
	local dir="$2"
	local version="$(basename "$(pwd -P)" | sed -e 's/^.*-\([[:digit:]][[:alnum:].]*\)$/\1/')"
	local maintainer="`sed -n -e '/Maintainer:/s/^Maintainer://p' package/debian/control`"
	if test -r bsd/"${name}".epoch
	then
		version="${version},$(cat "bsd/${name}.epoch")"
	fi
	pkg-gencontrol -p "${name}" -P "${dir}" --prefix /usr/local --version "${version}" --comment "$(head -n 1 bsd/"${name}".comment)" --description "$(cat bsd/"${name}".desc)" --maintainer "${maintainer}" --origin prog/redo --www 'http://JdeBP.info./Softwares/redo/'
	(
		if test -r bsd/"${name}".deps
		then
			printf '%s: {\n' 'deps'
			while read k
			do
				pkg query "  %n: { version: \"%v\", origin: %o }" "$k" || true
			done < bsd/"${name}".deps
			printf '}\n'
		fi
		if test -r bsd/"${name}".conflicts
		then
			printf "%s\n" "# pkg issue #772 opened 2014-04-09"
			printf "%s: [\n" "conflicts_is_undocumented_and_broken"
			comma=""
			while read k
			do
				printf "%s %s" "${comma}" "$k"
				comma=","
			done < bsd/"${name}".conflicts
			printf ' ]\n'
		fi
		printf '%s: {\n' 'scripts'
		for j in pre- post-
		do
			for k in install deinstall upgrade
			do
				s="$j$k"
				if test -r bsd/"${name}"."$s"
				then
					printf "  %s: <<ENDOFTHETEXT\n" "$s"
					sed -e 's/^/    /' bsd/"${name}"."$s"
					printf "ENDOFTHETEXT\n"
				fi
			done
		done
		printf '}\n'
	) >> ${dir}/+MANIFEST
}
old_pkg() {
	local name="$1"
	local dir="$2"
	(
		printf "@%s %s\n" 'name' "${name}"
		cd "${dir}"
		printf '@%s %s\n' 'owner' 'root'
		printf '@%s %s\n' 'group' 'wheel'
		for j in *bin usr var etc run
		do
			test -d "$j" && find "$j" -type d -o -type f -o -type l
		done | while read k
		do
			printf '@%s %s\n' 'mode' "`stat -f '%Lp' \"$k\"`"
			if test -d "$k"
			then
				printf '@%s %s\n' 'dir' "$k"
			else
				printf '@%s %s\n' 'file' "$k"
			fi
		done
	) > ${dir}/+MANIFEST
	ln -f bsd/"${name}".comment ${dir}/+COMMENT
	ln -f bsd/"${name}".desc ${dir}/+DESC
}
gen_pkgsrc() {
	local name="$1"
	local dir="$2"
	(
		printf "@%s %s\n" 'name' "${name}"
		if test -r bsd/"${name}".deps
		then
			while read k
			do
				printf '@%s %s\n' 'pkgdep' "$k"
			done < bsd/"${name}".deps
		fi
		if test -r bsd/"${name}".conflicts
		then
			while read k
			do
				printf '@%s %s\n' 'pkgcfl' "$k"
			done < bsd/"${name}".conflicts
		fi
		cd "${dir}"
		printf '@%s %s\n' 'owner' 'root'
		printf '@%s %s\n' 'group' 'wheel'
		for j in *bin usr var etc run
		do
			test -d "$j" && find "$j" -type d -o -type f -o -type l
		done | while read k
		do
			! test -d "$k" || continue
			printf '@%s %s\n' 'mode' "`stat -f '%Lp' \"$k\"`"
			printf '%s\n' "$k"
		done
	) > ${dir}/+MANIFEST
	(
		printf "%s=%s\n" 'OPSYS' "`uname -s`"
		printf "%s=%s\n" 'MACHINE_ARCH' "`uname -p`"
		printf "%s=%s\n" 'OS_VERSION' "`uname -r`"
		if test -r build/cc
		then
			read -r cc < build/cc
			printf "%s=%s\n" 'CC_VERSION' "`"${cc}" --version|head -n 1`"
		fi
		if test -r build/ccflags
		then
			printf "%s=%s\n" 'CC_FLAGS' "`head -n 1 build/ccflags`"
		fi
		if test -r build/cxx
		then
			read -r cxx < build/cxx
			printf "%s=%s\n" 'CXX_VERSION' "`"${cxx}" --version|head -n 1`"
		fi
		if test -r build/cxxflags
		then
			printf "%s=%s\n" 'CXX_FLAGS' "`head -n 1 build/cxxflags`"
		fi
		if test -r build/cppflags
		then
			printf "%s=%s\n" 'CPP_FLAGS' "`head -n 1 build/cppflags`"
		fi
		if test -r build/ldflags
		then
			printf "%s=%s\n" 'LD_FLAGS' "`head -n 1 build/ldflags`"
		fi
	) > ${dir}/+BUILD_INFO
	: > ${dir}/+BUILD_VERSION
	ln -f bsd/"${name}".comment ${dir}/+COMMENT
	ln -f bsd/"${name}".desc ${dir}/+DESC
}
case "`uname`" in
(OpenBSD)	old_pkg "$1" "$2" ;;
(NetBSD)	gen_scripts "$1" "$2" ; gen_pkgsrc "$1" "$2" ;;
(FreeBSD)	gen_pkgng "$1" "$2" ;;
(*)		exec false ;;
esac
true
