#!/bin/sh
#-
# Copyright (c) 2002-2006 FreeBSD GNOME Team <freebsd-gnome@FreeBSD.org>
# Copyright (c) 2009 KDE FreeBSD Team <kde@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Based on marcusmerge, adapted for area51
#       by Martin Wilke <miwi@FreeBSD.org>
#
#

# Path to the xorg ports without the trailing /. 
KDEDIR="/usr/home/miwi/dev/miwiports/ports/branches/xorg-dev"
# Path to the official ports collection.
PORTSDIR="/usr/ports" 


# Merge UPDATING
#if [ -f ${KDEDIR}/UPDATING-area51 ]; then
#    cp ${KDEDIR}/UPDATING-area51 ${PORTSDIR}/
#fi

# Merge MK files
echo "===> Merging files from xorg to the ports directory"
echo "${KDEDIR} --> ${PORTSDIR}"

if [ -d ${KDEDIR}/Mk ]; then
    for mk in `ls -1 ${KDEDIR}/Mk/*.mk`; do
	cp ${mk} ${PORTSDIR}/Mk
    done
fi

#Merge Category and Ports

for categorypath in `find ${KDEDIR} -type d -depth 1`; do
 	category=$(basename "$categorypath")

  if [ ${category} = ".svn" ]; then
	continue
   fi
   for port in `ls -1 ${KDEDIR}/${category}`; do
	if [ ${port} = ".svn" ]; then
	    continue
	fi
	if [ ! -f ${KDEDIR}/${category}/${port}/Makefile ]; then
	fi
	if [ ! -d ${PORTSDIR}/${category} ]; then

	    mkdir -p ${PORTSDIR}/${category}
	fi
	if [ -d ${PORTSDIR}/${category}/${port} ]; then

	    rm -rf ${PORTSDIR}/${category}/${port}/work
	    find ${PORTSDIR}/${category}/${port} \! -path "*/.svn/*" -type f | \
	    	xargs rm -f
	fi

	cd ${KDEDIR}/${category}
	tar --exclude "*/.svn*" -cf - ${port} | \
		tar -xf - -C ${PORTSDIR}/${category}
    done
done

echo ""

echo "You can now run \"portupgrade -a\" or \"portmaster -a\" to upgrade your ports tree." | /usr/bin/fmt 75 79
