#!/bin/sh
#by BarryK 2006 for Puppy Linux v2.13+
#passed param is file to be converted.
#converts a .pet file to .tar.gz.
#131122 support xz compressed pets (see dir2pet, installpkg.sh)
[ "$(cat /var/local/petget/nt_category 2>/dev/null)" != "true" ] && \
 [ -f /tmp/install_quietly ] && set -x
 #; mkdir -p /tmp/PPM_LOGs ; NAME=$(basename "$0"); exec 1>> /tmp/PPM_LOGs/"$NAME".log 2>&1
DL_SAVE_FLAG=$(cat /var/local/petget/nd_category)
chmod +w "$1" #make it writable.
FOOTERSIZE="32"
export LC_ALL=C

#determine the compression, extend test to 'XZ'
file -b "$1" | grep -i -q "^xz" && EXT=xz || EXT=gz #131122 #140108 add -i for 'XZ'

MD5SUM="`tail -c $FOOTERSIZE \"$1\"`"

NEWNAME="`echo -n \"$1\" | sed -e "s/\\.pet$/\\.tar\\.$EXT/g"`" #131122

head -c -$FOOTERSIZE "$1" > $NEWNAME
NEWMD5SUM="`md5sum \"$NEWNAME\" | cut -f 1 -d ' '`"

sync
[ "$DL_SAVE_FLAG" != "true" ] && rm -f "$1"
[ ! "$MD5SUM" = "$NEWMD5SUM" ] && exit 1
exit 0

