#!/bin/bash

# A simple script to checkout or update a svn or git repo as source service

# defaults
OUTFILE="."

while test $# -gt 0; do
  case $1 in
    *-outfilename)
      OUTFILE="${2}"
      shift
    ;;
    *-outdir)
      MYOUTDIR="$2"
      shift
    ;;
    *)
      echo Unknown parameter $1.
      echo 'Usage: extract_file --archive $ARCHIVE --file $FILE --outdir $OUT'
      exit 1
    ;;
  esac
  shift
done

if [ -z "$MYOUTDIR" ]; then
  echo "ERROR: no output directory is given via --outdir parameter!"
  exit 1
fi

existing_archive="$PWD/$(echo *.tar)"
bare_archive="$(echo *.tar)"
bare_archive="${bare_archive%.tar}"
cd "$MYOUTDIR"

if [ -e "$existing_archive" ]; then
  if [ "${existing_archive%.tar}" != "$existing_archive" ]; then
    tar xf "$existing_archive" "$bare_archive/ceph.spec.in" || exit 1
  else
    echo "ERROR: unknown archive format $existing_archive"
    exit 1
  fi
  if [ -e "$bare_archive/ceph.spec.in" ] ; then
    echo "ceph.spec.in found and extracted - Congratulations!"
    mv "$bare_archive/ceph.spec.in" "ceph.spec"
    rmdir "$bare_archive"
  else
    echo "Sorry, Charlie, better luck next time"
  fi
else
  echo "ERROR: archive not found: $existing_archive"
  exit 1
fi

exit 0
