File rearchive of Package obs-service-rearchive

#!/bin/bash

# A simple script to convert non-tar-archives to tar.gz as source service
#
# (C) by Mirko Dölle <mid@ct.heise.de>
#
# derived from obs-service-recompress from Adrian Schröter
#
# 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
FILE=""
FORMAT=""
OUTDIR=""

while test $# -gt 0; do
  case $1 in
    *-format)
      FORMAT="$2"
      shift
    ;;
    *-file)
      FILES="$FILES ${2##*/}"
      shift
    ;;
    *-outdir)
      OUTDIR="$2"
      shift
    ;;
    *)
      echo "Unknown parameter $1."
      echo 'Usage: rearchive --format $FORMAT --file $FILE'
      exit 1
    ;;
  esac
  shift
done

if [ -z "$FILES" ]; then
  echo "ERROR: no inputs files are given via --file parameter!"
  exit 1
fi
if [ -z "$OUTDIR" ]; then
  echo "ERROR: no output directory is given via --outdir parameter!"
  exit 1
fi


for i in $FILES; do
  if [ ${i#-} != $i ];then
    echo "WARNING: illegal --file option: $i"
    echo "SKIPPING ..."
    continue
  fi
  FILE=$(ls -1 ${i} _service\:*\:${i#_service:*:} 2>/dev/null|tail -n 1 || echo "")
  if [ -z "${FILE}" ]; then
    # quietly skip non-existing archive files
    continue
  fi

  BASENAME="$FILE"

  if [ "${FORMAT}" = "zip" -o "${FILE%.zip}" != "$FILE" ]; then
    UNPACK="unzip -nqq"
    BASENAME="${FILE%.*}"
  elif [ "${FORMAT}" = "rar" -o "${FILE%.rar}" != "$FILE" ]; then
    UNPACK="unrar e -o- -inul"
    BASENAME="${FILE%.*}"
  else
    echo "ERROR: unsupported/undetected source archive ${FILE}!"
    exit 1
  fi
  NEWFILE="${BASENAME#_service:.*:}.tar.gz"
  if [ -e "${NEWFILE}" ]; then
    echo "ERROR: will not overwrite existing tar archive ${NEWFILE}!"
    exit 1
  fi

  # do the real work
  WORKDIR=${PWD}
  TEMPDIR=$(mktemp -d "${FILE}.XXXXXXXX")
  cd "${TEMPDIR}"
  ${UNPACK} "${WORKDIR}/${FILE}" && tar czf "${OUTDIR}/${NEWFILE}" $(ls -1AU) || exit 1
  cd - > /dev/null
  rm -fR "${TEMPDIR}"

  if [ "${FILE#_service:}" != "$FILE" ]; then
    # we can remove service files, no need to store them twice
    rm -f "$FILE"
  fi
done

exit 0
openSUSE Build Service is sponsored by