#!/bin/bash
#
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
#
# Runs pocketlint on files changed from parent branch.


${shell-relative-path-setup}

utilitiesdir=${buildout:directory/utilities|shell-path}
[ -z "$utilitiesdir" ] && utilitiesdir=.


bzr() {
    # For pylint to operate properly, PYTHONPATH must point to the ./lib
    # directory in the launchpad tree. This directory includes a bzrlib. When
    # this script calls bzr, we want it to use the system bzrlib, not the one
    # in the launchpad tree.
    PYTHONPATH='' `which bzr` "$@"
}


if [ -z "$1" ]; then
    # No command line argument provided, use the default logic.
    bzr diff > /dev/null
    diff_status=$?
    if [ $diff_status -eq 0 ] ; then
        # No uncommitted changes in the tree.
        bzr status | grep "^Current thread:" > /dev/null
        if [ $? -eq 0 ] ; then
            # This is a loom, lint changes relative to the lower thread.
            rev_option="-r thread:"
        else
            if test "$(bzr pipes | sed -n -e "/^\\*/q;p" | wc -l)" -gt 0; then
                # This is a pipeline with at least one pipe before the
                # current, lint changes relative to the previous pipe
                rev_option="-r ancestor::prev"
            else
                # Lint changes relative to the parent.
                rev=`bzr info | sed \
                    '/parent branch:/!d; s/ *parent branch: /ancestor:/'`
                rev_option="-r $rev"
            fi
        fi
    elif [ $diff_status -eq 1 ] ; then
        # Uncommitted changes in the tree, lint those changes.
        rev_option=""
    else
        # bzr diff failed
        exit 1
    fi
    # Extract filename from status line.  Strip the @ that mark symlinks.
    files=`bzr st --short $rev_option |
        sed -e '/^.[MN]/!d; s/.* //' -e 's/@$//'`
else
    # Add newlines so grep filters out pyfiles correctly later.
    files=`echo $* | tr " " "\n"`
fi


echo "= Launchpad lint ="
echo ""
echo "Checking for conflicts and issues in changed files."

if [ -z "$files" ]; then
    echo "No changed files detected."
    exit 0
else
    echo
    echo "Linting changed files:"
    for file in $files; do
        echo "  $file"
    done
fi


# Are there patches to the schema or changes to current.sql?
sample_dir="database/sampledata"
current_sql="$sample_dir/current.sql"
current_dev_sql="$sample_dir/current-dev.sql"
lintdata_sql="$sample_dir/lintdata.sql"
lintdata_dev_sql="$sample_dir/lintdata-dev.sql"
database_changes=$(echo $files | sed '/database.*\(patch-\|current\)/!d')
if [ -n "$database_changes" ]; then
    make -C database/schema lintdata > /dev/null
    sql_diff=$(diff -q "$current_sql" "$lintdata_sql")
    if [ -z "$sql_diff" ]; then
        rm $lintdata_sql
    fi
    sql_dev_diff=$(diff -q "$current_dev_sql" "$lintdata_dev_sql")
    if [ -z "$sql_dev_diff" ]; then
        rm $lintdata_dev_sql
    fi
else
    sql_diff=""
    sql_dev_diff=""
fi

karma_bombs=`sed '/INTO karma /!d; /2000-/d; /2001-/d' $current_sql`

echo_sampledata_changes () {
    echo "    $2 differs from $1."
    echo "    Patches to the schema, or manual edits to $1"
    echo "    do not match the dump of the $3 database."
    echo "    If $2 is correct, copy it to"
    echo "    $1."
    echo "    Otherwise update $1 and run:"
    echo "        make schema"
    echo "        make newsampledata"
    echo "        cd $sample_dir"
    echo "        cp $4 $1"
    echo "    Run make schema again to update the test/dev database."
}

if [ -n "$sql_diff" -o -n "$sql_dev_diff" -o -n "$karma_bombs" ]; then
    echo ""
    echo ""
    echo "== Schema =="
    echo ""
fi

#
if [ -n "$sql_diff" -o -n "$karma_bombs" ]; then
    echo "$current_sql"
fi
if [ -n "$sql_diff" ]; then
    echo_sampledata_changes \
        "$current_sql" "$lintdata_sql" "launchpad_ftest_template"\
       	"newsampledata.sql"
fi
if [ -n "$karma_bombs" ]; then
    echo "    Karma time bombs were added to sampledata."
    echo "        The Karma table has dates after 2002-01-01; either revise"
    echo "        them or remove rows if they are unneeded."
fi

if [ -n "$sql_dev_diff" ]; then
    echo ""
    echo "$current_sql"
    echo_sampledata_changes \
        "$current_dev_sql" "$lintdata_dev_sql" "launchpad_dev_template"\
       	"newsampledata-dev.sql"
fi


# Sample data contains auto generated files with long lines.
pocketlint_files=`echo "$files" | grep -v "$sample_dir"`
if [ -z "$pocketlint_files" ]; then
    exit 0
fi

echo ""
pocketlint $pocketlint_files 2>&1
