#! /bin/bash
#250705
#create simple .deb package version 5
#author - wizard on the Puppy Linux Forum
#contributions by fredx181

function chkbuild () {
#exit if build fails
  if [ $PIPESTATUS -gt 0 ]; then
    yad --title="Build Failed" \
        --width=250 \
        --button="Exit":2 \
        --text="$(cat /tmp/dir2deb.log)" 
    exit
  fi
}
#end function

#create Debian dir for control file
dirname=$1
curdir=$(dirname "$dirname")
#exit if no directory
if [ -z "$dirname" ]; then
 yad  --borders=10 \
      --title="No directory" \
      --text="You must provide a directory\nExiting..." \
      --width=300 \
      --button="Exit:0"
 exit
fi

mkdir -p $dirname/DEBIAN
ctldir=$dirname/DEBIAN

if [ -e "$ctldir/control" ]; then
    yad --title="Control File" \
        --button="Yes":2 \
        --button="No":0 \
        --text="Existing control file found
Do you want to reuse it?        
"
fi

ret=$?
if [ $ret == 2 ]; then
  #get package name from existing control file 
  Package=$(grep Package: "$ctldir/control"  | cut -f 2- -d ' ')
  Version=$(grep Version: "$ctldir/control"  | cut -f 2- -d ' ')
  Architecture=$(grep Architecture: "$ctldir/control"  | cut -f 2- -d ' ')
  #build .deb file, create log file
  dpkg-deb --build $dirname "$curdir" 2>&1 | tee /tmp/dir2deb.log
  chkbuild
  yad --title="Package Complete" \
        --button="OK" \
        --text="Your .deb package is:
"$curdir"/${Package}_${Version}_${Architecture}.deb    
" 
  exit
fi

#get values from user
values=$(yad --form --width=350 --title="Create control file" --separator=';' \
--text="All fields required
Package Name = lowercase alphanum and - + _ only
" \
--button=Cancel:0 \
--button=Build:2 \
--button="Add Options:4" \
--field="Package Name" "no blank spaces, use dashes" \
--field="Version" "must begin with a number" \
--field="Maintainer" " " \
--field="Architecture:CB" all\!i386\!amd64 \
--field="Description" \ )

ret=$?

Package=`echo $values | awk -F';' '{print $1}'`
Version=`echo $values | awk -F';' '{print $2}'`
Maintainer=`echo $values | awk -F';' '{print $3}'`
Architecture=`echo $values | awk -F';' '{print $4}'`
Description=`echo $values | awk -F';' '{print $5}'`
echo $Package $Version $Maintainer $Architecture $Description

#run if Build chosen
if [ $ret == 0 ] ; then
  exit
 else 
 #create control file 
 echo "Package: $Package" > $ctldir/control   
 echo "Version: $Version" >> $ctldir/control   
 echo "Maintainer: $Maintainer" >> $ctldir/control     
 echo "Architecture: $Architecture" >> $ctldir/control
 echo "Description: $Description" >> $ctldir/control
fi
 
#advanced options
if [ $ret == 4 ]; then 
 avalues=$(yad --form --width=350 --title="Advanced Options" --separator=';' \
--text="Consult the Debian package management 
documentation for fields, usage and syntax

You can also manually edit the DEBIAN/control 
file to add additional fields, then run 
dir2deb again and use the existing control file
choice
" \
 --button="Manual Edit:0" \
 --button=Build:2 \
 --button=Exit:1 \
 --field="Depends" " " \
 --field="Replaces" "none" \ )

 ret=$?
 Depends=`echo $avalues | awk -F';' '{print $1}'`
 Replaces=`echo $avalues | awk -F';' '{print $2}'`

 echo "Depends: $Depends" >> $ctldir/control
 echo "Replaces: $Replaces" >> $ctldir/control
 
 if [ $ret == 0 ]; then
   geany $ctldir/control
   exit
   elif [ $ret == 1 ]; then
   exit
 fi

fi
#end advanced
  
#build .deb file, create log file
  dpkg-deb --build $dirname "$curdir" 2>&1 | tee /tmp/dir2deb.log 
  chkbuild
   
 yad --title="Package Complete" \
        --button="OK" \
        --text="Your .deb package is:
"$curdir"/${Package}_${Version}_${Architecture}.deb      
" \

exit
