#!/usr/bin/env bash
# ==========================================================================
#              ____        _ _     _     _____           _
#             | __ ) _   _(_) | __| |   |_   _|__   ___ | |___
#             |  _ \| | | | | |/ _` |_____| |/ _ \ / _ \| / __|
#             | |_) | |_| | | | (_| |_____| | (_) | (_) | \__ \
#             |____/ \__,_|_|_|\__,_|     |_|\___/ \___/|_|___/
#
#                           --- Build-Tools ---
#                https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# Packaging Scripts
# Copyright (C) 2017-2026 by Thomas Dreibholz
#
# 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/>.
#
# Contact: thomas.dreibholz@gmail.com

# Bash options:
set -eu


# ====== Handle arguments ===================================================
if [ $# -lt 1 ] ; then
   echo >&2 "Usage: $0 lsm_file_directory"
   exit 1
fi
LSM_DIRECTORY="$1"


# ====== Get package configuration ==========================================
CHANGELOG_HEADER="`head -n1 debian/changelog`"

# The package name, e.g. MyApplication
PACKAGE=`echo ${CHANGELOG_HEADER} | sed -e "s/(.*//" -e "s/ //g"`
# The package distribution, e.g. precise, raring, ...
PACKAGE_DISTRIBUTION=`echo ${CHANGELOG_HEADER} | sed -e "s/[^)]*)//" -e "s/;.*//g" -e "s/ //g"`
# The package's version, e.g. 1.2.3-1ubuntu1
PACKAGE_VERSION=`echo ${CHANGELOG_HEADER} | sed -e "s/.*(//" -e "s/).*//" -e "s/ //g" -e "s/ //g" -e "s/^[0-9]://g"`
# The package's output version, e.g. 1.2.3-1ubuntu
OUTPUT_VERSION=`echo ${PACKAGE_VERSION}   | sed -e "s/\(ubuntu\|ppa\)[0-9]*$/\1/"`
# The package's Debian version, e.g. 1.2.3-1
DEBIAN_VERSION=`echo ${OUTPUT_VERSION}    | sed -e "s/\(ubuntu\|ppa\)$//1"`
# The package's upstream version, e.g. 1.2.3
UPSTREAM_VERSION=`echo ${DEBIAN_VERSION}  | sed -e "s/-[0-9]*$//"`
# The package's plain upstream version, e.g. 1.2.3 (without e.g. ~svn<xxxx>)
PLAIN_VERSION=`echo ${UPSTREAM_VERSION}   | sed -e "s/\([0-9\.]*\)[-+~].*$/\1/"`


echo -e "\x1b[34m######################################################################\x1b[0m"
echo -e "\x1b[34mCHANGELOG_HEADER:     ${CHANGELOG_HEADER}\x1b[0m"
echo -e "\x1b[34mPACKAGE:              ${PACKAGE}\x1b[0m"
echo -e "\x1b[34mPACKAGE_DISTRIBUTION: ${PACKAGE_DISTRIBUTION}\x1b[0m"
echo -e "\x1b[34mPACKAGE_VERSION       ${PACKAGE_VERSION}\x1b[0m"
echo -e "\x1b[34mOUTPUT_VERSION:       ${OUTPUT_VERSION}\x1b[0m"
echo -e "\x1b[34mDEBIAN_VERSION:       ${DEBIAN_VERSION}\x1b[0m"
echo -e "\x1b[34mUPSTREAM_VERSION:     ${UPSTREAM_VERSION}\x1b[0m"
echo -e "\x1b[34mPLAIN_VERSION:        ${PLAIN_VERSION}\x1b[0m"
echo -e "\x1b[34m######################################################################\x1b[0m"


# ====== Update LSM entry ===================================================
LSM_ENTRY="${LSM_DIRECTORY}/${PACKAGE}.lsm"
if [ ! -e "${LSM_ENTRY}" ] ; then
   echo 2>&1 "ERROR: LSM entry ${LSM_ENTRY} does not exist!"
   exit 1
fi
for UPSTREAM_PACKAGE_TYPE in xz bz2 gz ; do
   UPSTREAM_PACKAGE="`find . -maxdepth 1 -name "${PACKAGE}-${UPSTREAM_VERSION}.tar.${UPSTREAM_PACKAGE_TYPE}" -printf '%f'`"
   if [ -e "${UPSTREAM_PACKAGE}" ] ; then
      break
   fi
done
if [ ! -e "${UPSTREAM_PACKAGE}" ]; then
   echo >&2 "ERROR: File ${PACKAGE}-${UPSTREAM_VERSION}.tar.* does not exist!"
   exit 1
fi

today=`env LANG=C date "+%Y-%m-%d"`
size=`wc -c "${UPSTREAM_PACKAGE}" | awk '{ print $1 }'`
let size=$size/1024

sed -e "s/^\(Entered-date:[ \t]*\)\(.*\)$/\1$today/g" \
    -e "s/^\(Version:[ \t]*\)\(.*\)$/\1${UPSTREAM_VERSION}/g" \
    -e "s/\([0-9]*\)\([ ]*kB[ ]*\)\(.*\)\(${PACKAGE}-\)\([0-9a-zA-Z\.~+-]*\)\(\.tar\.*\).*$/$size\2\3$UPSTREAM_PACKAGE/g" \
   <${LSM_ENTRY} >${LSM_ENTRY}.new

echo "Update:"
if diff --color=always ${LSM_ENTRY} ${LSM_ENTRY}.new ; then
   echo >&2 "Nothing to update?!"
   exit 1
fi


# ====== Apply changes ======================================================
echo -en "\x1b[34mRun: Apply update? [yes/no]?\x1b[0m "
read -er applyChanges
if [ "$applyChanges" == "yes" -o "$applyChanges" == "y" ] ; then
   mv ${LSM_ENTRY}.new ${LSM_ENTRY}
   thunderbird -compose to=lsm@pug.qqx.org,subject=add,attachment="${LSM_ENTRY}" || true &
fi
