#!/bin/bash
# 
# Copyright (C) 2009-2011 Roy Liu
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#   * 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.
#   * Neither the name of the author nor the names of any contributors may be
#     used to endorse or promote products derived from this software without
#     specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDERS 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.

if [[ ! -d "$1" ]] || [[ ! -d "$2" ]]; then

    echo "Please specify two directories." >&2
    exit 1
fi

WORK_DIR="patch"

if [[ -d "$WORK_DIR" ]]; then

    echo "Directory '$WORK_DIR' already exists. Exiting ..."
    exit 1
fi

SRC_DIR=$1
DST_DIR=$2

FILES_PATCH=()
FILES_ADD=()

rm -f patch.tgz

mkdir -p "$WORK_DIR/files-patch"
mkdir -p "$WORK_DIR/files-add"

while read RSYNC_CHANGE; do

    case "${RSYNC_CHANGE:0:1}" in

        ">")
            # Consider a file as patched if it's being transferred and changed.
            if [[ "${RSYNC_CHANGE:3:1}" == "s" ]]; then
                FILES_PATCH=("${FILES_PATCH[@]}" "${RSYNC_CHANGE:13}")
            fi
            ;;

        "*")
            # Consider a file as added if it's being deleted by the receiver and not a directory.
            if [[ "${RSYNC_CHANGE:1:8}" == "deleting" ]] && [[ ! -d "$DST_DIR/${RSYNC_CHANGE:12}" ]]; then
                FILES_ADD=("${FILES_ADD[@]}" "${RSYNC_CHANGE:12}")
            fi
            ;;

        *)
            ;;
    esac

done < <(rsync -r -i -n --delete --exclude=.DS_Store --exclude=.svn --exclude=/.git "$SRC_DIR"/ "$DST_DIR")

#

TMP_DIR=$(mktemp -d /tmp/gen_macports_patches.XXXXXX)

for FILE in "${FILES_PATCH[@]}"; do

    mkdir -p "$(dirname -- "$TMP_DIR/$FILE")"

    cp "$SRC_DIR/$FILE" "$TMP_DIR/$FILE.orig"
    cp "$DST_DIR/$FILE" "$TMP_DIR/$FILE"

    (cd "$TMP_DIR" && diff -u "$FILE.orig" "$FILE") >> "$WORK_DIR/files-patch/patch-$(basename -- "$FILE").diff"
done

rm -rf "$TMP_DIR"

#

for FILE in "${FILES_ADD[@]}"; do

    FILE_ADD_DIR=$(dirname -- "$WORK_DIR/files-add/$FILE")
    mkdir -p "$FILE_ADD_DIR"

    cp "$DST_DIR/$FILE" "$FILE_ADD_DIR"
done

# Create the patch archive.

cat <<EOF > "$WORK_DIR/README"
This is an autogenerated MacPorts patch archive. In the 'files-patch'
directory, you will find the directory hierarchy of files that require
patching. Analogously, the 'files-add' directory contains files not found in
the original package that may have to be added.
EOF

tar -C "$WORK_DIR/.." -c -z -f patch.tgz "$(basename -- "$WORK_DIR")"

rm -rf "$WORK_DIR"
