#! /bin/bash
#
# mkrelease
#
# Create a release for distribution.
#
# usage: mkrelease [-i] [-f specfile] <version> [<component>...]
#

set -e

if [ "$1" = "" ] ; then
	echo "usage: $0 [-i] <version> [<component>...]"
	echo "       -i          - include internal tools"
	echo "       -f specfile - read component list from specfile"
	echo "       -o dir      - specify output directory"
	echo "       -c location - specify line-ending conversion utility (enables conversion)"
	echo "       -l          - include licensing"
	exit 1
fi

comps="Foundation Encodings XML JSON Util Net"
internal=0
specfile=""
version=""
output=""
lineEndConv=""
licensingDep=""

while [ "$1" != "" ] ;
do
	if [ "$1" = "-i" ] ; then
		shift
		internal=1
	elif [ "$1" = "-f" ] ; then
		shift
		specfile=$1
		shift
	elif [ "$1" = "-o" ] ; then
		shift
		output=$1
		shift
	elif [ "$1" = "-c" ] ; then
		shift
		lineEndConv=$1
		shift
	elif [ "$1" = "-l" ] ; then
		shift
		licensingDep="Licensing-libexec"
		comps="$comps Licensing"
	elif [ "$version" = "" ] ; then
		version=$1
		shift
	else
		comps="$comps $1"
		shift
	fi
done

if [ "$specfile" != "" ] ; then
	while read c
	do
		comps="$comps $c"
	done <$specfile
fi

if [ "$version" = "" ] ; then
	echo "Error: no version specified."
	exit 1
fi

if [ "$output" != "" ] ; then
	target=$output
else
	target=$POCO_BASE/releases/poco-${version}
fi

mkdir -p ${target}
mkdir -p ${target}/doc
mkdir -p ${target}/contrib
mkdir -p ${target}/patches
mkdir -p ${target}/cmake
mkdir -p ${target}/dependencies


#
# readme files, etc.
#
echo ${version} "(`date +%Y-%m-%d`)" >${target}/VERSION
cp ${POCO_BASE}/LICENSE ${target}
cp ${POCO_BASE}/README ${target}
cp ${POCO_BASE}/CHANGELOG ${target}
cp ${POCO_BASE}/CONTRIBUTORS ${target}
cp ${POCO_BASE}/DLLVersion.rc ${target}

cp ${POCO_BASE}/doc/Acknowledgements.html ${target}/doc
cp ${POCO_BASE}/doc/*.page ${target}/doc

cp -R ${POCO_BASE}/contrib/* ${target}/contrib
cp -R ${POCO_BASE}/patches/* ${target}/patches

cp -R ${POCO_BASE}/cmake/* ${target}/cmake
cp ${POCO_BASE}/CMakeLists.txt ${target}

#
# External dependencies (non-Poco source code)
#
cp -R ${POCO_BASE}/dependencies/* ${target}/dependencies


#
# build system
#
cp ${POCO_BASE}/libversion ${target}


#
# CppUnit
#
mkdir -p ${target}/CppUnit
mkdir -p ${target}/CppUnit/doc
mkdir -p ${target}/CppUnit/include/CppUnit
mkdir -p ${target}/CppUnit/src

cp ${POCO_BASE}/CppUnit/doc/* ${target}/CppUnit/doc >/dev/null 2>&1 || true
cp ${POCO_BASE}/CppUnit/include/CppUnit/* ${target}/CppUnit/include/CppUnit >/dev/null 2>&1 || true
cp ${POCO_BASE}/CppUnit/src/* ${target}/CppUnit/src >/dev/null 2>&1 || true
cp ${POCO_BASE}/CppUnit/Makefile ${target}/CppUnit >/dev/null 2>&1 || true
cp ${POCO_BASE}/CppUnit/CMakeLists.txt ${target}/CppUnit >/dev/null 2>&1 || true

#
# Copy components
#
for comp in $comps ;
do
	cpproj ${POCO_BASE}/$comp ${target}/$comp
done


#
# Create components file
#
echo "CppUnit" >${target}/components
for comp in $comps ;
do
	echo $comp >>${target}/components
done


#
# Make all files writeable
#
chmod -R +w ${target}


#
# Remove VS90 and progen
#
find ${target} -name '*.progen' -exec rm {} \;
find ${target} -iname '*_vs90.sln' -exec rm {} \;


#
# Generate CMake-forwarding Makefile
#
cat >${target}/Makefile <<'ENDOFSCRIPT'
#
# Makefile
#
# CMake-forwarding Makefile for POCO [generated by mkrelease]
#

.DEFAULT_GOAL := all

CMAKE_BUILD_DIR ?= cmake-build
CMAKE_BUILD_TYPE ?= RelWithDebInfo
CMAKE_GENERATOR ?= $(shell command -v ninja >/dev/null 2>&1 && echo "Ninja" || echo "Unix Makefiles")
CMAKE_OPTIONS ?=

.PHONY: all test install clean distclean help

help:
	@echo "POCO C++ Libraries - CMake Build"
	@echo ""
	@echo "Usage: make [target] [CMAKE_OPTIONS='-DENABLE_FOO=ON ...']"
	@echo ""
	@echo "Targets:"
	@echo "  all        - Configure and build (default)"
	@echo "  test       - Build and run tests"
	@echo "  install    - Install to CMAKE_INSTALL_PREFIX"
	@echo "  clean      - Remove build directory"

all:
	cmake -S. -B$(CMAKE_BUILD_DIR) -G"$(CMAKE_GENERATOR)" \
		-DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE) \
		$(CMAKE_OPTIONS)
	cmake --build $(CMAKE_BUILD_DIR) --parallel

test:
	cmake -S. -B$(CMAKE_BUILD_DIR) -G"$(CMAKE_GENERATOR)" \
		-DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE) \
		-DENABLE_TESTS=ON $(CMAKE_OPTIONS)
	cmake --build $(CMAKE_BUILD_DIR) --parallel
	cd $(CMAKE_BUILD_DIR) && ctest --output-on-failure

install: all
	cmake --install $(CMAKE_BUILD_DIR)

clean:
	rm -rf $(CMAKE_BUILD_DIR)

distclean: clean
ENDOFSCRIPT


#
# Create CMake build script for Windows
#
cat >${target}/build_cmake.cmd <<'ENDOFSCRIPT'
@echo off
rem POCO C++ Libraries - CMake Build Script
rem Usage: build_cmake [Release|Debug] [additional cmake options...]

setlocal
set BUILD_TYPE=%1
if "%BUILD_TYPE%"=="" set BUILD_TYPE=Release
shift

cmake -S. -Bcmake-build -DCMAKE_BUILD_TYPE=%BUILD_TYPE% %*
cmake --build cmake-build --config %BUILD_TYPE% --parallel
ENDOFSCRIPT


#
# Fix line endings
#
if [ "$lineEndConv" != "" ] ; then
	$lineEndConv ${target}/build_cmake.cmd
	$lineEndConv ${target}/Makefile
	$lineEndConv ${target}/components
fi


#
# Create .tar and .zip archives
#
if [ "$output" = "" ] ; then
	cd releases
	find ${target}/ -print | sed "s:^${target}/*::" >${target}/MANIFEST

	tar cf poco-${version}.tar poco-${version}
	gzip poco-${version}.tar

	tar cf poco-${version}.tar poco-${version}
	bzip2 poco-${version}.tar

	if [ -x /usr/bin/zip ] ; then
		/usr/bin/zip -r -q poco-${version}.zip poco-${version}
	fi
fi
