#!/bin/bash

# convert a pet to a slackware t?z package
# 01micko GPL2 2013
ver=0.4

echo "You must be root"
[ $UID = 0 ]||exit
echo "root you are then.."

PROG=`basename $0`
usage(){
	echo "Usage:"
	echo "$PROG somepet.pet -- produces a Slackware .txz file."
	echo "$PROG somepet.pet -b -- produces .tbz instead of default .txz"
	echo "$PROG somepet.pet -g -- produces .tgz instead of default .txz"
	echo -e "$PROG somepet.pet -x=<build> \n-- produces build num or description instead of default PET. \nNo spaces, alpha-numeric chars only."
	echo -e "$PROG somepet.pet -d -- pauses the script to put in a verbose description."
	echo -e "$PROG somepet.pet -x=<arch> \n-- the script tries to get the architecture however\nyou can overide this, eg: \"armhf\", \"i686\""
	echo "$PROG -v -- states version"
	echo "$PROG -h -- shows this help"
	exit
}
[ ! "$*" ] && usage
case $1 in
-h)usage
;;
-v)echo "$PROG $ver" && exit
;;
esac

EXT=txz
PAUSE=

for arg in "$@";do
	case "$arg" in
	*.pet)PACKAGE=$arg ;;
	-b)EXT=tbz ;;
	-g)EXT=tgz ;;
	-d)PAUSE=1 ;;
	-a=*)ARCH=`echo "$arg"|awk -F= '{print $2}'` ;;
	-x=*)BUILD=`echo "$arg"|awk -F= '{print $2}'` ;;
	*)usage ;;
	esac
	shift
	done
	
BUILD="${BUILD:-PET}"

echo "$PACKAGE"|grep -q "\.pet$"
[ "$?" -ne 0 ] && echo "$1 is not a pet, exiting" && exit
file "$PACKAGE"|grep -q -E "gzip compressed data|xz compressed data" #should handle FatDog packages
[ "$?" -ne 0 ] && echo "$1 is not a pet or is not compatible, exiting" && exit
# unpack if above passed
tar -xf "$PACKAGE" 2>/dev/null #suppress the error

PETDIR=`basename $PACKAGE .pet`

# sort out arch
if [ -z $ARCH ];then
	EXEC=`find $PETDIR -executable -type f | grep -v "pinstall\.sh" | head -1`
	PREARCH=`file ${EXEC}|awk -F, '{print $2}'`
	case "$PREARCH" in
	*[3-6]86*)ARCH=i486 ;;
	*64*)ARCH=x86_64 ;;
	*ARM*)ARCH=arm ;;
	*)ARCH=noarch ;;
	esac
fi

# description option
if [ "$PAUSE" == 1 ];then
	echo "Now you can enter a detailed description of your package."
	read DESCRIPTION
	DESC="$DESCRIPTION"
fi

GEN_NAME=`cat $PETDIR/pet.specs|cut -d '|' -f2`
PVER=`cat $PETDIR/pet.specs|cut -d '|' -f3|tr '-' '\n'|head -1`
mv -f $PETDIR $GEN_NAME
PETDIR=${GEN_NAME}
PKGNAME=${PETDIR}-${PVER}-${ARCH}-${BUILD}.${EXT}
# make install dir
mkdir -p $PETDIR/install
# doinst.sh
[ -f  $PETDIR/pinstall.sh ] && cat $PETDIR/pinstall.sh > $PETDIR/install/doinst.sh && rm $PETDIR/pinstall.sh
# slack-desc
[ "$DESC" ] || DESC=`cat $PETDIR/pet.specs|cut -d '|' -f10`
#debug==================================================================
#echo -e "testing \n1 BUILD=${BUILD} \n2 EXT=${EXT} \n3 PACKAGE=${PACKAGE} \n4 $PAUSE \n5 ARCH=${ARCH} \n6 PKGNAME=${PKGNAME} \n7 DESC="$DESC" \n8 PREARCH=$PREARCH \n9 EXEC=$EXEC" 
#read
#===============================================================REMOVE
# slice up $DESC if more than 72 chars
DESCCNT=`echo "$DESC"|wc -c`
if [ $DESCCNT -gt 72 ];then
  case "$DESCCNT" in #working on average 6 letters per word, guess
  7?|8?|9?|10?|11?|12?|13?)DESC1=`echo "$DESC"|awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10}'`
         DESC2=`echo "$DESC"|awk '{print $11,$12,$13,$14,$15,$16,$17,$18,$19,$20}'`
         ;;
  *)DESC1=`echo $DESC|awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10}'`
         DESC2=`echo $DESC|awk '{print $11,$12,$13,$14,$15,$16,$17,$18,$19,$20}'`
         DESC3=`echo $DESC|awk '{print $21,$22,$23,$24,$25,$26,$27,$28,$29,$30}'`
         ;; #we truncate, ugly? yes! Usually less than 70 chars anyway
  esac
fi



NAMECNT=`echo $GEN_NAME|wc -c`
NAMECNT=$((${NAMECNT}-1))
x=$(c=1;while [ $c -le $NAMECNT ];do echo -n 'i';c=$(($c + 1));done)

cat << EOF >  $PETDIR/install/slack-desc
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':' except on otherwise blank lines.

EOF
x=`echo "$x"|tr 'i' ' '`
echo "${x}|-----handy-ruler------------------------------------------------------|" >>$PETDIR/install/slack-desc 
echo "${GEN_NAME}: $GEN_NAME" >>$PETDIR/install/slack-desc
echo "${GEN_NAME}:" >>$PETDIR/install/slack-desc
if [ "$DESC2" != "" ];then
  echo "${GEN_NAME}: $DESC1" >>$PETDIR/install/slack-desc
  echo "${GEN_NAME}: $DESC2" >>$PETDIR/install/slack-desc
  DCNT=7
  elif [ "$DESC3" != "" ];then
  echo "${GEN_NAME}: $DESC1" >>$PETDIR/install/slack-desc
  echo "${GEN_NAME}: $DESC2" >>$PETDIR/install/slack-desc
  echo "${GEN_NAME}: $DESC3" >>$PETDIR/install/slack-desc
  DCNT=6
  else
  echo "${GEN_NAME}: $DESC" >>$PETDIR/install/slack-desc
  DCNT=8
fi
z=1
while [ $z -le $DCNT ]
do echo "${GEN_NAME}:" >>$PETDIR/install/slack-desc
   z=$(($z + 1))
done
rm $PETDIR/{pet.specs,puninstall.sh} 2>/dev/null

# ready to make the t?z
MKPKG=`which makepkg`
if [ "$MKPKG" ];then
  (cd $PETDIR
   $MKPKG -l y -c n ../$PKGNAME)
   else
  (cd $PETDIR
  case $EXT in
  txz)
  tar cvf - . | xz -c > $PKGNAME
  [ "$?" -ne 0 ] && echo "failed to create $PKGNAME" && exit 
  ;;
  tgz)
  tar cvf - . | gzip -9c > $PKGNAME
  [ "$?" -ne 0 ] && echo "failed to create $PKGNAME" && exit 
  ;;
  tbz)
  tar cvf - . | bzip2 -9c > $PKGNAME
  [ "$?" -ne 0 ] && echo "failed to create $PKGNAME" && exit 
  ;;
  esac
  mv $PETDIR/$PKGNAME . )
fi
echo "$PKGNAME is in `pwd`"
