#!/bin/sh
. /etc/blackPanther-default-apps.conf
#*********************************************************************************************************
#*   __     __               __     ______                __   __                      _______ _______   *
#*  |  |--.|  |.---.-..----.|  |--.|   __ \.---.-..-----.|  |_|  |--..-----..----.    |       |     __|  *
#*  |  _  ||  ||  _  ||  __||    < |    __/|  _  ||     ||   _|     ||  -__||   _|    |   -   |__     |  *
#*  |_____||__||___._||____||__|__||___|   |___._||__|__||____|__|__||_____||__|      |_______|_______|  *
#* http://www.blackpantheros.eu | http://www.blackpanther.hu - kbarcza[]blackpanther.hu * Charles Barcza *
#*************************************************************************************(c)2002-2011********

export PATH=$PATH:/usr/sbin:/sbin

case $LANG in 
	hu*) 
	example=$example_hu
	specimputerr=$specimputerr_hu
	helper="
	
$GRN CSOMAGKÉSZÍTŐ SEGÍTSÉG$BLU blackPanther OS$GRN-hez - $DEF www.blackpantheros.eu
   
 $DEF   -h|--help|--segits $GRN  ennek a szövegnek a megjelenítése

 $DEF   1|ua1|-r1|-u1 $GRN	build a %make -tól, csak az első lépés az RPM létrehozása nélkül
 $DEF   2|ua2|-r2|-u2 $GRN	build az %install-tól, csak a második lépés és az RPM létrehozása
 $DEF        |-a |-ua $GRN      build automatikusan az első, második lépéssel az RPM csomag létrehozása
 $DEF  source|src|-src$GRN      forráscsomag létrehozása az alap, első, második lépések kihagyásával
 $DEF              -s $GRN      a csomagok aláírása GPG kulccsal
      
 $DEF Példa az automatikushoz :$MAG csomagkeszito example.spec
 $DEF Példa az első lépéshez  :$MAG csomagkeszito 1 example.spec
 $DEF Példa a második lépéshez:$MAG csomagkeszito 2 example.spec
 $DEF GPG-vel aláírt csomaghoz:$MAG csomagkeszito -s example.spec

 $DEF Példa a forráscsomag létrehozásához :$MAG csomagkeszito src example.spec$DEF

"
;;
	*)
	example=$example_en
	specimputerr=$specimputerr_en
	helper="
	
$GRN PACKAGEMAKER HELP FOR$BLU blackPanther OS$GRN -$DEF www.blackpantheros.eu
   
 $DEF  -h|--help|--segits $GRN  for this text

 $DEF  1|ua1|-r1|-u1 $GRN	build only one step from %make without build final RPM
 $DEF  2|ua2|-r2|-u2 $GRN	build only two step from %install with build final RPM
 $DEF       |-a |-ua $GRN       build automatically from one step and generate final  RPM
 $DEF source|src|-src$GRN       generate source RPM, skipping zero, one, two steps
 $DEF             -s $GRN       generate a signed RPM with GPG key
   
 $DEF Example for automate:$MAG packagemaker example.spec
 $DEF Example for one step:$MAG packagemaker 1 example.spec
 $DEF Example for two step:$MAG packagemaker 2 example.spec
 $DEF Example for signed : $MAG packagemaker -s example.spec

 $DEF Example for generate source package:$MAG packagemaker source example.spec

"
;;	
esac

if [ "$1" = '-h' -o "$1" = "--help" -o "$1" = "--segits" ]; then
    echo -e "$helper"
exit
fi

BIN=`which rpmbuild 2>/dev/null || echo ERROR`
IN=`echo $1 | grep ".spec$" || echo ERROR`

if [ "$IN" = ERROR ];then
    IN=`echo $2 | grep ".spec$" || echo ERROR`
  if [ "$IN" = ERROR ];then
    echo -e "$RED 
  * $specimputerr
  
$DEF $example $0 example.spec
  "
    exit
  fi
fi

if [ ! -f "$IN" ];then 
      echo ""
      echo -e "$RED ** The file not found on the entered $YEL($IN)$RED path! Aborted $DEF"
      echo ""
      exit
fi

SIGN=" "
SPEC="$1"
ENGINE="rpmbuild -b"
#while [ -n "$3" ]; do
#    case $3 in
#	"-f")
#	echo "Reforce"
#	REFORCEBP="yes"
#	;;
#	"-s")
#	echo "SIGN"
#	SIGN=" --sign "
if [ "$1" = '-s' -o  "$1" = '--sign' ]; then
  #echo -n " * Great! GPG Sign the package, please  "
  ENGINE="rpmbuild --sign -b" 
  SPEC=$2
elif [ "$2" = '-s' -o  "$2" = '--sign' -o "$3" = '-s' -o  "$3" = '--sign' ]; then
  ENGINE="rpmbuild --sign -b" 
  SPEC=$1
fi
#	;;
#    esac
#    shift
#done

LOCK=$(echo $BIN|awk -Fbin/ '{print $2}')
TOPDIR=`rpm --eval '%_topdir'`
TMPDIR=${TMPDIR=$HOME/tmp}
SPECLOG=$(echo "$IN" | awk -F.spec\$ '{print $1}' | sed 's|.*/||')
OPT="> $TMPDIR/$SPECLOG.log 2>&1"
#echo $OPT

if [ "$BIN" = ERROR ];then
      echo ""
    echo -en "$RED ** Build engine not installed! $DEF
 * Try autoinstall the rpm-build package...."
	su - -c 'installing rpm-build' root
      echo -e " * ${YEL}The progress now end. Now run again this $1$2$3 commands. $DEF"
      echo ""
	
    exit
fi

finish() {
    echo -e "$GRN * Following package(s) done: 
$(cat $TMPDIR/$SPECLOG.log | grep -e signature -e .rpm$ | sed 's|Wrote|  ** Wrote|')
    -------------------------------------------------------------
$YEL * Remove build-log after 5 sec... $DEF"
    sleep 5
    rm -f $TMPDIR/$SPECLOG.log  
    echo " * Clean..." 
}

fail() {
	echo ""
    echo -e "${RED} ** Failed! $DEF See full log for desctiption (example: cat $TMPDIR/$SPECLOG.log )"
	echo ""
	echo -e "${YEL}_____Details_________________________________________________________________________$DEF"
	 cat $TMPDIR/$SPECLOG.log | tail -n 20 | head -n 17
	echo -e "${YEL}_____________________________________________________________________________End______$DEF"
	echo ""
    exit 1
}

gensource() {
# generate source automatically
        SPEC=$2
	echo -ne "$MAG ** " 
	rpmbuild -bs "$SPEC"
	echo -ne "$DEF " 
	echo ""
}
progress() {
    #sleep 0.3
    	 if [ "${COUNTER}" = "0" ]; then
	    	    echo -ne "[                                                ]"
	 fi 
    if $(ps -C $LOCK --format args --noheaders | grep $LOCK > /dev/null 2>&1);then
         COUNTER=`expr ${COUNTER} + 1`
	 if [ ${COUNTER} -eq 77 ]; then
		    #echo -ne "${GRN}|${DEF}"
	    	    echo -n " ]"
	    	    #echo ""
	    	    #echo -n "               "
	    	    COUNTER=0
	          else
	    	    #echo -ne "${GRN}|${DEF}"
	    	    echo | >>/tmp/progress
		    cat /tmp/progress
		    #echo -n "."
		fi
		#echo  -ne " * Progress: [$RED |||"
		    #echo -n "."
		    #cat /tmp/progress
	progress
    fi
}

if [ "$1" = 'src' -o "$1" = 'source' -o "$1" = '-src' ]; then
    echo ""
    echo -e "$YEL * Generate source RPM $DEF"
    gensource $@

exit
fi

step1() {
    if  rpmbuild -bi --short-circuit "$2" 2>&1|pv -i 0.5 -cN " * Progressing:" > $TMPDIR/$SPECLOG.log ; then
        RET=$(cat $TMPDIR/$SPECLOG.log | grep -e "Bad exit" -e "error:" -e "Pass phrase check failed" -e "but unpackaged")
        if [ "$RET" != "" ];then
    	    fail
        fi
	#echo -e "$GRN * Command with one step ended"
	#echo ""
	if [ ! -f $TMPDIR/$SPECLOG.log ];then
	echo -e "$RED * You have a error! Try run from zero step and after try run again one the step $DEF "
	echo ""
	echo -e "${YEL}_____Details_________________________________________________________________________$DEF"
	 rpmbuild -bi --short-circuit "$2"
	echo -e "${YEL}_____________________________________________________________________________End______$DEF"
	echo ""
	 exit
	#finish
	fi
	echo -e "$GRN * Command with one step ended"
	echo ""
      else
	fail
    fi

}

step2() {
    #rpmbuild -bb --short-circuit "$2" $OPT
    if  ${ENGINE}b --short-circuit$SIGN "$2"  2>&1 | pv -W -i 0.5 -cN " * Progressing:" >> $TMPDIR/$SPECLOG.log ; then
        RET=$(cat $TMPDIR/$SPECLOG.log | grep -e "Bad exit" -e "error:" -e "Pass phrase check failed" -e "but unpackaged")
        if [ "$RET" != "" ];then
    	    fail
        fi
	if [ ! -f $TMPDIR/$SPECLOG.log ];then
	echo -e "$RED * You have a error! Try run from one step and after try run again two the step $DEF "
	echo ""
	echo -e "${YEL}_____Details_________________________________________________________________________$DEF"
	 rpmbuild -bb --short-circuit "$2"
	echo -e "${YEL}_____________________________________________________________________________End______$DEF"
	echo ""
	 exit
	#finish
	fi
	echo -e "$GRN * Command with two step ended"
	echo ""
	finish
      else
	fail
    fi
}

if [ "$1" = "1" -o "$1" = "-ua1" -o "$1" = "-r1" -o "$1" = "-u1" ];then
    echo -e "$BLU * Generate a RPM package from one step.. $DEF"
    sleep 1
    #rpmbuild -bi --short-circuit "$2" $OPT
    rm -f $TMPDIR/$SPECLOG.log
    step1 $@
elif [ "$1" = "2" -o "$1" = "-ua2" -o "$1" = "-r2" -o "$1" = "-u2" ];then
    echo -e "$BLU * Generate a RPM package only from two step.. $DEF"
    sleep 1
    step2 $@
elif [ "$1" = "-a" -o "$1" = "-ua" ];then
    rm -f $TMPDIR/$SPECLOG.log
    echo -e "$BLU * Generate a RPM package from one and after two step.. $DEF"
    sleep 1
    ${ENGINE}i --short-circuit$SIGN "$2" 2>&1| pv -W -cN " * Progressing:" > $TMPDIR/$SPECLOG.log && rpmbuild -bb --short-circuit "$2"| pv -W -cN " * Progressing:" >> $TMPDIR/$SPECLOG.log
    RET=$?
    #echo "???$RET"
    if [ "$RET" = 0 ]; then
	gensource $@
	finish
      else
	fail
    fi
else 
    echo -e "$BLU * Generate a RPM package automatically (with zero, one and two step).. please wait to finish! $DEF"
    sleep 1
    if [ "$1" = '-s' -o  "$1" = '--sign' -o "$2" = '-s' -o  "$2" = '--sign' ]; then
	 echo -ne "$MAG * Great! GPG Sign the package, please  "
    fi
    #echo "${ENGINE}a $SPEC 2>&1 | pv -W -cN ' * Progressing:' > $TMPDIR/$SPECLOG.log
    #"
    ${ENGINE}a "$SPEC" 2>&1 | pv -W -cN " * Progressing:" > $TMPDIR/$SPECLOG.log

    RET=$(cat $TMPDIR/$SPECLOG.log | grep -e "Bad exit" -e "error:" -e "Pass phrase check failed")
    if [ "$RET" = "" ];then
	#if  rpmbuild -ba "$1" > $TMPDIR/$SPECLOG.log 2>&1; then
	finish
	     else
	fail
    fi

fi
