#!/bin/bash
# Deb Builder
#
# Simple tool to create very basic deb files for your own binary/script apps
#
# Copyright 2011 Tony Brijeski <tb6517@yahoo.com>
# Licensed under the GPLv2 license
# see /usr/share/common-licenses/GPL-2
#

testroot="`whoami`"

if [ "$testroot" != "root" ]; then
echo " Must be run as root - exiting"
exit 1
fi


DIALOGMENU="`which zenity` --width=400 --height=400"
DIALOG="`which zenity`"
TITLE="--title="
TEXT="--text="
EDITTEXT="--text-info --editable --filename="
FILESELECT="--file-selection"
MULTIFILESELECT="--file-selection --multiple"
DIRSELECT="--file-selection --directory"
ENTRY="--entry "
ENTRYTEXT="--entry-text "
MENU="--list --column=Pick --column=Info"
YESNO="--question "
MSGBOX="--info "
PASSWORD="--entry --hide-text "
TITLETEXT="Deb Builder"
TEMPFOLDER="/tmp/deb-builder-temp"


if [ "$DIALOG" = "" ]; then
echo "Cannot find zenity.  Exiting."
exit 1
fi


$DIALOGMENU $TITLE"$TITLETEXT" $YESNO $TEXT"This is a tool to create a simple debian package.\n\nPlease make sure all binaries and support files for your package are properly installed on the system before attempting to create a package as this tool will place them in the package exactly where they are found on the system.\n\nEg.Your binaries should be in either \/usr\/bin or \/usr\/local\/bin if you are creating a binary app package.\n\nMenu entries will be created by this tool and you can edit them before final package creation.\n\nDo you want to continue?"

if [ "$?" != "0" ]; then
exit 0
fi

while [ "$PACKMENU" != "QUIT" ]; do

PACKMENU=`$DIALOGMENU $TITLE"$TITLETEXT" $MENU $TEXT"\nPlease choose whether to:\ncreate a new package from scratch,\nrepack an existing package,\nor unpack an existing deb file\n\n" NEW "Create a new package" REPACK "Repackage an existing package" UNPACK "Unpack an existing deb file" QUIT "Exit"`

if [ "$?" != "0" ]; then
exit 0
fi

if [ "$PACKMENU" = "QUIT" ]; then
exit 0
fi

if [ "$PACKMENU" = "REPACK" ]; then
#Mode for repacking due to tweaking after initial package creation

REPKFOLDER=`$DIALOG $TITLE"Select the folder of the package to repack" $DIRSELECT`

APPDEBFOLDERNAME=`basename $REPKFOLDER`
TEMPFOLDER=`dirname $REPKFOLDER`

#End of repacking mode
elif [ "$PACKMENU" = "UNPACK" ]; then
#Mode for unpacking existing deb files
cd $HOME
UNPKDEB=`$DIALOG $TITLE"Select the deb package to unpack" $FILESELECT`
UNPAKDEB=`basename $UNPKDEB`
DEBDIR=${UNPAKDEB%.deb}
MAINDIR=`dirname $UNPKDEB`
CD $MAINDIR
DEBIANDIR="$MAINDIR/$DEBDIR/DEBIAN"
mkdir -p $MAINDIR/$DEBDIR/DEBIAN

dpkg -x $UNPKDEB $MAINDIR/$DEBDIR
dpkg -e $UNPKDEB $DEBIANDIR

$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n\n$UNPKDEB unpacked to $MAINDIR/$DEBDIR"

#End of Unpacking mode
else
#Mode for complete package creation from scratch

#-----info-----
APPDEBNAME=`$DIALOG $TITLE"$TITLETEXT" $TEXT"Please type in the name you want to use for the deb package.\n\nShould be in lowercase and the only special character allowed is '-' between names.\n\nEg. package-name or packagename" $ENTRY`
if [ "$?" != "0" ]; then
exit 0
fi

VERSION=`$DIALOG $TITLE"$TITLETEXT" $TEXT"Please type in the version number you want to use for the deb package.\n\nEg. 1.0-1" $ENTRY`
if [ "$?" != "0" ]; then
exit 0
fi

ARCH=`$DIALOG $TITLE"$TITLETEXT" $TEXT"Please type in the architecture type for the deb package.\n\nEg. i386 or all" $ENTRY`
if [ "$?" != "0" ]; then
exit 0
fi

DEVNAME=`$DIALOG $TITLE"$TITLETEXT" $TEXT"Please type in your name to be listed as Maintainer.\n\nEg. John Doe" $ENTRY`
if [ "$?" != "0" ]; then
exit 0
fi

DEVEMAIL=`$DIALOG $TITLE"$TITLETEXT" $TEXT"Please type in your email address to be listed as Maintainer.\n\nEg. John_Doe@email.org" $ENTRY`
if [ "$?" != "0" ]; then
exit 0
fi

DEPENDS=`$DIALOG $TITLE"$TITLETEXT" $TEXT"Please type in the names of the packages that your app depends on with a comma between names.\n\nEg. coreutils, bash, zenity" $ENTRY`
if [ "$?" != "0" ]; then
exit 0
fi

DESCRIPTION=`$DIALOG $TITLE"$TITLETEXT" $TEXT"Please type in the package description.\n\nEg. This does something special" $ENTRY`
if [ "$?" != "0" ]; then
exit 0
fi

APPDEBFOLDERNAME=$APPDEBNAME"_"$VERSION"_"$ARCH
mkdir -p $TEMPFOLDER/$APPDEBFOLDERNAME/DEBIAN
cp -a /etc/deb-builder/DEBIAN/* $TEMPFOLDER/$APPDEBFOLDERNAME/DEBIAN/
mkdir -p $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/doc/$APPDEBNAME

#--------------

$DIALOG $TITLE"$TITLETEXT" $YESNO $TEXT"\n\n\nDoes this package have any binaries that will need to be accessed from the menu?"
if [ "$?" = "0" ]; then

#---------------menu itmems----------
MENUPARTS=""
mkdir -p $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/applications
mkdir -p $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/menu

while [ "$MENUPARTS" != "DONE" ]; do


MENUPARTS=`$DIALOGMENU $TITLE"$TITLETEXT" $MENU $TEXT"\nYou will need to add at least 1 binary file.\n\nThis will also help you create the .desktop file for the normal menu and create a menu entry for the standard debian menu." ADD "Add an executable" DONE "Continue"`



if [ "$MENUPARTS" = "ADD" ]; then
# Create .desktop file and debian menu files
APPCATCHOICES="Audio Audio_Submenu Video Video_Submenu Development Development_Submenu Education Education_Submenu Game Game_Submenu Graphics Graphics_Submenu Network Network_Submenu Office Office_Submenu Settings Settings_Submenu System System_Submenu Utility Utility_Submenu"

APPCAT=`$DIALOGMENU $TITLE"$TITLETEXT" $MENU $TEXT"\nPlease select which submenu you want to add the application to." Exit "Quit" $APPCATCHOICES`

if [ "$APPCAT" = "Exit" ]; then
  $DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n\nExiting now."
  exit 1
fi
if [ "$APPCAT" = "Audio" ]; then
SECTION="Applications/Sound"
APPCAT="AudioVideo"
elif [ "$APPCAT" = "Video" ]; then
SECTION="Applications/Video"
APPCAT="AudioVideo"
elif [ "$APPCAT" = "Games" ]; then
SECTION="Games"
else
SECTION="Applications/$APPCAT"
fi

cd /usr/local/bin
APPEXECBIN=`$DIALOG $TITLE"Select the Application to add to the $APPCAT Submenu" $FILESELECT`

if [ ! -x $APPEXECBIN ]; then
chmod 755 $APPEXECBIN
fi

APPEXECNAME=`basename $APPEXECBIN`
APPEXECFOLDER=`dirname $APPEXECBIN`
cd /usr/share/icons
APPICON=`$DIALOG $TITLE"Select the Icon for $APPEXECNAME" $FILESELECT`
APPICNNAME=`basename $APPICON`
APPICNFOLDER=`dirname $APPICON`

APPNAME=`$DIALOG $TITLE"$TITLETEXT" $TEXT"Please type in the name you want to show up in the Menu" $ENTRY`

APPCOMMENT=`$DIALOG $TITLE"$TITLETEXT" $TEXT"Please type in a brief description of what $APPNAME does" $ENTRY`

cat > $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/applications/$APPEXECNAME.desktop <<FOO
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=$APPNAME
GenericName=$APPNAME
Comment=$APPCOMMENT
Exec=$APPEXECBIN
Icon=$APPICON
Terminal=false
MultipleArgs=false
Type=Application
Categories=Applications;$APPCAT
GenericName[en_US]=$APPNAME

FOO

NEEDS="X11"

cat >> $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/menu/$APPDEBNAME <<FOO
?package($APPDEBNAME):needs="$NEEDS" section="$SECTION"\
        title="$APPNAME" command="$APPEXECBIN"
FOO

mkdir -p $TEMPFOLDER/$APPDEBFOLDERNAME/$APPEXECFOLDER
cp -a $APPEXECBIN $TEMPFOLDER/$APPDEBFOLDERNAME/$APPEXECFOLDER/
mkdir -p $TEMPFOLDER/$APPDEBFOLDERNAME/$APPICNFOLDER
cp -a $APPICON $TEMPFOLDER/$APPDEBFOLDERNAME/$APPICNFOLDER/

  $DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n\nCompleted adding $APPEXECBIN to the package."

fi

done
#-----------------------------------
fi

DATE=`date +"%a, %d %b %Y %T %z"`

#-----changelog.Debian-----
cat > $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/doc/$APPDEBNAME/changelog.Debian <<FOO
$APPDEBNAME ($VERSION) unstable; urgency=low

  *

 -- $DEVNAME <$DEVEMAIL>  $DATE

FOO

#--------------------------

#-----copyright-----
cd /usr/share/common-licenses/
COPYRIGHT=`$DIALOG $TITLE"Select the License for $APPDEBNAME" $FILESELECT`
cp $COPYRIGHT $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/doc/$APPDEBNAME/copyright
#-------------------

#-----extra files-----
EXTRAPARTS=""
cd /
while [ "$EXTRAPARTS" != "DONE" ]; do

EXTRAPARTS=`$DIALOGMENU $TITLE"$TITLETEXT" $MENU $TEXT"\nHere you can add files and folders to the package\n\n" FILE "Add a file to the package" FOLDER "Add a folder to the package" DONE "Continue"`

if [ "$EXTRAPARTS" = "FILE" ]; then
EXTRABIN=`$DIALOG $TITLE"Select the Application to add to the package" $FILESELECT`
EXTRANAME=`basename "$EXTRABIN"`
EXTRAFOLDER=`dirname "$EXTRABIN"`
mkdir -p "$TEMPFOLDER/$APPDEBFOLDERNAME/$EXTRAFOLDER"
cp -a "$EXTRABIN" "$TEMPFOLDER/$APPDEBFOLDERNAME/$EXTRAFOLDER/"
fi

if [ "$EXTRAPARTS" = "FOLDER" ]; then
EXTRADIR=`$DIALOG $TITLE"Select the Application to add to the package" $DIRSELECT`
mkdir -p "$TEMPFOLDER/$APPDEBFOLDERNAME/$EXTRADIR"
rsync -a "$EXTRADIR/" "$TEMPFOLDER/$APPDEBFOLDERNAME/$EXTRADIR/"
fi


done
#---------------------

#-----manual edit option-----
MANUALEDIT=""
cd $TEMPFOLDER/$APPDEBFOLDERNAME/
while [ "$MANUALEDIT" != "DONE" ]; do

MANUALEDIT=`$DIALOGMENU $TITLE"$TITLETEXT" $MENU $TEXT"\nHere you can check or edit all the files in the package\n\n" EDIT "Edit or check a file in the package" DONE "Continue"`

if [ "$MANUALEDIT" = "EDIT" ]; then
PICFILE=`$DIALOG $TITLE"Select the file to edit" $FILESELECT`
$DIALOG --height=500 --width=700 $EDITTEXT$PICFILE > $PICFILE.new
cp $PICFILE.new $PICFILE
rm $PICFILE.new
fi

done
#----------------------------


$DIALOG $TITLE"$TITLETEXT" $YESNO $TEXT"\n\n\nDo you want to build the package now?"
if [ "$?" != "0" ]; then
exit 0
fi

#End of Package creation from scratch
fi

if [ "$PACKMENU" != "UNPACK" ]; then
#Only do this stuff if we are packing or repacking
if [ "$PACKMENU" = "REPACK" ]; then
cd $TEMPFOLDER/$APPDEBFOLDERNAME
INSTALLEDSIZE=`du -s | awk '{print $1}'`
sed -i -e "s/^Installed-Size.*/Installed-Size: $INSTALLEDSIZE/g" $TEMPFOLDER/$APPDEBFOLDERNAME/DEBIAN/control
else
#-----control-----
cd $TEMPFOLDER/$APPDEBFOLDERNAME
INSTALLEDSIZE=`du -s | awk '{print $1}'`

cat > $TEMPFOLDER/$APPDEBFOLDERNAME/DEBIAN/control <<FOO
Package: $APPDEBNAME
Version: $VERSION
Architecture: $ARCH
Maintainer: $DEVNAME <$DEVEMAIL>
Installed-Size: $INSTALLEDSIZE
Depends: $DEPENDS
Section: $APPCAT
Priority: optional
Description: $DESCRIPTION

FOO
#-----------------
fi


#Prepare changelog files
for i in `ls $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/doc/`; do
#check if there are doc folder entries and gzip changelogs
if [ -e $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/doc/$i/changelog.Debian ]; then
gzip -9 $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/doc/$i/changelog.Debian
cp $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/doc/$i/changelog.Debian.gz $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/doc/$i/changelog.gz
rm -f $TEMPFOLDER/$APPDEBFOLDERNAME/usr/share/doc/$i/changelog.Debian
fi

done

#-----md5sums-----
cd $TEMPFOLDER/$APPDEBFOLDERNAME
find [a-z]* -type f | xargs md5sum >> $TEMPFOLDER/$APPDEBFOLDERNAME/DEBIAN/md5sums
#-----------------

cd $TEMPFOLDER
chown -R root:root $APPDEBFOLDERNAME
dpkg -b $APPDEBFOLDERNAME
tar czf $APPDEBFOLDERNAME.tar.gz $APPDEBFOLDERNAME 

$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n\nIf everything went well you should have packages named $APPDEBFOLDERNAME.deb and $APPDEBFOLDERNAME.tar.gz in the $TEMPFOLDER folder."

fi

done

exit 0


syntax highlighted by Code2HTML, v. 0.9.1