#! /bin/bash
#
# build-distribution
# Part of ComixCursors, a desktop cursor theme.
#
# Copyright © 2010 Ben Finney <ben+gnome@benfinney.id.au>
# Copyright © 2006–2010 Jens Luetkens <j.luetkens@hamburg.de>
#
# This work is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this work. If not, see <http://www.gnu.org/licenses/>.

# This script creates all distribution packages of ComixCursors from
# the sources. Run it as root from inside the source VCS working tree.
#
# Additional requirements:
# * Bazaar VCS program <http://bazaar.canonical.com/>
# * RPM package building tools <http://rpm.org/>

set -o errexit

bindir=bin

VERSION=$("${bindir}"/current-package-version)
export VERSION

themename_root="ComixCursors"
distdir="${PWD}/dist"

#
# start
#

printf "Packaging %s %s...\n" "$themename_root" $VERSION

workdir="$(mktemp -t -d)"

#
# source package
#
printf "Creating source package...\n"

make clean
srcname="${themename_root}-sources-${VERSION}"
srcdir="${workdir}/${srcname}"
mkdir --parents "${srcdir}"
bzr export "$srcdir"/

mkdir --parents "$distdir"
rm -rf "$distdir"/*

tarfile="${distdir}/${srcname}.tar.bz2"
tar -cjf "$tarfile" --exclude-vcs --directory "$workdir" "$srcname"/

#
# Now build the cursors for packaging.
#

printf "Installing cursor files...\n"

# Make a temporary directory for installing icons into.
ICONSDIR="${workdir}/icons"
export ICONSDIR
mkdir --parents "${ICONSDIR}"

./install-all

function package_variant {
    # Package the tarball for a specific variant of the cursors.
    local variant="$1"
    local SUMMARY="$2"

    if [ -n "$variant" ] ; then
        PACKAGENAME="${themename_root}-${variant}"
    else
        PACKAGENAME="${themename_root}"
    fi

    printf "Creating cursors package %s...\n" "$PACKAGENAME"

    # Now it's important that the variants get processed in an
    # "reverse" order, so only directories matching package name get
    # moved and packaged.

    packagedir="${workdir}/${PACKAGENAME}"
    mkdir --parents "$packagedir"
    mv "${ICONSDIR}/${PACKAGENAME}"* "$packagedir"/.

    tarfile="${distdir}/${PACKAGENAME}-${VERSION}.tar.bz2"
    tar -cjf "$tarfile" --directory "$packagedir" --files-from <(
        cd "$packagedir"
        ls
    )

    #
    # RPM packages
    #
    rpmdir="/usr/src/packages"
    if [ -d "$rpmdir" ] ; then
        printf "Creating RPM package...\n"
        specfilename="${PACKAGENAME}.spec"
        specfile="${rpmdir}/SPECS/${specfilename}"
        rpmsourcesdir="${rpmdir}/SOURCES"
        export PACKAGENAME SUMMARY
        make "$specfilename"
        cp "$specfilename" "$specfile"
        cp "$tarfile" "${rpmsourcesdir}"/.
        (
            cd "$rpmsourcesdir"
            rpmbuild -bb "$specfile"
        )
        mv "${rpmdir}/RPMS/noarch/${PACKAGENAME}"* "$distdir"/.
    else
        printf "Directory $rpmdir not found, skipping RPM packaging.\n"
    fi
}

package_variant "LH-Opaque" "The opaque left-handed Comix Cursors"
package_variant "LH" "The left-handed Comix Cursors"
package_variant "Opaque" "The opaque Comix Cursors"
package_variant "" "The original Comix Cursors"

printf "Cleaning up temporary working area...\n"
rm -r "$workdir"

printf "Done.\n"
