#!/bin/sh
# Author: Jamie Strandboge <jamie@ubuntu.com>
# Copyright (C) 2012 Canonical Ltd.
#
# This script is distributed under the terms and conditions of the GNU General
# Public License, Version 2 or later. See http://www.gnu.org/copyleft/gpl.html
# for details.

set -e

db="$1"

if [ -z "$db" ]; then
    echo "Must specify a db. Eg 'database.pickle.bz2'" >&2
    exit 1
fi

for i in "$db" "$db".sha256 ; do
    echo "= Fetching $i ="
    # Fetch from people, instead of usn.ubuntu.com, because the version of the
    # USN database on usn.ubuntu.com isn't updated until the Jenkins deployment
    # job is executed to redeploy the site
    wget -N https://people.canonical.com/~ubuntu-security/usn/"$i"
done

echo "= Verifiying $i ="
sha256sum -c ./"$db".sha256

if echo "$db" | grep -q 'bz2$' ; then
    echo ""
    echo "= Uncompressing $db ="
    bzip2 -d -f -k ./"$db"
fi

echo "Download complete. File saved to '$(basename "$db" .bz2)'"
