#!/bin/sh
# vim: set ts=4 sw=4 et:

#
# Copyright (c) 2008-2009, Novell, Inc.
# 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 <ORGANIZATION> nor the names of its 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 OWNER 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.
#
#
# (Licensed under the simplified BSD license)
#
# Authors: Vincent Untz <vuntz@opensuse.org>
#

basedir=`dirname $0`

## Options
# What's the current GNOME version in Factory
# Note: when moving to unstable, also remove the unneeded limits in upstream-limits.txt
GNOME_FACTORY_VERSION=stable


## Basic setup

CACHE_DIR=./cache
CONFIG_FILE=
LOG_FILE=

usage() {
    echo "Usage: $0 [-o CONF-FILE] [-l LOG-FILE]"
    echo ""
    echo "Options:"
    echo "   -o CONF-FILE     Use CONF-FILE as configuration file"
    echo "   -l LOG-FILE      Use LOG-FILE to log errors"
}

while getopts o:l:h option; do
    case $option in
    o) CONFIG_FILE=$OPTARG;;
    l) LOG_FILE=$OPTARG;;
    h|help) usage; exit 0;;
    *) usage; exit 1;;
    esac
done

if test "x$CONFIG_FILE" != "x"; then
    if test ! -f $CONFIG_FILE; then
        echo >&2 "Configuration file $CONFIG_FILE does not exit."
        exit 1
    else
        OBS_OPTIONS_CACHE_DIR=`grep "^ *cache-dir =" $CONFIG_FILE | sed "s/.*= *\(.*\) *$/\1/g" | tail -n 1`
        test "x$OBS_OPTIONS_CACHE_DIR" != "x" && CACHE_DIR=$OBS_OPTIONS_CACHE_DIR
    fi
fi

mkdir -p $CACHE_DIR

##############################################################
# Download latest upstream versions
# For non-GNOME:Factory, we only care about the official GNOME modules.

concatenate_all_versions () {
    DESTFILE=$CACHE_DIR/upstream/latest

    rm -f $DESTFILE.new

    for file in $CACHE_DIR/upstream/gnome-$GNOME_FACTORY_VERSION \
                $CACHE_DIR/upstream/gnome-$GNOME_FACTORY_VERSION-extras \
                $CACHE_DIR/upstream/upstream; do
        if test -f $file; then
            cat $file >> $DESTFILE.new
        fi
    done

    if test $? -ne 0; then
        echo "Error while creating the merged latest upstream versions file"
        return 1
    fi

    # we do everything above in a temporary file so that errors are safely
    # ignored, and so that we can compare the result (and keep the old file
    # with the old mtime if there's no change)
    cmp --quiet $DESTFILE.new $DESTFILE
    if test $? -ne 0; then
        mv $DESTFILE.new $DESTFILE
    else
        rm -f $DESTFILE.new
    fi
}

download_gnome_version () {
    VERSION=$1
    if test "x$1" = "x"; then
        return 1
    fi

    DESTFILE=$CACHE_DIR/upstream/gnome-$VERSION
    rm -f $DESTFILE.new

    wget -q -nc -O $DESTFILE.new http://www.gnome.org/~vuntz/tmp/versions/versions-$VERSION

    if test $? -ne 0; then
        echo "Error while checking for new GNOME upstream versions ($VERSION)"
        return 1
    fi

    # Don't use gstreamer from ftp.gnome.org -- it can be outdated
    sed -i "s/^\(desktop:gst-plugins.*\)$/# \1/g;s/^\(desktop:gstreamer:.*\)$/# \1/g" $DESTFILE.new
    # We don't care about mobile stuff
    sed -i "s/^\(mobile:.*\)$/# \1/g" $DESTFILE.new
    # Let's name the group fgo, instead of core, apps, extras, etc.
    sed -i "s/^[^#:][^:]*:/fgo:/g" $DESTFILE.new

    cmp --quiet $DESTFILE.new $DESTFILE
    if test $? -ne 0; then
        mv $DESTFILE.new $DESTFILE
    else
        rm -f $DESTFILE.new
    fi
}

download_cpan_version () {
    DESTFILE=$CACHE_DIR/upstream/cpan
    rm -f $DESTFILE.new

    # -a will keep the mtime
    test -f $DESTFILE && cp -a $DESTFILE $DESTFILE.new

    LOG_OPTION=
    if test "x$LOG_FILE" != "x"; then
        LOG_OPTION="--log $LOG_FILE"
    fi

    $basedir/download-cpan-versions $LOG_OPTION \
        --save-file=$DESTFILE.new \
        --only-if-old
    RETVAL=$?

    if test $RETVAL -eq 2; then
        # No update was done (old file was not old enough)
        rm -f $DESTFILE.new
        return 2
    fi

    if test $RETVAL -ne 0; then
        echo "Error while checking for new upstream versions on CPAN"
        rm -f $DESTFILE.new
        return 1
    fi

    sort -u $DESTFILE.new > $DESTFILE.new.sorted
    mv $DESTFILE.new.sorted $DESTFILE.new

    cmp --quiet $DESTFILE.new $DESTFILE
    if test $? -ne 0; then
        mv $DESTFILE.new $DESTFILE
    else
        rm -f $DESTFILE.new
    fi
}

download_pypi_version () {
    DESTFILE=$CACHE_DIR/upstream/pypi
    rm -f $DESTFILE.new

    # -a will keep the mtime
    test -f $DESTFILE && cp -a $DESTFILE $DESTFILE.new

    LOG_OPTION=
    if test "x$LOG_FILE" != "x"; then
        LOG_OPTION="--log $LOG_FILE"
    fi

    $basedir/download-pypi-versions $LOG_OPTION \
        --save-file=$DESTFILE.new \
        --only-if-old
    RETVAL=$?

    if test $RETVAL -eq 2; then
        # No update was done (old file was not old enough)
        rm -f $DESTFILE.new
        return 2
    fi

    if test $RETVAL -ne 0; then
        echo "Error while checking for new upstream versions on pypi"
        rm -f $DESTFILE.new
        return 1
    fi

    sort -u $DESTFILE.new > $DESTFILE.new.sorted
    mv $DESTFILE.new.sorted $DESTFILE.new

    cmp --quiet $DESTFILE.new $DESTFILE
    if test $? -ne 0; then
        mv $DESTFILE.new $DESTFILE
    else
        rm -f $DESTFILE.new
    fi
}

download_fallback_version () {
    DESTFILE=$CACHE_DIR/upstream/fallback
    rm -f $DESTFILE.new

    # -a will keep the mtime
    test -f $DESTFILE && cp -a $DESTFILE $DESTFILE.new

    LOG_OPTION=
    if test "x$LOG_FILE" != "x"; then
        LOG_OPTION="--log $LOG_FILE"
    fi

    $basedir/download-fallback-versions $LOG_OPTION \
        --save-file=$DESTFILE.new
    RETVAL=$?

    if test $RETVAL -eq 2; then
        # No update was done (old file was not old enough)
        rm -f $DESTFILE.new
        return 2
    fi

    if test $RETVAL -ne 0; then
        echo "Error while checking for fallback of new upstream versions"
        rm -f $DESTFILE.new
        return 1
    fi

    cmp --quiet $DESTFILE.new $DESTFILE
    if test $? -ne 0; then
        mv $DESTFILE.new $DESTFILE
    else
        rm -f $DESTFILE.new
    fi
}

download_upstream_version () {
    DESTFILE=$CACHE_DIR/upstream/upstream

    # -a will keep the mtime
    test -f $DESTFILE && cp -a $DESTFILE $DESTFILE.new

    LOG_OPTION=
    if test "x$LOG_FILE" != "x"; then
        LOG_OPTION="--log $LOG_FILE"
    fi

    $basedir/download-upstream-versions $LOG_OPTION \
        --upstream-tarballs=$basedir/upstream-tarballs.txt \
        --upstream-limits=$basedir/upstream-limits.txt \
        --save-file=$DESTFILE.new \
        --only-if-old --use-old-as-fallback
    RETVAL=$?

    if test $RETVAL -eq 2; then
        # No update was done (old file was not old enough)
        rm -f $DESTFILE.new
        return 2
    fi

    if test $RETVAL -ne 0; then
        echo "Error while checking for new upstream versions"
        rm -f $DESTFILE.new
        return 1
    fi

    cmp --quiet $DESTFILE.new $DESTFILE
    if test $? -ne 0; then
        mv $DESTFILE.new $DESTFILE
    else
        rm -f $DESTFILE.new
    fi
}

mkdir -p $CACHE_DIR/status
mkdir -p $CACHE_DIR/upstream

## Discontinued
# download_gnome_version 2.26
# download_gnome_version 2.28
# download_gnome_version 2.30
# download_gnome_version 2.32
# download_gnome_version 3.0
# download_gnome_version 3.2
# download_gnome_version 3.4
# download_gnome_version 3.6
# download_gnome_version 3.6-extras
# download_gnome_version 3.8
# download_gnome_version 3.8-extras

#download_gnome_version 3.12
#download_gnome_version 3.12-extras
# Disabled because of infrastructure change on GNOME servers
#download_gnome_version stable
#download_gnome_version unstable
#download_gnome_version stable-extras
#download_gnome_version unstable-extras

# Do this once, before the slow step
concatenate_all_versions

download_cpan_version
download_pypi_version
download_fallback_version

download_upstream_version
if test $? -eq 0; then
    concatenate_all_versions
fi

# Check that we have everything in the match database; we only do this once per
# day to avoid sending mails every X minutes.
MATCH_CHECK_TIMESTAMP=0
MATCH_CHECK_FILE="$CACHE_DIR/status/upstream-match-check"
if test -f "$MATCH_CHECK_FILE"; then
    MATCH_CHECK_TIMESTAMP=`stat --format="%Y" "$MATCH_CHECK_FILE"`
    MATCH_CHECK_TIMESTAMP=`echo "$MATCH_CHECK_TIMESTAMP + 24 * 3600" | bc`
fi
if test "$MATCH_CHECK_TIMESTAMP" -lt "`date +%s`"; then
    for i in `grep -v '^#' $CACHE_DIR/upstream/latest | grep ':' | cut -d ':' -f 2`; do
        re_i=`echo $i | sed 's/\+/\\\\\\+/g'`
        grep -q -E "^(# ?)?$re_i[:|]" $basedir/upstream-packages-match.txt
        if test $? -ne 0; then
            echo $i not in $basedir/upstream-packages-match.txt
        fi
    done
    echo "Last check for upstream match database completeness: `date --rfc-3339=seconds`" > "$MATCH_CHECK_FILE"
fi
