#!/bin/sh

#
# Copyright (c) 2002-2003 Alexander Leidinger
# 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.
#


# $1 = destdir
# $2 = chown parameter
# $3 = source dir

dest=$1
user=$2
SRCDIR=$3

#in_cvs_list=$(grep -E '^(C|M) ' ${PORTSDIR}/update.log \
#	| cut -d ' ' -f 2 \
#	| cut -d / -f 1,2 \
#	| sort -u)

in_cvs_list="sbin/dump sbin/restore sys/kern/kern_exec.c usr.sbin/ntp \
	usr.sbin/adduser contrib/gcc"

[ ! -d ${dest} ] && mkdir -p ${dest}
rm ${dest}/*.diff

for i in ${in_cvs_list}; do
	patch=$(echo ${i} | sed 's@/@:@g').diff
	cd ${SRCDIR} && cvs -R diff -u ${i} > ${dest}/${patch}
	# remove if empty
	[ ! -s ${dest}/${patch} ] && rm ${dest}/${patch}
done


#no_cvs_list=$(grep -E '^\? .*patch-' ${PORTSDIR}/update.log \
#	| cut -d ' ' -f 2 \
#	| sort -u)

no_cvs_list=""

for i in ${no_cvs_list}; do
	file=$(echo ${i} | sed 's@/@:@g')
	cp ${SRCDIR}/${i} ${dest}/${file}
	# remove if empty
	[ ! -s ${dest}/${file} ] && rm ${dest}/${file}
done

icc_files=" \
	sys/dev/mcd/mcdreg.h			\
	sys/dev/scd/scdreg.h			\
	sys/i386/include/_types.h		\
	sys/i386/include/limits.h		\
	sys/i386/include/stdarg.h		\
	sys/i386/include/varargs.h		\
	\
	sys/alpha/alpha/busdma_machdep.c	\
	sys/alpha/include/atomic.h		\
	sys/alpha/include/cpufunc.h		\
	sys/alpha/include/endian.h		\
	sys/alpha/include/in_cksum.h		\
	sys/alpha/include/limits.h		\
	sys/alpha/include/stdarg.h		\
	sys/alpha/include/varargs.h		\
	sys/alpha/include/ioctl_bt848.h		\
	sys/alpha/include/ioctl_meteor.h	\
	sys/alpha/include/critical.h		\
	sys/alpha/include/_types.h		\
	sys/amd64/amd64/fpu.c			\
	sys/amd64/amd64/prof_machdep.c		\
	sys/amd64/include/_types.h		\
	sys/amd64/include/atomic.h		\
	sys/amd64/include/cpufunc.h		\
	sys/amd64/include/critical.h		\
	sys/amd64/include/endian.h		\
	sys/amd64/include/ieeefp.h		\
	sys/amd64/include/in_cksum.h		\
	sys/amd64/include/limits.h		\
	sys/amd64/include/pcpu.h		\
	sys/amd64/include/profile.h		\
	sys/amd64/include/stdarg.h		\
	sys/amd64/include/varargs.h		\
	sys/amd64/include/bus_amd64.h		\
	sys/arm/include/limits.h		\
	sys/arm/include/_types.h		\
	sys/arm/include/profile.h		\
	sys/arm/include/stdarg.h		\
	sys/arm/arm/busdma_machdep.c		\
	sys/boot/arc/include/arcfuncs.h		\
	sys/boot/efi/include/ia64/efibind.h	\
	sys/dev/isp/ispvar.h			\
	sys/dev/ichsmb/ichsmb.c			\
	sys/dev/iir/iir.h			\
	sys/dev/mpt/mpilib/mpi_type.h		\
	sys/dev/cx/machdep.h			\
	sys/dev/random/nehemiah.c		\
	sys/i386/acpica/acpi_wakeup.c		\
	sys/i386/include/atomic.h		\
	sys/i386/include/cpufunc.h		\
	sys/i386/include/endian.h		\
	sys/i386/include/ioctl_bt848.h		\
	sys/i386/include/pcpu.h			\
	sys/i386/include/ieeefp.h		\
	sys/i386/include/in_cksum.h		\
	sys/i386/include/ioctl_meteor.h		\
	sys/i386/include/bus_at386.h		\
	sys/i386/include/profile.h		\
	sys/i386/include/critical.h		\
	sys/i386/isa/npx.c			\
	sys/i386/isa/prof_machdep.c		\
	sys/i386/i386/db_interface.c		\
	sys/i386/i386/identcpu.c		\
	sys/i386/i386/in_cksum.c		\
	sys/ia64/ia64/busdma_machdep.c		\
	sys/ia64/include/cpufunc.h		\
	sys/ia64/include/endian.h		\
	sys/ia64/include/in_cksum.h		\
	sys/ia64/include/limits.h		\
	sys/ia64/include/stdarg.h		\
	sys/ia64/include/critical.h		\
	sys/ia64/include/_types.h		\
	sys/kern/subr_prof.c			\
	sys/powerpc/include/atomic.h		\
	sys/powerpc/include/endian.h		\
	sys/powerpc/include/limits.h		\
	sys/powerpc/include/stdarg.h		\
	sys/powerpc/include/varargs.h		\
	sys/powerpc/include/_types.h		\
	sys/powerpc/include/critical.h		\
	sys/powerpc/include/in_cksum.h		\
	sys/powerpc/powerpc/busdma_machdep.c	\
	sys/sparc64/include/endian.h		\
	sys/sparc64/include/stdarg.h		\
	sys/sparc64/include/varargs.h		\
	sys/sparc64/include/_types.h		\
	sys/sparc64/include/critical.h		\
	sys/sparc64/sparc64/iommu.c		\
	sys/sparc64/sparc64/bus_machdep.c	\
	sys/sys/dir.h				\
	sys/sys/ioctl.h				\
	sys/sys/linker_set.h			\
	sys/sys/queue.h				\
	sys/sys/syslimits.h			\
	\
	sys/sys/cdefs.h				\
"

pq_files=" \
	sys/vm	\
	sys/i386/i386/identcpu.c	\
	sys/amd64/amd64/identcpu.c	\
	sys/alpha/alpha/cpuconf.c	\
	sys/arm/arm/identcpu.c		\
	sys/ia64/ia64/machdep.c		\
	sys/powerpc/powerpc/machdep.c	\
	sys/sparc64/sparc64/identcpu.c	\
	sys/kern/vfs_bio.c		\
	sys/conf/NOTES			\
	sys/conf/options		\
"

snd="sys/compat/linux sys/dev/sound/pcm"

rest=" \
	etc/rc.d/localpkg		\
	sys/i386/i386/sys_machdep.c	\
	sys/kern/vfs_aio.c		\
	sys/netinet/ip_fw2.c		\
	sys/sys/ktr.h			\
"

cd ${SRCDIR} && cvs -R diff -u ${icc_files} > ${dest}/compiler_independence.diff
[ ! -s ${dest}/compiler_independence.diff ] && rm ${dest}/compiler_independence.diff

cd ${SRCDIR} && cvs -R diff -u ${pq_files} > ${dest}/pq.diff
[ ! -s ${dest}/pq.diff ] && rm ${dest}/pq.diff

cd ${SRCDIR} && cvs -R diff -u ${snd} > ${dest}/SNDCTL_DSP_SETDUPLEX.diff
[ ! -s ${dest}/SNDCTL_DSP_SETDUPLEX.diff ] && rm ${dest}/SNDCTL_DSP_SETDUPLEX.diff

cd ${SRCDIR} && cvs -R diff -u ${rest} > ${dest}/misc.diff
[ ! -s ${dest}/misc.diff ] && rm ${dest}/misc.diff

chmod -R a+r ${dest}
chown -R ${user} ${dest}

