#
# cygport build script for unison*
# by Andrew E. Schulman
#

# This script works for any of the Cygwin unison* packages:
#   unison2.27
#   unison2.32
#   unison2.40
#   unison2.45
#   unison2.48+4.04.2
#   unison2.48+4.08.1
#   unison2.49
#   unison2.51
# Just set the NAME, VERSION, and RELEASE.
#
# In general, this script should work for any Unison package whose name looks
# like
#   unison${UNISON_VERSION}
# or
#   unison${UNISON_VERSION}+${OCAML_VERSION}
# where UNISON_VERSION is the Unison main version number (first two numbers,
# e.g. 2.48), and OCAML_VERSION is the OCaml compiler version that's required
# to build it. If there's no OCAML_VERSION in the name, then the package will be
# built with the current default OCaml version in OPAM.

NAME=unison2.48+4.08.1
VERSION=2.48.4
RELEASE=1

# Packaging

SUMMARY="Synchronize collections of files on different hosts (text interface)"
DESCRIPTION="Unison is a file synchronizer for Unix and Windows. It allows two replicas of a
collection of files and directories to be stored on different hosts (or
different disks on the same host), modified separately, and then brought up to
date by propagating the changes in each replica to the other."
CATEGORY="Utils"
HOMEPAGE="https://www.cis.upenn.edu/~bcpierce/unison/"

# Sources

SRC_URI="
	https://www.cis.upenn.edu/~bcpierce/unison/download/releases/unison-${PV}/unison-${PV}.tar.gz
	https://www.cis.upenn.edu/~bcpierce/unison/download/releases/unison-${PV}/unison-${PV}-manual.html
"
#SRC_DIR=unison-${PV}
SRC_DIR=src

PATCH_URI=""
case $NAME in
	unison2.48+4.08.1) PATCH_URI+=" unison-2.48-Fix-compilation-with-OCaml-4.08.1.patch"
esac

# Build

DEPEND="ocaml flexdll opam"
UISTYLE=text	# console UI

if [[ $NAME =~ unison(.*)\+(.*) ]] ; then
	PUV=${BASH_REMATCH[1]}	# Unison version # (e.g. 2.48)
	POV=${BASH_REMATCH[2]}	# OCaml version # (e.g. 4.08.1)
	PEXE=unison-${PUV}+${POV}	# Name of unison executable
	OBSOLETES=unison${PUV}
else
	PUV=${PN#unison}
	POV=0.00.0
	PEXE=unison-${PUV}
fi

# Calculate PUP = package priority, for alternatives(8).
# For unisonA.BB+C.DD.E, the priority is an integer ABBBCDDE.
# E.g. for unison2.48+4.08.1, the priority is 20484081.

[[ "${PUV}+${POV}" =~ (.)\.(.+)\+(.)\.(.+)\.(.+) ]]
PUP=$(expr \
	  10000000 \* ${BASH_REMATCH[1]} \
	+    10000 \* ${BASH_REMATCH[2]} \
	+     1000 \* ${BASH_REMATCH[3]} \
	+       10 \* ${BASH_REMATCH[4]} \
	+             ${BASH_REMATCH[5]}
)

src_compile ()
{
	# Substitute Unison version information into generic Cygwin patch files
	cd "$S"
	set -v
	sed -i \
		-e "s:@PN@:${PN}:g" \
		-e "s:@PV@:${PV}:g" \
		-e "s:@PR@:${PR}:g" \
		-e "s:@P@:${P}:g" \
		-e "s:@PVR@:${PVR}:g" \
		-e "s:@PF@:${PF}:g" \
		-e "s:@PUV@:${PUV}:g" \
		-e "s:@PUP@:${PUP}:g" \
		-e "s:@PEXE@:${PEXE}:g" \
		CYGWIN-PATCHES/*
	set +v

	# Switch to the right OCaml version
	opam init -n
	opam_default_version=$(eval $(opam env --switch=default --set-switch) && ocaml -vnum)
	if [ $POV = 0.00.0 ] ; then
		switch=default
		ocaml_version=$opam_default_version
	elif opam switch list --short | grep -q "^${POV//./\\.}\$"; then
		switch=$POV
		ocaml_version=$POV
	elif [ $POV = $opam_default_version ] ; then
		switch=default
		ocaml_version=$opam_default_version
	else
		switch=$POV
		ocaml_version=$POV
		inform "Building OCaml $POV. This will take a while."
		opam switch create $POV
	fi
	inform "Switching to OCaml $ocaml_version"
	eval $(opam env --switch=$switch --set-switch)

	# April 2016: OCaml is broken OOTB in x86_64. Builds fail with the mysterious
	# error "flexdll error: cannot relocate RELOC_REL32, target is too far".
	# See https://cygwin.com/ml/cygwin-apps/2016-04/msg00049.html
	#
	# January 2018: This is fixed. No need for the statements below anymore,
	# but leaving them here for reference for now.
#	if [ "$ARCH" = x86_64 ] ; then
#		rebase -b 0x06440000 /usr/lib/ocaml/stublibs/dllunix.so
#		rebase -b 0x06510000 /usr/lib/ocaml/stublibs/dllcamlstr.so
#	fi

	cd "$B"
	lndirs
	unset NAME CFLAGS
	make OSARCH=Cygwin UISTYLE=$UISTYLE
}

src_install ()
{
	cd "${B}"

	newbin unison.exe ${PEXE}.exe

	dodoc \
		CONTRIB \
		INSTALL \
		INSTALL.gtk2 \
		INSTALL.win32 \
		INSTALL.win32-cygwin-gnuc \
		INSTALL.win32-msvc \
		ROADMAP.txt
	newdoc unison-${PV}-manual.html unison-manual.html

	dosym ../Cygwin/${PN}.README /usr/share/doc/${PN}/README.Cygwin
}
