#!/bin/bash

# 2013-08-19

data="$HOME/src/pce/local/dist"
out="$(pwd)/dist"

if test ! -d "$data" ; then
	echo "$0: no source directory ($data)" >&2
	exit 1
fi

print_header ()
{
	local text=$1
	local str

	str="--$text--------------------------------------------------------------------------------"

	echo
	echo "${str:0:78}"
}

copy_dir ()
{
	local dst=$1
	local src=$2
	local dir file

	if test ! -d "$src" ; then
		echo "| *** skipping directory ($src)"
		return
	fi

	echo "| copy dir $dst <- $src"

	find "$src" -type f -printf "%P\\n" |\
	while read file ; do
		dir=$(dirname "$file")
		if test ! -d "$dst/$dir" ; then
			mkdir -p "$dst/$dir"
		fi
		if test -e "$dst/$file" ; then
			rm -f "$dst/$file"
		fi
		echo "| copy d $dst/$file <- $src/$file"
		cp -a "$src/$file" "$dst/$file"
	done
}

# copy_file dst src
copy_file ()
{
	local dst=$1
	local src=$2

	if test ! -f "$src" ; then
		echo "| *** skipping file ($src)"
		continue
	fi

	bas=$(basename "$dst")

	if test ! -d "$bas" ; then
		mkdir -p "$bas"
	fi

	if test -e "$dst" ; then
		rm -f "$dst"
	fi

	echo "| copy f $dst <- $src"
	cp -a "$src" "$dst"
}

# copy_files dst-dir [src...]
copy_files ()
{
	local dst=$1
	local src bas

	shift 1

	if test ! -d "$dst" ; then
		mkdir -p "$dst"
	fi

	while test $# -gt 0 ; do
		src=$1
		shift 1

		if test ! -f "$src" ; then
			echo "| *** skipping file ($src)"
			continue
		fi

		bas=$(basename "$src")

		if test -e "$dst/$bas" ; then
			rm -f "$dst/$bas"
		fi

		echo "| copy f $dst/$bas <- $src"
		cp -a "$src" "$dst/$bas"
	done
}

# replace_file dst src
#
# Replace file dst with src if dst exists, otherwise do nothing.
#
replace_file ()
{
	local dst=$1
	local src=$2

	if test ! -f "$dst" ; then
		return
	fi

	echo "| copy f $dst <- $src"
	rm -f "$dst"
	cp -a "$src" "$dst"
}

remove_files ()
{
	local dst

	while test $# -gt 0 ; do
		dst=$1
		shift 1

		if test ! -f "$dst" ; then
			continue
		fi

		echo "| remove f $dst"
		rm -f "$dst"
	done
}

make_pdf ()
{
	local src

	while test $# -gt 0 ; do
		src=$1
		shift 1

		if test ! -f "$src" ; then
			continue
		fi

		echo "| make_pdf $src"

		mkpdf "$src" > /dev/null
	done
}

make_dos ()
{
	local src

	while test $# -gt 0 ; do
		src=$1
		shift 1

		if test ! -f "$src" ; then
			#echo "| *** skipping make_dos ($src)"
			continue
		fi

		echo "| make_dos $src"
		lnconv -e dos < "$src" > "$src.tmp"
		touch --reference="$src" "$src.tmp"
		chmod --reference="$src" "$src.tmp"
		mv -f "$src.tmp" "$src"
	done
}


# -linux----------------------------------------------------------------------

make_linux ()
{
	print_header "pce-$vers-linux"

	dst="$out/pce-$vers-linux"

	if test -d "$dst" ; then
		return 0
	fi

	cflags="-O2 -fomit-frame-pointer -pipe"

	{
		echo "# $(date "+%Y-%m-%d %T")"
		echo "\$ CFLAGS=\"$cflags\" ./configure --prefix=/usr/local"
		CFLAGS="$cflags" ./configure --prefix=/usr/local
	} 2>&1 | tee pce-configure.log

	make clean > /dev/null

	{
		echo "# $(date "+%Y-%m-%d %T")"
		echo "\$ make V=1"
		make V=1
		echo "\$ make V=1 man"
		make V=1 man
	} 2>&1 | tee pce-make.log

	{
		echo "# $(date "+%Y-%m-%d %T")"
		echo "\$ make V=1 prefix=$dst mandir=$dst/man install"
		make V=1 "prefix=$dst" "mandir=$dst/man" install
	} 2>&1 | tee pce-install.log

	strip -p "$dst/bin"/*

	for dir in "cas-utils" "ihex" "srec" ; do
		if test -d "contrib/$dir" ; then
			( cd "contrib/$dir" && make "CFLAGS=$cflags" "distdir=$dst/contrib/$dir" clean dist )
		fi
	done

	make_pdf "src/arch/atarist"/*.ps
	make_pdf "src/arch/ibmpc"/*.ps
	make_pdf "src/arch/rc759"/*.ps
	make_pdf "src/utils/pbit"/*.ps
	make_pdf "src/utils/pri"/*.ps
	make_pdf "src/utils/pce-img"/*.ps
	make_pdf "src/utils/pfdc"/*.ps
	make_pdf "src/utils/psi"/*.ps

	copy_dir   "$dst/doc/pce" "doc"
	copy_files "$dst/doc/pce" "AUTHORS" "COPYING" "ChangeLog" "NEWS" "README"
	copy_files "$dst/man/man1" "src/arch/atarist"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/arch/ibmpc"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/arch/rc759"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/pbit"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/pri"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/pce-img"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/pfdc"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/psi"/*.1{,.txt,.pdf}

	copy_files "$dst/log" "$0" "pce-configure.log" "pce-make.log" "pce-install.log"
}


# -win32----------------------------------------------------------------------

make_win32 ()
{
	print_header "pce-$vers-win32"

	dst="$out/pce-$vers-win32"

	if test -d "$dst" ; then
		return 0
	fi

	HOST=i686-mingw32

	export CC="$HOST-gcc"

	if test -d "/usr/cross/cc-$HOST/$HOST/bin" ; then
		PATH="/usr/cross/cc-$HOST/$HOST/bin:$PATH"
	elif test -d "/usr/local/cross/cc-$HOST/$HOST/bin" ; then
		PATH="/usr/local/cross/cc-$HOST/$HOST/bin:$PATH"
	fi
	export PATH

	cflags="-O2 -fomit-frame-pointer -pipe"

	{
		echo "# $(date "+%Y-%m-%d %T")"
		echo "\$ CFLAGS=\"$cflags\" ./configure --host=$HOST --prefix=/usr/local --without-x"
		CFLAGS="$cflags" ./configure "--host=$HOST" --prefix=/usr/local --without-x
	} 2>&1 | tee pce-configure.log

	make clean > /dev/null

	{
		echo "# $(date "+%Y-%m-%d %T")"
		echo "\$ make V=1"
		make V=1
		echo "\$ make V=1 man"
		make V=1 man
	} 2>&1 | tee pce-make.log

	{
		echo "# $(date "+%Y-%m-%d %T")"
		echo "\$ make V=1 prefix=$dst mandir=$dst/man install"
		make V=1 "prefix=$dst" "mandir=$dst/man" install
	} 2>&1 | tee pce-install.log

	for d in "/usr/local/bin" "/usr/cross/cc-$HOST/$HOST/bin" "/usr/local/cross/cc-$HOST/$HOST/bin" ; do
		if test -f "$d/SDL.dll" ; then
			cp -p "$d/SDL.dll" "$dst/bin/SDL.dll"
			break
		fi
	done

	copy_files "$dst/doc/pce" "$data/sdl/README-SDL.txt"

	$HOST-strip -p "$dst/bin"/*.exe
	$HOST-strip -p "$dst/bin"/*.dll

	for dir in "cas-utils" "ihex" "srec" ; do
		if test -d "contrib/$dir" ; then
			(
				cd "contrib/$dir"
				make "CROSS=$HOST-" EXEEXT=.exe "distdir=$dst/contrib/$dir" clean dist
				make_dos "$dst/contrib/$dir"/*.1
			)
		fi
	done

	copy_dir   "$dst/doc/pce" "doc"
	copy_files "$dst/doc/pce" "AUTHORS" "COPYING" "ChangeLog" "NEWS" "README"
	copy_files "$dst/man/man1" "src/arch/atarist"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/arch/ibmpc"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/arch/rc759"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/pbit"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/pri"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/pce-img"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/pfdc"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/psi"/*.1{,.txt,.pdf}

	copy_files "$dst/log" "$0" "pce-configure.log" "pce-make.log" "pce-install.log"

	make_dos "$dst/doc/pce"/*
	make_dos "$dst/etc/pce"/*
	make_dos "$dst/log"/*.log
	make_dos "$dst/man/man1"/*.1{,.txt}
}


# -machines ------------------------------------------------------------------

make_machines ()
{
	local s f b

	for s in "$data/machines"/* ; do
		if test ! -d "$s" ; then
			continue
		fi

		b=${s##*/}

		print_header "machines/pce-$vers-$b"

		w32="$out/pce-$vers-win32"
		dst="$out/machines/pce-$vers-$b"

		if test -d "$dst" ; then
			continue
		fi

		copy_dir   "$dst" "$s"
		copy_files "$dst/bin" "$w32/bin/SDL.dll"
		copy_files "$dst/bin" "$w32/bin/pbit.exe" "$w32/bin/pce-img.exe" "$w32/bin/pfdc.exe" "$w32/bin/psi.exe" "$w32/bin/pri.exe"
		copy_files "$dst/doc/man" "$w32/man/man1"/pbit.*
		copy_files "$dst/doc/man" "$w32/man/man1"/pce-img.*
		copy_files "$dst/doc/man" "$w32/man/man1"/pfdc.*
		copy_files "$dst/doc/man" "$w32/man/man1"/pri.*
		copy_files "$dst/doc/man" "$w32/man/man1"/psi.*

		ok=0
		if test -f "$dst/bin/pce-atarist.exe" ; then
			ok=1
			copy_files "$dst/bin" "$w32/bin/pce-atarist.exe"
			copy_files "$dst/doc/man" "$w32/man/man1"/pce-atarist.*
		fi
		if test -f "$dst/bin/pce-ibmpc.exe" ; then
			ok=1
			copy_files "$dst/bin" "$w32/bin/pce-ibmpc.exe"
			copy_files "$dst/doc/man" "$w32/man/man1"/pce-ibmpc.*
			copy_files "$dst/rom" "$w32/share/pce/ibmpc/ibmpc-pcex.rom"
		fi
		if test -f "$dst/bin/pce-macplus.exe" ; then
			ok=1
			copy_files "$dst/bin" "$w32/bin/pce-macplus.exe"
			copy_files "$dst/doc/man" "$w32/man/man1"/pce-macplus.*
			copy_files "$dst/rom" "$w32/share/pce/macplus/macplus-pcex.rom"
		fi
		if test -f "$dst/bin/pce-rc759.exe" ; then
			ok=1
			copy_files "$dst/bin" "$w32/bin/pce-rc759.exe"
			copy_files "$dst/doc/man" "$w32/man/man1"/pce-rc759.*
		fi
		if test $ok -eq 0 ; then
			copy_files "$dst/bin" "$w32/bin/pce-atarist.exe"
			copy_files "$dst/bin" "$w32/bin/pce-ibmpc.exe"
			copy_files "$dst/bin" "$w32/bin/pce-macplus.exe"
			copy_files "$dst/doc/man" "$w32/man/man1"/pce-ibmpc.*
			copy_files "$dst/doc/man" "$w32/man/man1"/pce-rc759.*
			copy_files "$dst/rom" "$w32/share/pce/ibmpc/ibmpc-pcex.rom"
			copy_files "$dst/rom" "$w32/share/pce/macplus/macplus-pcex.rom"
		fi

		copy_dir "$dst/doc" "$w32/doc/pce"

		remove_files "$dst"/*.sh

		# temporary hack
		for f in "$dst/doc/man"/*.1 ; do
			if test ! -s "$f" ; then
				rm -f "$f"
			fi
		done

		make_dos "$dst"/*.bat
		make_dos "$dst"/*.cfg
		make_dos "$dst"/*.txt
		make_dos "$dst"/READ*
		make_dos "$dst/doc"/*.txt
	done
}


# -macosx---------------------------------------------------------------------

make_macosx ()
{
	print_header "pce-$vers-macosx"

	dst="$out/pce-$vers-macosx"

	if test -d "$dst" ; then
		continue
	fi

	extra="--enable-static-sdl"
	cflags="-O2"
	lflags=""

	case "$(uname -r)" in
	7.9*)
		# MacOS X 10.3
		extra="$extra --disable-char-pty"
		lflags="$lflags -Wl,-bind_at_load"
		;;
	esac

	extra=${extra# }
	cflags=${cflags# }
	lflags=${lflags# }

	{
		echo "# $(date "+%Y-%m-%d %T")"
		echo
		echo "\$ uname -s -r -p"
		uname -s -r -p
		echo
		echo "\$ CFLAGS=\"$cflags\" LDFLAGS=\"$lflags\" ./configure $extra --prefix=/usr/local --mandir=/usr/local/man"
		CFLAGS="$cflags" LDFLAGS="$lflags" ./configure $extra --prefix=/usr/local --mandir=/usr/local/man
	} 2>&1 | tee pce-configure.log

	make clean > /dev/null

	{
		echo "# $(date "+%Y-%m-%d %T")"
		echo "\$ make V=1"
		make V=1
	} 2>&1 | tee pce-make.log

	{
		echo "# $(date "+%Y-%m-%d %T")"
		echo "\$ make V=1 prefix=$dst mandir=$dst/man install"
		make V=1 "prefix=$dst" "mandir=$dst/man" install
	} 2>&1 | tee pce-install.log

	strip "$dst/bin"/*

	for dir in "cas-utils" "ihex" "srec" ; do
		if test -d "contrib/$dir" ; then
			( cd "contrib/$dir" && make "CFLAGS=$cflags" "STRIP_OPT=" "distdir=$dst/contrib/$dir" clean dist )
		fi
	done

	copy_dir   "$dst/doc/pce" "doc"
	copy_files "$dst/doc/pce" "AUTHORS" "COPYING" "ChangeLog" "NEWS" "README"
	copy_files "$dst/man/man1" "src/arch/atarist"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/arch/ibmpc"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/pfdc"/*.1{,.txt,.pdf}
	copy_files "$dst/man/man1" "src/utils/psi"/*.1{,.txt,.pdf}

	copy_files "$dst/log" "$0" "pce-configure.log" "pce-make.log" "pce-install.log"
}


# -macosx-ibmpc---------------------------------------------------------------

make_macosx_ibmpc ()
{
	print_header "pce-$vers-ibmpc-macosx"

	src="$out/pce-$vers-macosx"
	dst="$out/pce-$vers-ibmpc-macosx"

	if test -d "$dst" ; then
		return 0
	fi

	#copy_dir   "$dst" "$data/images/ibmpc"
	copy_dir   "$dst" "$data/macosx/ibmpc"
	copy_files "$dst/pce-ibmpc.app/Contents/Resources" "$src/bin/pce-ibmpc"
	copy_dir   "$dst/pce-ibmpc.app/Contents/Resources/rom" "contrib/rom/ibmpc"
	copy_files "$dst/pce-ibmpc.app/Contents/Resources/rom" "$src/share/pce/ibmpc/ibmpc-pcex.rom"
	copy_files "$dst" "$src/etc/pce/pce-ibmpc.cfg"
}


# -macosx-macplus-------------------------------------------------------------

make_macosx_macplus ()
{
	print_header "pce-$vers-macplus-macosx"

	src="$out/pce-$vers-macosx"
	dst="$out/pce-$vers-macplus-macosx"

	if test -d "$dst" ; then
		return 0
	fi

	#copy_dir   "$dst" "$data/images/macplus"
	copy_dir   "$dst" "$data/macosx/macplus"
	copy_files "$dst/pce-macplus.app/Contents/Resources" "$src/bin/pce-macplus"
	copy_dir   "$dst/pce-macplus.app/Contents/Resources/rom" "contrib/rom/ibmpc"
	copy_files "$dst" "$src/etc/pce/pce-mac-classic.cfg"
	copy_files "$dst" "$src/etc/pce/pce-mac-plus.cfg"
	copy_files "$dst" "$src/etc/pce/pce-mac-se.cfg"
}


# ----------------------------------------------------------------------------

make_all ()
{
	if test ! -f version ; then
		echo "| *** version not found"
		exit 1
	fi

	read prj v1 v2 v3 vers rest < version

	case "$(uname -s)" in
	[Dd]arwin)
		make_macosx
		make_macosx_ibmpc
		make_macosx_macplus
		;;
	*)
		make_linux
		make_win32
		make_machines
		;;
	esac
}

export CCACHE=0

make_all 2>&1 | tee mkbindist.log
