ARGUME=$PACKAGE
COL=80

if test x"$RELEASE" = x0 ; then
   echo "RELEASE=0 is not aloud in debian"
   RELEASE='0.1'
fi
if test x$ARCH = xnoarch ; then
   ARCH=all
fi
#DEBIAN=debian
#mkdir -p $UPACKDROOT/$DEBIAN 
FILECONTROL=$UPACKDINFO/control

cl2pkgf.pl "$DIRSUMA/ChangeLog" deb "$UPACKDINFO/changelog" "$PACKAGE" "$VERSION"  "$RELEASE" "testing-unstable-experimental" "low"

#cp /tmp/cldeb $DEBIAN/changelog
#echo  cp /tmp/cldeb $DEBIAN/changelog

echo CONTROL: $FILECONTROL

brand=debian

if test x$brand = "xdebian"; then 
    PKG_GROUP=$PKG_SECTION_DEBIAN
#    PKGTYPE="deb"
fi
if test "x$PKG_GROUP" = "x"; then 
     PKG_GROUP="nose"                 
fi
echo PKGTYPE $PKGTYPE
echo PKG_GROUP $PKG_GROUP




#####33 ver como calcula ionstalled size en bytes dividido 1024 y redondeado
#3 revisar ARCHITECUTRE
FILE=$FILECONTROL

echo Package: $PACKAGE>$FILE
#echo Source:
if test x$RELEASE = x ; then
 echo Version: $VERSION>>$FILE
else
 echo Version: $VERSION-$RELEASE>>$FILE
fi
echo Section: $PKG_GROUP>>$FILE
echo Priority: $PRIORITY>>$FILE
echo Architecture: $ARCH>>$FILE
#echo Essential: >>$FILE
echo Depends:>>$FILE
echo Installed-Size: $INSTALLEDSIZE>>$FILE
echo Maintainer: $MAINTAINER>>$FILE
echo Description: $SUMMARY>>$FILE
echo "$DESCRIPTION" | fmt -w $COL | sed -n -e "s/^/ /p " >> $FILE
#echo Homepage
#cat $FILE


#echo OOOOOOOOO	 ${UPACKDROOT} -  ${UPACKDINFO}
# verificar si existen 
#armo en otro lado?

#	mkdir -p /tmp/${BUILD_ROOT}/DEBIAN
#forma=2

#compilar dpkg al menos el 1.15 de sid

#fakeroot?
#chown a root:root?

if test ! -e ${UPACKDINFO}/control; then
	    echo "Package: $ARGUME" >>${UPACKDINFO}/control
	    echo "Priority: optional" >>${UPACKDINFO}/control
	    echo "Section: devel" >>${UPACKDINFO}/control
	    echo "Architecture: i386" >>${UPACKDINFO}/control
	    echo "Maintainer: $ARGUME $ARGUME <$ARGUME@sumapack.org>" >>${UPACKDINFO}/control
	    echo "Version: 0.1" >>${UPACKDINFO}/control
	    echo "Depends: libc6" >>${UPACKDINFO}/control
	    echo "Description: called $ARGUME" >>${UPACKDINFO}/control
            echo " is $ARGUME "             >>${UPACKDINFO}/control
        fi 

prefix=/usr/local
	if test ! -e ${UPACKDROOT}${prefix}/doc/$ARGUME/copyright; then
           mkdir -p ${UPACKDROOT}${prefix}/share/doc/$ARGUME
           echo "Copyright (C) 2010 dsa"  >>${UPACKDROOT}${prefix}/share/doc/$ARGUME/copyright
           echo "This package contains free copyleft software;" >>${UPACKDROOT}${prefix}/share/doc/$ARGUME/copyright
           echo "On Debian systems, the GNU GPL can be found at" >>${UPACKDROOT}${prefix}/share/doc/$ARGUME/copyright
           echo "/usr/share/common-licenses/GPL" >>${UPACKDROOT}${prefix}/share/doc/$ARGUME/copyright
       fi
       if test ! -e ${UPACKDROOT}${prefix}/share/doc/$ARGUME/changelog.gz; then
           mkdir -p ${UPACKDROOT}${prefix}/share/doc
           echo "$ARGUME (0.1) unstable; urgency=low"  >${UPACKDROOT}${prefix}/share/doc/$ARGUME/changelog
           echo "" >>${UPACKDROOT}${prefix}/share/doc/$ARGUME/changelog
           echo "  * $ARGUME" >>${UPACKDROOT}${prefix}/share/doc/$ARGUME/changelog
           echo "" >>${UPACKDROOT}${prefix}/share/doc/$ARGUME/changelog
           echo " -- $ARGUME $ARGUME <$ARGUME@sumapack.org>  Tue, 18 Nov 2008 18:20:37 -0500" >>${UPACKDROOT}${prefix}/share/doc/$ARGUME/changelog
           echo "" >>${UPACKDROOT}${prefix}/share/doc/$ARGUME/changelog

            gzip -9 ${UPACKDROOT}${prefix}/share/doc/$ARGUME/changelog
       fi

type -P fakeroot  > /dev/null
UFA=$?
type -P dpkg  > /dev/null 
UDP=$?
#echo UDP UFA $UDP - $UFA
if test x"$UDP" = x0 -a  x"$UFA" = x0 ; then 
# if   type -P fakeroot  > /dev/null; then
#if test 1 = 2 ; then
	mkdir -p ${UPACKDROOT}/DEBIAN
	cp ${UPACKDINFO}/* ${UPACKDROOT}/DEBIAN
	#chown -R root:root ${UPACKDROOT}/*
        find ${UPACKDINFO} -type d | xargs chmod 755
	fakeroot dpkg-deb  --build ${UPACKDROOT} $PKGNAME  ;#  RESULTA=$$?;\
	rm -r ${UPACKDROOT}/DEBIAN
        exit 0
   else
        TMP=/tmp/kk
        mkdir -p $TMP
	

        echo "2.0" >$TMP/debian-binary
	#chown root:root $TMP/debian-binary
        #chown -R root:root ${UPACKDROOT}
        #chown -R root:root ${UPACKDINFO}
        find ${UPACKDINFO} -type d | xargs chmod 755
	#tar --group=root --owner=root -C ${UPACKDROOT} -zcf $TMP/data.tar.gz .
	#tar --group=root --owner=root -C ${UPACKDINFO} -zcf $TMP/control.tar.gz .
	tar --group=root --owner=root -C ${UPACKDROOT} --format=gnu -c . |gzip -9c >$TMP/data.tar.gz 
	tar --group=root --owner=root -C ${UPACKDINFO} --format=gnu -c . |gzip -9c > $TMP/control.tar.gz 
        #chown root:root $TMP/*
        echo	ar -Drc $PKGNAME  $TMP/debian-binary  $TMP/control.tar.gz $TMP/data.tar.gz
	ar -Drc "$PKGNAME" $TMP/debian-binary $TMP/control.tar.gz $TMP/data.tar.gz 

#este ar termina los nombres de archivos con /, debian hace como  el de bsd que los termina con espacios
#  * must create archive in bsd format
#  * debian-binary must always be first.
#  * The order of the members is: debian-binary, control, data.
#  * Optional members follow the required members, and are prefixed with '_'.
#Deb format 3.0 introduces several new features.
#  * The control member will always be control.tar.gz.
#  * The data member can have different compression formats, based on the
#    extension after the tar.  The current supported list is:
#    .gz		gzip
#    .bz2	bzip2
#    <empty>	no compression




# ar deterministic mode?
fi




#y rules?
#lintian linuxstatus_1.1-1_all.deb

# mv debian.deb linuxstatus_1.1-1_all.deb
#fakeroot dpkg-deb --build debian #bad owner, no es root:root
#ChangeLog.Debian gzip --best
#dpkg-gencontrol
#dpkg-buildpackage -rfakeroot -uc -us

# Re: dpkg-deb versus dpkg-buildpackage
#thanks - so .. is the following correct?
#assuming the source build tree has been created

#'dpkg-buildpackage' script requires a debian/rules file

#'dpkg-deb' does not use debian/rules (dbhelper)

#I understand now - needed to play with it a bit - debuild is a wrapper for dpkg-buildpackage#, lintian and linda


#http://tldp.org/HOWTO/html_single/Debian-Binary-Package-Building-HOWTO/#AEN108



##
#Logical package format

#The package is a binary format and consists of four sections:[1]

#    * The lead identifies the file as an RPM file and contains some obsolete headers.
#    * The signature which can be used to ensure integrity and/or authenticity
#    * The header contains metadata including package name, version, architecture, file list, etc..
#    * A file archive, which usually is cpio compressed with gzip. In more recent versions of RPM, star can also be used for archive and bzip2, lzma or xz for compression. RPM 5.0 format supports using xar for archiving.
#          o You can retrieve the cpio file archive out of an RPM file by using the tool rpm2cpio, which is included with the RPM Package Manager.[1] You can then extract the package's files out of the cpio archive without needing to install the RPM package.
#rpm2cpio logrotate-1.0-1.i386.rpm > blah.cpio
#cat logrotate-1.0-1.i386.rpm | rpm2cpio > blah.cpio
#rpm2cpio logrotate-1.0-1.i386.rpm  | cpio -t
#rpm2cpio logrotate-1.0-1.i386.rpm |cpio -ivd usr/man/man8/logrotate.8
# To extract a cpio file:
#$ cpio -iv < cpio_file
#To list the contents of a cpio file:
#$ cpio -itv < cpio_file
#To create a cpio file with all files in the current directory:
#$ ls | cpio -o > cpio_file
#To extract all files from an RPM:
#$ rpm2cpio RPM_file | cpio -idv
#To extract individual file(s) from an RPM:
#$ rpm2cpio RPM_file | cpio -id individual_file(s)
#To extract individual files from a Sun Flash Archive file:
#1. Split the Flash Archive into the cookie, identification, and archive files.
#$ flar split Flash_Archive_file
#2. Extract the file(s) from the cpio archive.
#e.g. extracting var/sadm/install/contents
#$ cpio -idv var/sadm/install/contents < archive
