#!/bin/bash

TAGS_URL=svn://trac.rezo.net/spip/tags

OLD=3.0.21
NEW=3.1.0

function diff_print_deleted_files {
    local dir=""
    if [[ $3 ]]; then
        dir=$3"/"
    fi
    svn diff --summarize \
        --old=$1 \
        --new=$2 | \
        egrep "^D" | \
        sed -e "s#^D *$1\/#$dir#"
}

function print_directories {
    while (( "$#" )); do
        is_dir=`echo $1 | egrep -v "svn://"`
        if [[ $is_dir ]]; then
            echo $1
        fi
        shift
    done
}

function find_url {
    fisrt=0
    DIR=$1
    TMP_URL=""
    shift
    while (( "$#" )); do
        is_dir=`echo $1 | egrep -v "svn://"`
        if [[ $fisrt -eq 0 ]]; then
            if [[ $is_dir ]]; then
                fisrt=1
            else
                fisrt=2
            fi
        fi
        if [[ $is_dir ]]; then
            if [[ "$DIR" == "$1" ]]; then
                if [[ $fisrt -eq 1 ]]; then
                    shift
                    echo $1
                else
                    echo $TMP_URL
                fi
            fi
        else
            if [[ $fisrt -eq 2 ]]; then
                TMP_URL=$1
            fi
        fi
        shift
    done
}

function print_delete_plugins {
    for dir in `print_directories $OLD_PLUGINS`
    do
        local dir_to_delete=`echo $NEW_PLUGIN_DIRECTORIES | grep -v $dir`
        if [[ $dir_to_delete ]]; then
            echo $dir"/*"
        else
            OLD_PLUGIN_URL=`find_url $dir $OLD_PLUGINS`
            NEW_PLUGIN_URL=`find_url $dir $NEW_PLUGINS`
            diff_print_deleted_files $OLD_PLUGIN_URL $NEW_PLUGIN_URL $dir
        fi
    done
}

OLD_PLUGINS=`svn propget svn:externals ${TAGS_URL}/spip-$OLD`
NEW_PLUGINS=`svn propget svn:externals ${TAGS_URL}/spip-$NEW`
NEW_PLUGIN_DIRECTORIES=`print_directories $NEW_PLUGINS`

print_delete_plugins
diff_print_deleted_files ${TAGS_URL}/spip-$OLD ${TAGS_URL}/spip-$NEW