#!/bin/bash
#
# Copyright 2015 National Instruments Corp.
#
# This program 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 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

if ! ls | grep host > /dev/null
then
    echo "This script must be run from UHD's top-level directory."
    exit 1
fi

if [ -n "$(git status --porcelain --untracked-files=all)" ]; then
    echo "This script requires a clean repository!"
    exit 1
fi

FORCE_YES=0
if [ $# -eq 1 ]
then
    if [ "$1" = "-y" ]
    then
        FORCE_YES=1
    fi
fi

UHD_TOP_LEVEL=$PWD

# Get version info
VERSION=`head -1 host/cmake/debian/changelog | sed 's/.*(\(.*\)-0ubuntu.*/\1/'`
ORIG_RELEASE=`head -1 host/cmake/debian/changelog | sed 's/.*) \(.*\);.*/\1/'`

#
# Currently supported versions can be found here:
# https://launchpad.net/ubuntu/+ppas
#
RELEASES="focal jammy noble questing"
PPA=ppa:ettusresearch/uhd

#
# Make sure this is the intended version.
#
# This is based on the contents of debian/changelog. If convert_changelog.py was not
# run on this version, it will show the previous release.
#
echo "Will generate installer configuration files for UHD "$VERSION
if [ $FORCE_YES -ne 1 ]
then
    echo "Is this correct? (yes/no)"
    read response
    if [ "$response" != "yes" ]
    then
        exit 0
    fi
fi

# debuild expects our directory name to be ${source package}-${version}, so
# copy files to a directory with that name excluding files that cause problems
# or are unnecessary for Debian builds
rm -rf ${UHD_TOP_LEVEL}/../uhd-${VERSION}
mkdir ${UHD_TOP_LEVEL}/../uhd-${VERSION}
rsync --exclude='.git' --exclude='.ci' --exclude='.git*' --exclude='.clang*' --exclude='/debian/' --exclude='*.swp' --exclude='/fpga-src/' --exclude='/build' --exclude='/images/*.pyc' --exclude='/images/uhd-*' --exclude='tags' --exclude='/host/cmake/msvc/' --exclude='/host/cmake/vcpkg/' --exclude='/fpga/usrp1' --exclude='/fpga/usrp2' --exclude='/fpga/usrp3/top/b200' --exclude='/fpga/usrp3/top/b2xxmini' --exclude='/fpga/usrp3/lib/*_200' -a  ${UHD_TOP_LEVEL}/ ${UHD_TOP_LEVEL}/../uhd-${VERSION}/
if [ $? != 0 ]
then
    echo "Failed to copy UHD source."
    exit 1
fi
cd ${UHD_TOP_LEVEL}/../uhd-${VERSION}

# Generate the TAR file to be uploaded.
echo "Creating UHD source archive."
tar -cJf ../uhd_${VERSION}.orig.tar.xz .
if [ $? != 0 ]
then
    echo "Failed to create UHD source archive."
    exit 1
fi

#
# Generate package info for each version.
#
# This script substitutes each version string into the debian/changelog file to
# create package info for each version. We need to store the original outside the
# UHD repo, or dpkg-source will detect the change and error out.
#
cp -r host/cmake/debian .
cp host/utils/uhd-usrp.rules debian/uhd-host.udev
find host/docs -name '*.1' > debian/uhd-host.manpages
rm -f debian/postinst.in debian/postrm.in debian/preinst.in debian/prerm.in

if [ $FORCE_YES -ne 1 ]
then
    echo "Proceed to generate package info? (yes/no)"
    read response
    if [ "$response" != "yes" ]
    then
        exit 0
    fi
fi

for RELEASE in ${RELEASES}
do
    cp debian/changelog ../changelog.backup
    sed -i "s/${ORIG_RELEASE}/${RELEASE}/;s/0ubuntu1/0ubuntu1~${RELEASE}1/" debian/changelog
    debuild -S -i -sa
    if [ $? != 0 ]
    then
        echo "Failed to generate package info for" ${RELEASE}
        mv ../changelog.backup debian/changelog
        exit 1
    fi
    mv ../changelog.backup debian/changelog
done

if [ $FORCE_YES -ne 1 ]
then
    echo "Proceed to upload to launchpad? (yes/no)"
    read response
    if [ "$response" != "yes" ]
    then
        exit 0
    fi
fi

# Upload package into to Launchpad, which will automatically build packages
for RELEASE in ${RELEASES}
do
    dput ${PPA} ../uhd_${VERSION}-0ubuntu1~${RELEASE}1_source.changes
    if [ $? != 0 ]
    then
        echo "Failed to upload" ${VERSION} "package info to Launchpad."
	    exit 1
    fi
done

if [ $FORCE_YES -ne 1 ]
then
    echo
    echo "Clean up build artifacts? (yes/no)"
    read response
    if [ "$response" = "yes" ]
    then
        cd ..
        rm -r uhd-${VERSION} uhd_${VERSION}.orig.tar.xz uhd*dsc uhd*changes uhd*debian.tar.* uhd*_source.build uhd*.upload
    fi
fi
