recompress 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#!/bin/bash

# A simple script to checkout or update a svn or git repo as source service
#  
# (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=""
FILES=""

while test $# -gt 0; do
  case $1 in
    *-compression)
      MYCOMPRESSION="$2"
      shift
    ;;
    *-file)
      FILES="$FILES ${2##*/}"
      shift
    ;;
    *-outdir)
      MYOUTDIR="$2"
      shift
    ;;
    *)
      echo Unknown parameter $1.
      echo 'Usage: recompress --compression $COMPRESSION --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

for i in $FILES; do
  FILE=`ls -1 "$i" || ls -1 "_service:*:$i"`
  if [ ! -f "$FILE" ]; then
    echo "Unknown file $i"
    exit 1
  fi
  UNCOMPRESS="cat"
  BASENAME="$FILE"
  if [ "${FILE%.gz}" != "$FILE" ]; then
    UNCOMPRESS="gunzip -c"
    BASENAME="${FILE%.gz}"
  elif [ "${FILE%.tgz}" != "$FILE" ]; then
    UNCOMPRESS="gunzip -c"
    BASENAME="${FILE%.tgz}.tar"
  elif [ "${FILE%.bz2}" != "$FILE" ]; then
    UNCOMPRESS="bunzip2 -c"
    BASENAME="${FILE%.bz2}"
  elif [ "${FILE%.xz}" != "$FILE" ]; then
    UNCOMPRESS="xz -dc"
    BASENAME="${FILE%.xz}"
  fi

  if [ "$MYCOMPRESSION" == "gz" ]; then
    COMPRESS="gzip -c -n --rsyncable -"
    NEWFILE="${BASENAME#_service:}.gz"
  elif [ "$MYCOMPRESSION" == "bz2" ]; then
    COMPRESS="bzip2 -c -"
    NEWFILE="${BASENAME#_service:}.bz2"
  elif [ "$MYCOMPRESSION" == "xz" ]; then
    COMPRESS="xz -c -"
    NEWFILE="${BASENAME#_service:}.xz"
  elif [ "$MYCOMPRESSION" == "none" ]; then
    COMPRESS="cat -"
    NEWFILE="${BASENAME#_service:}"
  else
    echo "ERROR: Unknown compression"
    exit 1
  fi

  # recompress
  mkdir uncompress_dir
  cd uncompress_dir
  mv ../$FILE uncompress.tar
  tar -xf uncompress.tar
  rm -rf uncompress.tar
  #tar --mtime="2017-01-01 00:00:00" --pax-option=exthdr.name=%d/PaxHeaders/%f,atime:=0,ctime:=0,mtime:=0 -cf ../$FILE `ls -A`
  tar --mtime="2017-01-01 00:00:00" --pax-option=exthdr.name=%d/PaxHeaders/%f,atime:=0,ctime:=0 -cf ../$FILE `ls -A`
  cd ../
  rm -rf uncompress_dir
  # do the real work
  $UNCOMPRESS -- "$FILE" | $COMPRESS > "$MYOUTDIR/$NEWFILE" || exit 1

  # Check if the (compressed) target file already exists in the directory where
  # the service is invoked and drop the newly generated one. Avoids overwriting
  # otherwise identical files which only have different timestamps. Note that
  # zdiff and co all fail to do that properly...
  if [ -f $NEWFILE ] ; then
    DIFF_TMPDIR=$(mktemp -d)
    SRC_DIR="$PWD"
    cd $DIFF_TMPDIR
    mkdir new old
    $(cd new ; tar -xf "$MYOUTDIR/$NEWFILE" 2> /dev/null || mv -- "$MYOUTDIR/$NEWFILE" .)
    $(cd old ; tar -xf "$SRC_DIR/$NEWFILE" 2> /dev/null || mv -- "$SRC_DIR/$NEWFILE" .)
    if diff -r new old > /dev/null ; then
      echo "Identical target file $NEWFILE already exists, skipping.."
      rm -r -- "$MYOUTDIR/$NEWFILE"
    else
      echo "Compressed $FILE to $NEWFILE"
    fi
    cd $SRC_DIR
    rm -rf -- "$DIFF_TMPDIR"
  else
    echo "Compressed $FILE to $NEWFILE"
  fi
  
  # we can remove service files, no need to store them twice
  rm -f -- "$FILE"
done

exit 0