#!/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>
#              Thomas Voß <thomas.voss@canonical.com>
#              Robert Bruce Park <robert.park@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
# - libubuntu-location-service${soversion}.install.${target_arch}
#
# For all but control, this is a straight substition and/or renaming exercise for each file.
#

set -e  # Fail if any command fails.

progname=$(basename $0)

[ $# -gt 1 ] && {
    echo "usage: $progname [path-to-debian-dir]" >&2
    exit 1
}
dir=$1
version_dir=$(mktemp -d)

[ -n "$dir" ] || dir="./debian"
[ -n "$SERIES" ] || SERIES="$(lsb_release -c -s)"

# Dump version numbers into files and initialize vars from those files.
sh ${dir}/get-versions.sh ${dir} ${version_dir}

export UBUNTU_LOCATION_SERVICE_SOVERSION=$(cat $version_dir/libubuntu-location-service.soversion)

trap "rm -fr $version_dir" 0 INT TERM QUIT

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

process "$dir/control.in" >"$dir/control"
process "$dir/libubuntu-location-service.install.in" >"$dir/libubuntu-location-service${UBUNTU_LOCATION_SERVICE_SOVERSION}.install"
process "$dir/libubuntu-location-service.install.with-gps.in" >"$dir/libubuntu-location-service${UBUNTU_LOCATION_SERVICE_SOVERSION}.install.with-gps"
process "$dir/libubuntu-location-service-dev.install.in" >"$dir/libubuntu-location-service-dev.install"

ln -vsf "libubuntu-location-service${UBUNTU_LOCATION_SERVICE_SOVERSION}.install.with-gps" "$dir/libubuntu-location-service${UBUNTU_LOCATION_SERVICE_SOVERSION}.install.amd64"
ln -vsf "libubuntu-location-service${UBUNTU_LOCATION_SERVICE_SOVERSION}.install.with-gps" "$dir/libubuntu-location-service${UBUNTU_LOCATION_SERVICE_SOVERSION}.install.arm64"
ln -vsf "libubuntu-location-service${UBUNTU_LOCATION_SERVICE_SOVERSION}.install.with-gps" "$dir/libubuntu-location-service${UBUNTU_LOCATION_SERVICE_SOVERSION}.install.armhf"
ln -vsf "libubuntu-location-service${UBUNTU_LOCATION_SERVICE_SOVERSION}.install.with-gps" "$dir/libubuntu-location-service${UBUNTU_LOCATION_SERVICE_SOVERSION}.install.i386"

exit 0
