#! /bin/sh -ex
#
# Copyright (C) 2004, 2007, 2012  Internet Systems Consortium, Inc. ("ISC")
# Copyright (C) 1999-2001  Internet Software Consortium.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

# $Id$

# XXX no command line options at all.  especially useful would be a way
# to specify the target directory on the NT machine, since it is needed
# in the Makefiles and otherwise config.status.win32 needs to be edited.

# XXX long term something different clearly needs to be done, so someone
# without any Unix boxes at all could download the source kit and compile.
# it'll happen; it has been a higher priority just to try to work
# on the code issues.

ZIP=bind9-nt.zip
DOZIP=false

case "$1" in
-nozip) DOZIP=false; shift ;;
-zip)   DOZIP=true ; shift ;;
esac

case $# in
0)
    dstdir=../bind9-nt
    rmtdir=g:/proj/bind9-nt
    ;;
1)
    dstdir=../bind9-nt
    rmtdir="$1"
    ;;
2)
    dstdir="$2"
    rmtdir="$1"
    ;;
*)
    echo "usage: $0 rmtdir [dstdir]" >&2
    exit 99
    ;;
esac

test -f version || {
    echo "$0: must be run in bind9 top level directory" >&2
    exit 1
}

set -- `ls -i version`
this_version=$1
if test -e "$dstdir/version"; then
    set -- `ls -i "$dstdir/version" 2>&1`
else
    set -- 0
fi
that_version=$1

# Try to make an effort to not wipe out current directory or non-bind directory
test "0$this_version" -eq "0$that_version" && {
    echo "$0: dstdir must not be current directory" >&2
    exit 1
}

test ! -d $dstdir -o "$that_version" -ne 0 || {
    echo "$0: existing dstdir must be a bind9 source directory" >&2
    exit 1
}

rm -rf $dstdir
mkdir $dstdir
tar cf - . | (cd $dstdir; tar xfp -)
cd $dstdir

# XXX Tale BSD/OS special
rm -f stdio.h

# Start with a clean slate.  This is not done in the source tree before
# the copy so as not to disturb things there.
./configure
make clean

# XXX header files that (currently) need to be built on Unix
##cd lib/isc
##make
##cd ../dns
##make code.h include/dns/enumtype.h include/dns/enumclass.h include/dns/rdatastruct.h
##cd ../..

# XXX Need a better way
perl -pi -e '$_ = "" if /chmod\(.*\)/' lib/dns/sec/dst/dst_parse.c

# XXX grrr.
perl -pi -e 'last if /^##confparser\.c/' lib/dns/config/Makefile.in

# Adjust Makefiles to have NT paths and nmake-compatible variable expansion.
set +x
echo doing Makefile substitutions
while read file type dates; do
        case $file in
                */Makefile.in|./make/includes.in|./version)
                        echo $file
                        perl -pi -e 'tr%{}/%()\\%;
                                     s%(-I.*)unix%$1win32%;
                                     s%(-I.*)pthreads.*?(\\?)$%$2%;
                                     s%-I ?%/I %g;
                                     s%-D%/D %g;
                                     s%(\$\(ARFLAGS\)) \$@%$1 /out:\$@%;
                                     s%\$\(UNIXOBJS\)%\$(WIN32OBJS)%;
                                     s%\$\(PTHREADOBJS\)%%;
                                     s%^(SUBDIRS.*)(pthreads)(.*)%$1$3%;
                                     s%^(SUBDIRS.*)(unix)(.*)%$1win32$3%;
                                     s%rm -f%-del /q /f%;
                                     s%rm -rf%-rd /q /s%;
                                     s%touch%copy NUL:%' $file
                        ;;
		./make/rules.in)
                        echo frobbing conditionals in $file

                        # /K is specified in MAKEDEFS *YUCK* because nmake
                        # for some BRAINDAMAGED REASON does not put K
                        # in MAKEFLAGS  (confirm with "nmake /PKN foo";
                        # only P and N are in Makeflags.)  However ... doing
                        # things this way ends up putting /K when you might not
                        # want it.  Thanks, Microsoft.
                        perl -pi -e \
                                'print "MAKEDEFS = /nologo /K\$(MAKEFLAGS)\n"
                                        if $. == 1;

                                 tr%{}/%()\\%;

                                 s%-I%/I %g;
                                 s%-D%/D %g;
                                 s%-c%/c %g;

                                 s/\.c\.o:/.c.obj:/;

                                 s%rm -f%-del /q /f%;
                                 s%rm -rf%-rd /q /s%;

                                 s/for\ i\ in\ \$\(ALL_SUBDIRS\).*
                                  /for %i in (\$(ALL_SUBDIRS)) do \\/x;

                                 s/if\ \[\ (\S+)\ !=\ "nulldir".*
                                  /\@if NOT $1=="nulldir" \\/x;

                                 s/^.*echo "making .* in .*\n//;

                                 s/\(cd\ (\S+);\ (.*)\);\ \\
                                  /pushd $1 && cd && $2 && popd || popd/x;

                                 s/\$\$i/%i/g;

                                 s/^\t+(fi|done).*\n//' $file
                        ;;
        esac
done < util/copyrights
set -x

# Make Makefiles
./config.status.win32 $rmtdir

# Copy win32 special files
cp config.h.win32 config.h

if $DOZIP; then
    dir="`basename $dstdir`"
    cd ..
    rm -f $ZIP
    # q = quiet
    # r = recurse
    # l = LF -> CRLF
    # 9 = highest compression
    zip -qrl9 $ZIP $dir
fi

exit 0
