#!/bin/sh
#
# Copyright (C) 2023 Milan Kupcevic <milan@debian.org>
#
#  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/>.

set -e

while [ "$1" ]; do
  case "$1" in
    --upstream-version|-v)
      shift; new_version="$1"
      ;;
    --force-bad-version|-b|--find|-f)
      :
      ;;
  esac
  shift
done

new_tarball=../simulide_${new_version}.orig.tar.xz

if [ ! -f "$new_tarball" ]; then
  echo "debian/repack: tarball package name is missing" >&2
  exit 1
fi

tmp_dir="debian.repack.tmp"
rm -rf "$tmp_dir"
mkdir "$tmp_dir"

tar -C "$tmp_dir" --auto-compress --strip-components=3 -xf "$new_tarball"

tar -C "$tmp_dir" --auto-compress --transform="s/^\./simulide-$new_version/" -cf "$new_tarball" "."
rm -rf "$tmp_dir"
