#!/bin/sh

# Copyright (C) 2015 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3 as
# published by the Free Software Foundation.
#
# This program 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Authored by: Michi Henning <michi.henning@canonical.com>

#
# Script to generate debian files for dual landing in Vivid (gcc 4.9 ABI)
# and Wily and later (gcc 5 ABI).
#
# This script is called from debian/rules and generates:
#
# - control
# - libunity-scopes${soversion}.install
# - libunity-scopes${soversion}.scope.click-hook
# - shlibs.libunity-scopes-${full_version}.shlibs (for Wily and later only)
# - shlibs.libunity-scopes-qt-${qt_full_version}.shlibs (for Wily and later only)
# - libunity-scopes-qt${qt_soversion}.install
#
# For all but control, this is a straight substition and/or renaming exercise for each file.
# For control, if building on Wily or later, we also fix the "Replaces:" and "Conflicts:"
# entries, so we don't end up with two packages claiming ownership of the same places
# in the file system.
#
# Because the debian files for the different distributions are generated on the fly,
# this allows us to keep a single source tree for both distributions. See ../HACKING
# for more explanations.
#

set -e  # Fail if any command fails.

progname=$(basename $0)

[ $# -gt 1 ] && {
    echo "usage: $progname [path-to-debian-dir]" >&2
    exit 1
}
dir=$1
[ -n "$dir" ] || dir="./debian"

# Only call lsb_release if $SERIES isn't already set
[ -n "$SERIES" ] || SERIES=$(lsb_release -c -s)

export UNITY_SCOPES_MAJOR_VERSION=$(cut -d. -f1 "$dir/VERSION")
export UNITY_SCOPES_MINOR_VERSION=$(cut -d. -f2 "$dir/VERSION")
export UNITY_SCOPES_MICRO_VERSION=$(cut -d. -f3 "$dir/VERSION")

export UNITY_SCOPES_QT_MAJOR_VERSION=$(cut -d. -f1 "$dir/QT-VERSION")
export UNITY_SCOPES_QT_MINOR_VERSION=$(cut -d. -f2 "$dir/QT-VERSION")
export UNITY_SCOPES_QT_MICRO_VERSION=$(cut -d. -f3 "$dir/QT-VERSION")

export UNITY_SCOPES_MAJOR_MINOR="$UNITY_SCOPES_MAJOR_VERSION.$UNITY_SCOPES_MINOR_VERSION"
export UNITY_SCOPES_QT_MAJOR_MINOR="$UNITY_SCOPES_QT_MAJOR_VERSION.$UNITY_SCOPES_QT_MINOR_VERSION"

export UNITY_SCOPES_FULL_VERSION="$UNITY_SCOPES_MAJOR_MINOR.$UNITY_SCOPES_MICRO_VERSION"
export UNITY_SCOPES_QT_FULL_VERSION="$UNITY_SCOPES_QT_MAJOR_MINOR.$UNITY_SCOPES_QT_MICRO_VERSION"

export UNITY_SCOPES_VIVID_SOVERSION=$(expr "$UNITY_SCOPES_MINOR_VERSION" + 3)

if [ "$SERIES" = "vivid" ]
then
    export UNITY_SCOPES_SOVERSION="$UNITY_SCOPES_VIVID_SOVERSION"
    export UNITY_SCOPES_QT_SOVERSION="$UNITY_SCOPES_QT_MINOR_VERSION"
else
    export UNITY_SCOPES_SOVERSION="$UNITY_SCOPES_MAJOR_MINOR"
    export UNITY_SCOPES_QT_SOVERSION="$UNITY_SCOPES_QT_MAJOR_MINOR"
fi
[ -n "$UNITY_SCOPES_SOVERSION" ]
[ -n "$UNITY_SCOPES_QT_SOVERSION" ]


process()
{
    cat <<EOF
# This file is autogenerated. DO NOT EDIT!
#
# Modifications should be made to $(basename "$1") instead.
# This file is regenerated automatically by bileto.
#
EOF
    perl -pe 's/@([A-Z_]+)@/$ENV{$1} or die "$1 undefined"/eg' <"$1"
}

# Generate debian/control from debian/control.in, substituting the soversion for both libs.
# For wily onwards, we also add an entry for the vivid versions to "Conflicts:" and "Replaces:".

process "$dir/control.in" >"$dir/control"

[ "$SERIES" != "vivid" ] && {
    sed -i -e "/Replaces: libunity-scopes0,/a\
\          libunity-scopes${UNITY_SCOPES_VIVID_SOVERSION}," \
           -e "/Conflicts: libunity-scopes0,/a\
\           libunity-scopes${UNITY_SCOPES_VIVID_SOVERSION}," \
        "$dir/control"
}

# Generate the install files, naming them according to the soversion.

# Install file for binary package
process "$dir/libunity-scopes.install.in" \
    >"$dir/libunity-scopes${UNITY_SCOPES_SOVERSION}.install"

# Install file for click hook
process "$dir/libunity-scopes.scope.click-hook.in" \
    >"$dir/libunity-scopes${UNITY_SCOPES_SOVERSION}.scope.click-hook"

# Shlibs file
process "$dir/libunity-scopes.shlibs.in" \
    >"$dir/libunity-scopes-${UNITY_SCOPES_FULL_VERSION}.shlibs"

process "$dir/libunity-scopes-qt.shlibs.in" \
    >"$dir/libunity-scopes-qt${UNITY_SCOPES_QT_FULL_VERSION}.shlibs"

# Install file for qt binary package
process "$dir/libunity-scopes-qt.install.in" \
    >"$dir/libunity-scopes-qt${UNITY_SCOPES_QT_SOVERSION}.install"

exit 0
