obs-service-recompress/recompress

121 lines
3.0 KiB
Bash

#!/bin/bash
set -x
# A simple script to recomress the archive
#
#
# (C) 2010 by Adrian Schröter <adrian@suse.de>
#
# 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 2
# of the License, or (at your option) any later version.
# See http://www.gnu.org/licenses/gpl-2.0.html for full license text.
# defaults
MYCOMPRESSION=""
RENAME_PATTERN=""
FILES=""
while test $# -gt 0; do
case $1 in
*-compression)
MYCOMPRESSION="$2"
shift
;;
*-rename_pattern)
RENAME_PATTERN="$2"
shift
;;
*-file)
FILES="$FILES ${2##*/}"
shift
;;
*-outdir)
MYOUTDIR="$2"
shift
;;
*)
echo Unknown parameter $1.
echo 'Usage: recompress --compression $COMPRESSION --rename_pattern $RENAME_PATTERN --file $FILE --outdir $OUT'
exit 1
;;
esac
shift
done
if [ -z "$MYCOMPRESSION" ]; then
MYCOMPRESSION="bz2"
fi
if [ -z "$FILES" ]; then
echo "ERROR: no inputs files are given via --file parameter!"
exit 1
fi
if [ -z "$MYOUTDIR" ]; then
echo "ERROR: no output directory is given via --outdir parameter!"
exit 1
fi
if [ -n $RENAME_PATTERN ] ; then
MYVERSION=""
MYRELEASE=""
if [ -f _service:extract_version:release ]; then
MYRELEASE=`cat _service:extract_version:release`
fi
if [ -f _service:extract_version:version ]; then
MYVERSION=`cat _service:extract_version:version`
fi
RENAME_PATTERN=${RENAME_PATTERN//%RELEASE%/$MYRELEASE}
RENAME_PATTERN=${RENAME_PATTERN//%VERSION%/$MYVERSION}
fi
for i in $FILES; do
FILE=`ls -1 $i`
UNCOMPRESS="cat"
BASENAME="$FILE"
if [ "${FILE%.gz}" != "$FILE" ]; then
UNCOMPRESS="gunzip -c"
BASENAME="${FILE%.gz}"
elif [ "${FILE%.bz2}" != "$FILE" ]; then
UNCOMPRESS="bunzip2 -c"
BASENAME="${FILE%.bz2}"
elif [ "${FILE%.xz}" != "$FILE" ]; then
UNCOMPRESS="xz -dc"
BASENAME="${FILE%.xz}"
fi
$UNCOMPRESS "$FILE" > "$MYOUTDIR/$BASENAME" || exit 1
if [ -n $RENAME_PATTERN ]; then
if [ "${BASENAME%.tar}" != "$BASENAME" ]; then
tar xf $MYOUTDIR/$BASENAME -C $MYOUTDIR || exit 1
OLDDIR=${BASENAME%.tar}
OLDDIR=${OLDDIR##_service:*:}
mv "$MYOUTDIR/$OLDDIR" "$MYOUTDIR/$RENAME_PATTERN" || exit 1
tar cf "$MYOUTDIR/$RENAME_PATTERN.tar" -C $MYOUTDIR $RENAME_PATTERN || exit 1
rm -rf "$MYOUTDIR/$RENAME_PATTERN" "$MYOUTDIR/$BASENAME"|| exit 1
BASENAME="$RENAME_PATTERN.tar"
fi
fi
if [ "$MYCOMPRESSION" == "gz" ]; then
gzip "$MYOUTDIR/$BASENAME"
elif [ "$MYCOMPRESSION" == "bz2" ]; then
bzip2 "$MYOUTDIR/$BASENAME"
elif [ "$MYCOMPRESSION" == "xz" ]; then
xz "$MYOUTDIR/$BASENAME"
elif [ "$MYCOMPRESSION" == "none" ]; then
COMPRESS="cat -"
else
echo "ERROR: Unknown compression $MYCOMPRESSION"
exit 1
fi
if [ "${FILE#_service:}" != "$FILE" ]; then
# we can remove service files, no need to store them twice
rm -f "$FILE"
fi
done
exit 0