#! /bin/bash

################################################################
#
# Copyright (c) 2026 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################

if test $# -ne 2; then
    exit 1
fi

from="$1"
to="$2"

case $to in
    *.tar.gz) ;;
    *)
	echo "unsupported output file name: $to" 1>&2
	exit 1
	;;
esac

case $from in
    *.tar.bz2) bzcat "$from"    | gzip -f - >"$to" || exit 1 ;;
    *.tar.xz)  xz -cd "$from"   | gzip -f - >"$to" || exit 1 ;;
    *.tar.zst) zstd -cd "$from" | gzip -f - >"$to" || exit 1 ;;
    *.zip)
	tmp=$(mktemp -d)
	unzip -q -d "$tmp" -- "$from" || exit 1
	( cd "$tmp" && tar czO * ) >"$to" || exit 1
	rm -rf "$tmp"
	;;
    *)
	echo "unsupported input file name: $from" 1>&2
	exit 1
	;;
esac

exit 0
