#!/bin/bash

###############################################
#            DEBRepacker v0.1 beta            #
#              Created by Fabius              #
#               4th August 2011               #
#         Follow me on twitter @fab1us        #
###############################################

###############################################
# > USAGE                                     #
###############################################
# Check readme.txt for details and examples!  #
###############################################
# repack --help            shows command syntax
# repack --version         shows repack version
#
# repack [[/path/]inputfile.deb [-O [[/path/]outputfile.deb]] | -A [/path/inputdir/] [-D [/path/outputdir/]]] [[-P [[/path/]Packages[.gz|.bz2]]] [-F [debsfolder]] [-H [msh]]] [-X] [-W [[/path/]pkglog.txt]]
#        [-a [Author]] [-b [Pre-Depends]] [-c [Depiction]] [-d [Depends]] [-e [dev]] [-f [Conflicts]] [-g [Provides]] [-h [Homepage]] [-i [Icon]] [-l [Replaces]] [-m [Maintainer]]
#        [-n [Name]] [-o [Sponsor]] [-p [Package]] [-r [Description]] [-s [Section]] [-t [Tag]] [-u [Architecture]] [-v [Version]] [-y [Priority]] [-z [Installed-Size]]
#
# IMPORTANT! Enclose parameter's values into quotes (i.e.: "value"), especially if they contain spaces (i.e.: "value with spaces")!
#
# Use "" (no value between quotes) to remove a parameter from the output DEBs control file.
# The following parameters CANNOT BE REMOVED as they are necessary fields of a control file (if you try to blank them with "" original values will be used):
# Package, Version
#
# inputfile.deb > input DEB to be repacked. It's possible to specify a /path/to/inputfile.deb or simply use inputfile.deb if it's placed in current working folder.
# -O > change output DEB name. If not specified, or if you do not provide a value for this, Package ID and Version of output DEB will be used as output DEB name, example: "com.myrepo.myapp_1.0_iphoneos-arm.deb"
#      It's possible to specify a /path/to/outputfile.deb or simply use outputfile.deb to place it in current working folder.
# -A > repack all DEBs in a folder. If you do not provide a value for this, current working folder will be used as input folder.
# -D > change output folder when repacking all DEBs of a folder. If not specified, or if you do not provide a value for this, current working folder will be used as output folder.
#      Package ID and Version of output DEBs will be used as output DEB name, example: "com.myrepo.myapp_1.0_iphoneos-arm.deb").
# -P > creates or updates a Packages file (plain text or gzip compressed or bzip2 compressed) for your repo with the new control files of repacked DEBs.
#      If you do not provide a value for this, a plain text Packages file will be created (or updated if file exists) in current working directory.
# -F > specifies the folder where debs will be hosted on your repo, to be written on Packages file, exampe: "debs" if your debs are under http://www.example.com/repo/debs/
#      If you do not provide a vlue for this, "debs" will be used as folder when writing on Packages file.
# -H > specifies the hashsum of the DEB, to be written on Packages file, exampe: "msh" (m = md5 | s = sha1 | h = sha256)
#      You can choose to write one, two or all three hashsums. If you do not provide a vlue for this only md5 hash will be written.
# -X > delete original DEB after repacking.
# -W > generate a text file containing a log of the control files of all repacked DEBs
#      If no file name is specified nor a default value is set, pkglog.txt in current working folder will be used.
# -a > change "Author" parameter value, example: "Fabius <fabius@fabius.fab>"
# -b > change "Pre-Depends" parameter value, example: "com.fabius.myappdepend"
# -c > change "Depiction" parameter value, example: "http://www.example.com/MyAppDepiction.php"
# -d > change "Depends" parameter value, example: "mobilesubstrate (>= 0.9.2587-1), libstatusbar | firmware (<< 4.0)"
# -e > change "dev" parameter value, example: "fabius"
# -f > change "Conflicts" parameter value, example: "com.fabius.badapp, com.fabius.anotherbadapp"
# -g > change "Provides" parameter value, example: "myapp"
# -h > change "Homepage" parameter value, example: "http://www.example.com/MyApp.php"
# -i > change "Icon" parameter value, example: "file:///Applications/MyApp.app/icon.png"
# -l > change "Replaces" parameter value, example: "com.fabius.oldapp"
# -m > change "Maintainer" parameter value, example: "Fabius <fabius@fabius.fab>"
# -n > change "Name" parameter value, example: "My App"
# -o > change "Sponsor" parameter value, example: "Fabius <http://twitter.com/fab1us>"
# -p > change "Package" parameter value, example: "com.myrepo" (NO DOT AT THE END! It will be automatically added to result like: com.myrepo.myapp)
#      This parameter cannot be removed from control file as it's necessary. If you try to blank it, original value will be used.
# -r > change "Description" parameter value, example: "This is my app!"
# -s > change "Section" parameter value, example: "Fabius Apps"
# -t > change "Tag" parameter value, example: "purpose::extension, cydia::commercial"
# -u > change "Architecture" parameter value, example: "iphoneos-arm"
# -v > change "Version" parameter value, example: "1.0"
#      This parameter cannot be removed from control file as it's necessary. If you try to blank it, original value will be used.
# -y > change "Priority" parameter value, example: "optional"
# -z > change "Installed-Size" parameter value (number in Bytes), example: "12345"
###############################################

###############################################
# > CONFIG                                    #
###############################################
# Input something between "" to set default   #
# values or simply leave blank.               #
# Check readme.txt for instructions!          #
###############################################
# Delete input DEB automatically (0 = off, 1 = on)
# !!! PAY ATTENTION WITH THIS !!!
CONST_AutoDelete=0
# Name for Packages file
# YOU MUST TYPE A VALID NAME AND AN EXISTING PATH!
CONST_PackagesFile=""
# Name for new packages control files log
# YOU MUST TYPE A VALID NAME AND AN EXISTING PATH!
CONST_PackagesLog=""
# Author:
CONST_Author=""
# Depiction:
CONST_Depiction=""
# Pre-Depends:
CONST_PreDepends=""
# Depends:
CONST_Depends=""
# dev:
CONST_dev=""
# Conflicts:
CONST_Conflicts=""
# Provides:
CONST_Provides=""
# Homepage:
CONST_Homepage=""
# Icon:
CONST_Icon=""
# Replaces:
CONST_Replaces=""
# Maintainer:
CONST_Maintainer=""
# Name:
CONST_Name=""
# Sponsor:
CONST_Sponsor=""
# Package:
CONST_Package=""
# Description:
CONST_Description=""
# Section:
CONST_Section=""
# Tag:
CONST_Tag=""
# Architecture:
CONST_Architecture=""
# Version:
CONST_Version=""
# Priority:
CONST_Priority=""
# Installed-Size:
CONST_InstalledSize=""
###############################################

###############################################
# !!! DONT TOUCH ANYTHING AFTER THIS LINE !!! #
###############################################

###############
# Help function
###############

function ShowHelp
{
	echo ""
	echo "Check readme.txt for details and examples"
	echo ""
	echo "repack --help            shows command syntax"
	echo "repack --version         shows repack version"
	echo ""
	echo "repack [[/path/]inputfile.deb [-O [[/path/]outputfile.deb]] | -A [/path/inputdir/] [-D [/path/outputdir/]]] [[-P [[/path/]Packages[.gz|.bz2]]] [-F [debsfolder]] [-H [msh]]] [-X] [-W [[/path/]pkglog.txt]]"
	echo "       [-a [Author]] [-b [Pre-Depends]] [-c [Depiction]] [-d [Depends]] [-e [dev]] [-f [Conflicts]] [-g [Provides]] [-h [Homepage]] [-i [Icon]] [-l [Replaces]] [-m [Maintainer]]"
	echo "       [-n [Name]] [-o [Sponsor]] [-p [Package]] [-r [Description]] [-s [Section]] [-t [Tag]] [-u [Architecture]] [-v [Version]] [-y [Priority]] [-z [Installed-Size]]"
	echo ""
	echo "IMPORTANT! Enclose parameter's values into quotes (i.e.: \"value\"), especially if they contain spaces (i.e.: \"value with spaces\")!"
	echo ""
	echo "Use \"\" (no value between quotes) to remove a parameter from the output DEBs control file."
	echo "The following parameters CANNOT BE REMOVED as they are necessary fields of a control file (if you try to blank them with \"\" original values will be used):"
	echo "Package, Version"
	echo ""
	echo "inputfile.deb > input DEB to be repacked. It's possible to specify a /path/to/inputfile.deb or simply use inputfile.deb if it's placed in current working folder."
	echo "-O > change output DEB name. If not specified, or if you do not provide a value for this, Package ID and Version of output DEB will be used as output DEB name, example: \"com.myrepo.myapp_1.0_iphoneos-arm.deb\""
	echo "     It's possible to specify a /path/to/outputfile.deb or simply use outputfile.deb to place it in current working folder."
	echo "-A > repack all DEBs in a folder. If you do not provide a value for this, current working folder will be used as input folder."
	echo "-D > change output folder when repacking all DEBs of a folder. If not specified, or if you do not provide a value for this, current working folder will be used as output folder."
	echo "     Package ID and Version of output DEBs will be used as output DEB name, example: \"com.myrepo.myapp_1.0_iphoneos-arm.deb\")."
	echo "-P > creates or updates a Packages file (plain text or gzip compressed or bzip2 compressed) for your repo with the new control files of repacked DEBs."
	echo "     If you do not provide a value for this, a plain text Packages file will be created (or updated if file exists) in current working directory."
	echo "-F > specifies the folder where debs will be hosted on your repo, to be written on Packages file, exampe: \"debs\" if your debs are under http://www.example.com/repo/debs/"
	echo "     If you do not provide a vlue for this, \"debs\" will be used as folder when writing on Packages file."
	echo "-H > specifies the hashsum of the DEB, to be written on Packages file, exampe: \"msh\" (m = md5 | s = sha1 | h = sha256)"
	echo "     You can choose to write one, two or all three hashsums. If you do not provide a vlue for this only md5 hash will be written."
	echo "-X > delete original DEB after repacking."
	echo "-W > generate a text file containing a log of the control files of all repacked DEBs"
	echo "     If no file name is specified nor a default value is set, pkglog.txt in current working folder will be used."
	echo "-a > change \"Author\" parameter value, example: \"Fabius <fabius@fabius.fab>\""
	echo "-b > change \"Pre-Depends\" parameter value, example: \"com.fabius.myappdepend\""
	echo "-c > change \"Depiction\" parameter value, example: \"http://www.example.com/MyAppDepiction.php\""
	echo "-d > change \"Depends\" parameter value, example: \"mobilesubstrate (>= 0.9.2587-1), libstatusbar | firmware (<< 4.0)\""
	echo "-e > change \"dev\" parameter value, example: \"fabius\""
	echo "-f > change \"Conflicts\" parameter value, example: \"com.fabius.badapp, com.fabius.anotherbadapp\""
	echo "-g > change \"Provides\" parameter value, example: \"myapp\""
	echo "-h > change \"Homepage\" parameter value, example: \"http://www.example.com/MyApp.php\""
	echo "-i > change \"Icon\" parameter value, example: \"file:///Applications/MyApp.app/icon.png\""
	echo "-l > change \"Replaces\" parameter value, example: \"com.fabius.oldapp\""
	echo "-m > change \"Maintainer\" parameter value, example: \"Fabius <fabius@fabius.fab>\""
	echo "-n > change \"Name\" parameter value, example: \"My App\""
	echo "-o > change \"Sponsor\" parameter value, example: \"Fabius <http://twitter.com/fab1us>\""
	echo "-p > change \"Package\" parameter value, example: \"com.myrepo\" (NO DOT AT THE END! It will be automatically added to result like: com.myrepo.myapp)"
	echo "     This parameter cannot be removed from control file as it's necessary. If you try to blank it, original value will be used"
	echo "-r > change \"Description\" parameter value, example: \"This is my app!\""
	echo "-s > change \"Section\" parameter value, example: \"Fabius Apps\""
	echo "-t > change \"Tag\" parameter value, example: \"purpose::extension, cydia::commercial\""
	echo "-u > change \"Architecture\" parameter value, example: \"iphoneos-arm\""
	echo "-v > change \"Version\" parameter value, example: \"1.0\""
	echo "     This parameter cannot be removed from control file as it's necessary. If you try to blank it, original value will be used"
	echo "-y > change \"Priority\" parameter value, example: \"optional\""
	echo "-z > change \"Installed-Size\" parameter value (number in Bytes), example: \"12345\""
	echo ""
	exit 0
}

##################
# Version function
##################

function ShowVersion
{
	echo "DEBRepacker v0.1 beta (4th August 2011) - By Fabius [ http://twitter.com/fab1us ]"
}

#########################
# DEB validation function
#########################

function CheckDEB
{
	IFS=$'\n'
	DEB=$1
	set $(cat $1)
	if [[ $1 != "!<arch>" || $2 != debian-binary* ]]; then
		return 0
	else
		return 1
	fi
}

#########################
# BZ2 validation function
#########################

function CheckBZ2
{
	IFS=$'\n'
	DEB=$1
	set $(cat $1)
	if [[ $1 != BZh91AY\&SY* ]]; then
		return 0
	else
		return 1
	fi
}

########################
# GZ validation function
########################

function CheckGZ
{
	IFS=$'\n'
	DEB=$1
	set $(cat $1)
	if [[ $1 != * ]]; then
		return 0
	else
		return 1
	fi
}

#####################
# UnCompress function
#####################

function UnCompress
{
	if [[ $2 == "gz" ]]; then
		CheckGZ $1
		if [[ $? = 0 ]]; then
			echo "[ERROR] Packages file is not a .gz file! (different format)"
			exit 0
		fi
		gzip -d $1
	elif [[ $2 == "bz2" ]]; then
		CheckBZ2 $1
		if [[ $? = 0 ]]; then
			echo "[ERROR] Packages file is not a .bz2 file! (different format)"
			exit 0
		fi
		bzip2 -d $1
	fi
}

###################
# Compress function
###################

function Compress
{
	if [[ $2 == "gz" ]]; then
		gzip -9 $1
	elif [[ $2 == "bz2" ]]; then
		bzip2 -9 $1
	fi
}

##################
# Logging function
##################

function LogIt
{
	#rm -rf ${2}
	echo "" >> ${2}
	while read line
	do
		echo "$line" >> ${2}
	done < ${1}
	echo "" >> ${2}
}

############################
# Packages file SnD function
############################

function SearchAndDestroy
{
	i=0
	while read line
	do
		let i++
		if [[ $line == *$1* ]]; then
			break
		fi
	done < ${2}
	for (( j=$i;j>0;j-- )); do
		x=$(sed -n "$j"p ${2})
		if [[ $x != "" ]]; then
			let i--
		else
			break
		fi
	done
	for (( j=0;j<100;j++ )); do
		x=$(sed -n "$((i+1))"p ${2})
		if [[ $x != "" ]]; then
			sed -i "$((i+1))"d ${2}
		else
			break
		fi
	done
	sed -i "$((i+1))"d ${2}
}

###################
# Packages function
###################

function PackageIt
{
	#rm -rf ${2}
	pkgsdir=$(dirname ${2})
	name=$(basename ${2})
	theFile=$pkgsdir/Packages
	if [[ $name == "Packages.gz" ]]; then
		type=1
		if [[ -e ${2} ]]; then
			UnCompress ${2} gz
		fi
	elif [[ $name == "Packages.bz2" ]]; then
		type=2
		if [[ -e ${2} ]]; then
			UnCompress ${2} bz2
		fi
	elif [[ $name == "Packages" ]]; then
		type=3
	fi
	if [[ -e ${2} ]]; then
		SearchAndDestroy $3 ${theFile}
	fi
	echo "" >> ${theFile}
	while read line
	do
		echo "$line" >> ${theFile}
	done < ${1}
	echo "Filename: $4" >> ${theFile}
	echo "Size: $5" >> ${theFile}
	if [[ $5 != "" ]]; then
		echo "MD5sum: $6" >> ${theFile}
	fi
	if [[ $6 != "" ]]; then
		echo "SHA1sum: $7" >> ${theFile}
	fi
	if [[ $7 != "" ]]; then
		echo "SHA256sum: $8" >> ${theFile}
	fi
	echo "" >> ${theFile}
	if [[ $type -eq 1 ]]; then
		Compress $theFile gz
	elif [[ $type -eq 2 ]]; then
		Compress $theFile bz2
	fi
}

####################
# Repacking function
####################

function DoRepackIt
{
	########################################
	# Get all parameters except for last one
	########################################

	tparam=$#
	
	for (( param=2; param<$tparam; param++ ))
	do
		param2=$((param+1))
		param2="$param2"
		paramX=${!param2}
		if [[ ${!param} == "-O" ]]; then
			if [[ $VAR_OutputFolder != "" ]]; then
				echo "[ERROR] You cannot use -O and -D parameters together!"
				exit 0
			fi
			VAR_OutputFile="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_OutputFile="n"
			else
				VALUE_OutputFile=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-D" ]]; then
			if [[ $VAR_OutputFile != "" ]]; then
				echo "[ERROR] You cannot use -O and -D parameters together!"
				exit 0
			fi
			VAR_OutputFolder="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_OutputFolder="n"
			else
				VALUE_OutputFolder=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-P" ]]; then
			VAR_PackagesFile="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_PackagesFile="n"
			else
				VALUE_PackagesFile=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-F" ]]; then
			VAR_OnlineFolder="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_OnlineFolder="n"
			else
				VALUE_OnlineFolder=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-H" ]]; then
			VAR_HashSum="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_HashSum="n"
			else
				VALUE_HashSum=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-X" ]]; then
			VAR_AutoDelete=1
		fi
		if [[ ${!param} == "-W" ]]; then
			VAR_PackagesLog="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_PackagesLog="n"
			else
				VALUE_PackagesLog=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-a" ]]; then
			VAR_Author="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Author="n"
			else
				VALUE_Author=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-b" ]]; then
			VAR_PreDepends="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_PreDepends="n"
			else
				VALUE_PreDepends=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-c" ]]; then
			VAR_Depiction="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Depiction="n"
			else
				VALUE_Depiction=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-d" ]]; then
			VAR_Depends="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Depends="n"
			else
				VALUE_Depends=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-e" ]]; then
			VAR_dev="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_dev="n"
			else
				VALUE_dev=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-f" ]]; then
			VAR_Conflicts="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Conflicts="n"
			else
				VALUE_Conflicts=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-g" ]]; then
			VAR_Provides="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Provides="n"
			else
				VALUE_Provides=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-h" ]]; then
			VAR_Homepage="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Homepage="n"
			else
				VALUE_Homepage=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-i" ]]; then
			VAR_Icon="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Icon="n"
			else
				VALUE_Icon=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-l" ]]; then
			VAR_Replaces="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Replaces="n"
			else
				VALUE_Replaces=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-m" ]]; then
			VAR_Maintainer="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Maintainer="n"
			else
				VALUE_Maintainer=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-n" ]]; then
			VAR_Name="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Name="n"
			else
				VALUE_Name=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-o" ]]; then
			VAR_Sponsor="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Sponsor="n"
			else
				VALUE_Sponsor=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-p" ]]; then
			VAR_Package="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Package="n"
			else
				VALUE_Package=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-r" ]]; then
			VAR_Description="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Description="n"
			else
				VALUE_Description=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-s" ]]; then
			VAR_Section="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Section="n"
			else
				VALUE_Section=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-t" ]]; then
			VAR_Tag="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Tag="n"
			else
				VALUE_Tag=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-u" ]]; then
			VAR_Architecture="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Architecture="n"
			else
				VALUE_Architecture=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-v" ]]; then
			VAR_Version="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Version="n"
			else
				VALUE_Version=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-y" ]]; then
			VAR_Priority="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_Priority="n"
			else
				VALUE_Priority=${!param2}
				let param++
			fi
		fi
		if [[ ${!param} == "-z" ]]; then
			VAR_InstalledSize="y"
			if [[ ${#paramX} -eq 2 && ${!param2} == -* ]]; then
				VAR_InstalledSize="n"
			else
				VALUE_InstalledSize=${!param2}
				let param++
			fi
		fi
	done
	
	####################
	# Get last parameter
	####################
	
		if [[ ${!tparam} == "-O" ]]; then
			VAR_OutputFile="n"
		fi
		if [[ ${!tparam} == "-D" ]]; then
			VAR_OutputFolder="n"
		fi
		if [[ ${!tparam} == "-P" ]]; then
			VAR_PackagesFile="n"
		fi
		if [[ ${!tparam} == "-F" ]]; then
			VAR_PackagesFile="n"
		fi
		if [[ ${!tparam} == "-H" ]]; then
			VAR_PackagesFile="n"
		fi
		if [[ ${!tparam} == "-X" ]]; then
			VAR_AutoDelete=1
		fi
		if [[ ${!tparam} == "-W" ]]; then
			VAR_PackagesLog="n"
		fi
		if [[ ${!tparam} == "-a" ]]; then
			VAR_Author="n"
		fi
		if [[ ${!tparam} == "-b" ]]; then
			VAR_PreDepends="n"
		fi
		if [[ ${!tparam} == "-c" ]]; then
			VAR_Depiction="n"
		fi
		if [[ ${!tparam} == "-d" ]]; then
			VAR_Depends="n"
		fi
		if [[ ${!tparam} == "-e" ]]; then
			VAR_dev="n"
		fi
		if [[ ${!tparam} == "-f" ]]; then
			VAR_Conflicts="n"
		fi
		if [[ ${!tparam} == "-g" ]]; then
			VAR_Provides="n"
		fi
		if [[ ${!tparam} == "-h" ]]; then
			VAR_Homepage="n"
		fi
		if [[ ${!tparam} == "-i" ]]; then
			VAR_Icon="n"
		fi
		if [[ ${!tparam} == "-l" ]]; then
			VAR_Replaces="n"
		fi
		if [[ ${!tparam} == "-m" ]]; then
			VAR_Maintainer="n"
		fi
		if [[ ${!tparam} == "-n" ]]; then
			VAR_Name="n"
		fi
		if [[ ${!tparam} == "-o" ]]; then
			VAR_Sponsor="n"
		fi
		if [[ ${!tparam} == "-p" ]]; then
			VAR_Package="n"
		fi
		if [[ ${!tparam} == "-r" ]]; then
			VAR_Description="n"
		fi
		if [[ ${!tparam} == "-s" ]]; then
			VAR_Section="n"
		fi
		if [[ ${!tparam} == "-t" ]]; then
			VAR_Tag="n"
		fi
		if [[ ${!tparam} == "-u" ]]; then
			VAR_Architecture="n"
		fi
		if [[ ${!tparam} == "-v" ]]; then
			VAR_Version="n"
		fi
		if [[ ${!tparam} == "-y" ]]; then
			VAR_Priority="n"
		fi
		if [[ ${!tparam} == "-z" ]]; then
			VAR_InstalledSize="n"
		fi
		if [[ $VALUE_PackagesLog != "" && ! -e $(dirname $VALUE_PackagesLog) ]]; then
			echo "[ERROR] Packages Log file cannot be created cause the folder you specified doesnt exists!"
			exit 0
		fi
		if [[ $VALUE_PackagesLog != "" && -d ${VALUE_PackagesLog} ]]; then
			if [[ ! -e $VALUE_PackagesLog ]]; then
				echo "[ERROR] Packages Log file cannot be created cause the folder you specified doesnt exists!"
				exit 0
			fi
			VALUE_PackagesLog=$VALUE_PackagesLog/pkglog.txt
		fi
		if [[ $VALUE_PackagesFile != "" && ! -e $(dirname $VALUE_PackagesFile) ]]; then
			echo "[ERROR] Packages file cannot be created cause the folder you specified doesnt exists!"
			exit 0
		fi
		if [[ $VALUE_PackagesFile != "" && -d ${VALUE_PackagesFile} ]]; then
			if [[ ! -e $VALUE_PackagesFile ]]; then
				echo "[ERROR] Packages file cannot be created cause the folder you specified doesnt exists!"
				exit 0
			fi
			VALUE_PackagesFile=$VALUE_PackagesFile/Packages
		fi
	
	###############################
	# Initialize process and unpack
	###############################
	
	if [[ $VAR_OutputFile == "y" ]]; then
		if [[ $VALUE_OutputFile != "" ]]; then
			VALUE_OutputFile=${VALUE_OutputFile}
			outdir=$(dirname $VALUE_OutputFile)
			if [[ ! -e $outdir ]]; then
				echo "[ERROR] Output folder does not exist!"
				exit 0
			fi
			newname=$(basename $VALUE_OutputFile)
			if [[ $newname != *.deb ]]; then
				newname="$newname".deb
			fi
		else
			remember=1
		fi
	else
		remember=1
	fi
	if [[ $VAR_OutputFolder == "y" ]]; then
		if [[ $VALUE_OutputFolder != "" ]]; then
			VALUE_OutputFolder=${VALUE_OutputFolder}
			outdir=$VALUE_OutputFolder
			if [[ ! -e $outdir ]]; then
				echo "[ERROR] Output folder does not exist!"
				exit 0
			fi
			remember=3
		else
			if [[ $VALUE_OutputFile == "" ]]; then
				remember=2
			fi
		fi
	else
		if [[ $VALUE_OutputFile == "" ]]; then
			remember=2
		fi
	fi
	if [[ $1 == */* ]]; then
		dir=$(echo $1 | sed 's/.\{4\}$//')
	else
		dir=$(dirs)/$(echo $1 | sed 's/.\{4\}$//')
	fi
	mkdir "$dir"_fabius
	mkdir "$dir"_fabius/DEBIAN
	theDEBname=$1
	dpkg --control "$1" "$dir"_fabius/DEBIAN
	dpkg -x "$1" "$dir"_fabius
	
	##############################
	# Make changes to control file
	##############################
	
	rm -rf "$dir"_fabius/DEBIAN/lortnoc
	while read line
	do
		cSTRING=$(echo "$line")
		if [[ $cSTRING == Author:* ]]; then
			if [[ $VAR_Author == "y" ]]; then
				if [[ $VALUE_Author != "" ]]; then
					echo "Author: $VALUE_Author" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Author == "n" ]]; then
				if [[ $CONST_Author != "" ]]; then
					echo "Author: $CONST_Author" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Pre-Depends:* ]]; then
			if [[ $VAR_PreDepends == "y" ]]; then
				if [[ $VALUE_PreDepends != "" ]]; then
					echo "Pre-Depends: $VALUE_PreDepends" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_PreDepends == "n" ]]; then
				if [[ $CONST_PreDepends != "" ]]; then
					echo "Pre-Depends: $CONST_PreDepends" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Depiction:* ]]; then
			if [[ $VAR_Depiction == "y" ]]; then
				if [[ $VALUE_Depiction != "" ]]; then
					echo "Depiction: $VALUE_Depiction" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Depiction == "n" ]]; then
				if [[ $CONST_Depiction != "" ]]; then
					echo "Depiction: $CONST_Depiction" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Depends:* ]]; then
			if [[ $VAR_Depends == "y" ]]; then
				if [[ $VALUE_Depends != "" ]]; then
					echo "Depends: $VALUE_Depends" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Depends == "n" ]]; then
				if [[ $CONST_Depends != "" ]]; then
					echo "Depends: $CONST_Depends" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == dev:* ]]; then
			if [[ $VAR_dev == "y" ]]; then
				if [[ $VALUE_dev != "" ]]; then
					echo "dev: $VALUE_dev" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_dev == "n" ]]; then
				if [[ $CONST_dev != "" ]]; then
					echo "dev: $CONST_dev" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Conflicts:* ]]; then
			if [[ $VAR_Conflicts == "y" ]]; then
				if [[ $VALUE_Conflicts != "" ]]; then
					echo "Conflicts: $VALUE_Conflicts" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Conflicts == "n" ]]; then
				if [[ $CONST_Conflicts != "" ]]; then
					echo "Conflicts: $CONST_Conflicts" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Provides:* ]]; then
			if [[ $VAR_Provides == "y" ]]; then
				if [[ $VALUE_Provides != "" ]]; then
					echo "Provides: $VALUE_Provides" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Provides == "n" ]]; then
				if [[ $CONST_Provides != "" ]]; then
					echo "Provides: $CONST_Provides" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Homepage:* ]]; then
			if [[ $VAR_Homepage == "y" ]]; then
				if [[ $VALUE_Homepage != "" ]]; then
					echo "Homepage: $VALUE_Homepage" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Homepage == "n" ]]; then
				if [[ $CONST_Homepage != "" ]]; then
					echo "Homepage: $CONST_Homepage" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Icon:* ]]; then
			if [[ $VAR_Icon == "y" ]]; then
				if [[ $VALUE_Icon != "" ]]; then
					echo "Icon: $VALUE_Icon" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Icon == "n" ]]; then
				if [[ $CONST_Icon != "" ]]; then
					echo "Icon: $CONST_Icon" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Replaces:* ]]; then
			if [[ $VAR_Replaces == "y" ]]; then
				if [[ $VALUE_Replaces != "" ]]; then
					echo "Replaces: $VALUE_Replaces" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Replaces == "n" ]]; then
				if [[ $CONST_Replaces != "" ]]; then
					echo "Replaces: $CONST_Replaces" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Maintainer:* ]]; then
			if [[ $VAR_Maintainer == "y" ]]; then
				if [[ $VALUE_Maintainer != "" ]]; then
					echo "Maintainer: $VALUE_Maintainer" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Maintainer == "n" ]]; then
				if [[ $CONST_Maintainer != "" ]]; then
					echo "Maintainer: $CONST_Maintainer" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Name:* ]]; then
			if [[ $VAR_Name == "y" ]]; then
				if [[ $VALUE_Name != "" ]]; then
					echo "Name: $VALUE_Name" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Name == "n" ]]; then
				if [[ $CONST_Name != "" ]]; then
					echo "Name: $CONST_Name" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Sponsor:* ]]; then
			if [[ $VAR_Sponsor == "y" ]]; then
				if [[ $VALUE_Sponsor != "" ]]; then
					echo "Sponsor: $VALUE_Sponsor" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Sponsor == "n" ]]; then
				if [[ $CONST_Sponsor != "" ]]; then
					echo "Sponsor: $CONST_Sponsor" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Package:* ]]; then
			IFS="."
			set -- $cSTRING
			theNum=$#
			unset IFS
			id=$(echo $cSTRING |cut -f"$theNum" -d".")
			if [[ $VALUE_Package != "" ]]; then
				APPNAME=$VALUE_Package.$id
			else
				APPNAME=$(echo $cSTRING | sed 's:Package\x3A\x20::')
			fi
			if [[ $VAR_Package == "y" ]]; then
				if [[ $VALUE_Package != "" ]]; then
					echo "Package: $VALUE_Package.$id" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Package == "n" ]]; then
				if [[ $CONST_Package != "" ]]; then
					echo "Package: $CONST_Package.$id" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Description:* ]]; then
			if [[ $VAR_Description == "y" ]]; then
				if [[ $VALUE_Description != "" ]]; then
					echo "Description: $VALUE_Description" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Description == "n" ]]; then
				if [[ $CONST_Description != "" ]]; then
					echo "Description: $CONST_Description" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Section:* ]]; then
			if [[ $VAR_Section == "y" ]]; then
				if [[ $VALUE_Section != "" ]]; then
					echo "Section: $VALUE_Section" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Section == "n" ]]; then
				if [[ $CONST_Section != "" ]]; then
					echo "Section: $CONST_Section" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Tag:* ]]; then
			if [[ $VAR_Tag == "y" ]]; then
				if [[ $VALUE_Tag != "" ]]; then
					echo "Tag: $VALUE_Tag" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Tag == "n" ]]; then
				if [[ $CONST_Tag != "" ]]; then
					echo "Tag: $CONST_Tag" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Architecture:* ]]; then
			if [[ $VAR_Architecture == "y" ]]; then
				if [[ $VALUE_Architecture != "" ]]; then
					echo "Architecture: $VALUE_Architecture" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Architecture == "n" ]]; then
				if [[ $CONST_Architecture != "" ]]; then
					echo "Architecture: $CONST_Architecture" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Version:* ]]; then
			if [[ $VALUE_Version != "" ]]; then
				APPNAME=$VALUE_Version
			else
				APPVERSION=$(echo $cSTRING | sed 's:Version\x3A\x20::')
			fi
			if [[ $VAR_Version == "y" ]]; then
				if [[ $VALUE_Version != "" ]]; then
					echo "Version: $VALUE_Version" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Version == "n" ]]; then
				if [[ $CONST_Version != "" ]]; then
					echo "Version: $CONST_Version" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Priority:* ]]; then
			if [[ $VAR_Priority == "y" ]]; then
				if [[ $VALUE_Priority != "" ]]; then
					echo "Priority: $VALUE_Priority" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_Priority == "n" ]]; then
				if [[ $CONST_Priority != "" ]]; then
					echo "Priority: $CONST_Priority" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
		if [[ $cSTRING == Installed-Size:* ]]; then
			if [[ $VAR_InstalledSize == "y" ]]; then
				if [[ $VALUE_InstalledSize != "" ]]; then
					echo "Installed-Size: $VALUE_InstalledSize" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			elif [[ $VAR_InstalledSize == "n" ]]; then
				if [[ $CONST_InstalledSize != "" ]]; then
					echo "Installed-Size: $CONST_InstalledSize" >> "$dir"_fabius/DEBIAN/lortnoc
				else
					echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
				fi
			else
				echo "$line" >> "$dir"_fabius/DEBIAN/lortnoc
			fi
		fi
	done < "$dir"_fabius/DEBIAN/control
	if [[ $remember -eq 1 ]]; then
		outdir=$(dirs)
		newname="$APPNAME"_"$APPVERSION"_iphoneos-arm.deb
	elif [[ $remember -eq 2 ]]; then
		outdir=$(dirs)
		newname="$APPNAME"_"$APPVERSION"_iphoneos-arm.deb
	elif [[ $remember -eq 3 ]]; then
		newname="$APPNAME"_"$APPVERSION"_iphoneos-arm.deb
	fi
	cPERM="$(stat -c %a "$dir"_fabius/DEBIAN/control)"
	cOWN="$(stat -c %U "$dir"_fabius/DEBIAN/control)"
	cGRP="$(stat -c %G "$dir"_fabius/DEBIAN/control)"
	rm -rf "$dir"_fabius/DEBIAN/control
	mv -f ""$dir"_fabius/DEBIAN/lortnoc" ""$dir"_fabius/DEBIAN/control"
	chmod $cPERM "$dir"_fabius/DEBIAN/control
	chown $cOWN:$cGRP "$dir"_fabius/DEBIAN/control
	
	###############################################
	# Repack, log, rebuild Packages and end process
	###############################################
	
	dpkg -b ""$dir"_fabius" >/dev/null 2>&1
	if [[ $VAR_PackagesLog == "y" ]]; then
		if [[ $VALUE_PackagesLog != "" ]]; then
			LogIt ""$dir"_fabius/DEBIAN/control" "$VALUE_PackagesLog"
		else
			if [[ $CONST_PackagesLog != "" ]]; then
				LogIt ""$dir"_fabius/DEBIAN/control" "$CONST_PackagesLog"
			else
				LogIt ""$dir"_fabius/DEBIAN/control" "pkglog.txt"
			fi
		fi
	elif [[ $VAR_PackagesLog == "n" ]]; then
		if [[ $CONST_PackagesLog != "" ]]; then
			LogIt ""$dir"_fabius/DEBIAN/control" "$CONST_PackagesLog"
		else
			LogIt ""$dir"_fabius/DEBIAN/control" "pkglog.txt"
		fi
	fi
	if [[ $VALUE_OnlineFolder != "" ]]; then
		onlinefolder=$VALUE_OnlineFolder
		if [[ $onlinefolder != /* ]]; then
			onlinefolder=/$onlinefolder
		fi
		if [[ $onlinefolder != */ ]]; then
			onlinefolder=$onlinefolder/
		fi
	else
		onlinefolder=/debs/
	fi
	filesizeof=""$dir"_fabius.deb"
	theSize=$(stat -c%s "${filesizeof}")
	if [[ $VALUE_HashSum != "" ]]; then
		if [[ $VALUE_HashSum == *m* ]]; then
			theMD5sum=$(md5sum "${filesizeof}")
			theMD5sum=${theMD5sum%% *}
		fi
		if [[ $VALUE_HashSum == *s* ]]; then
			theSHA1sum=$(sha1sum "${filesizeof}")
			theSHA1sum=${theSHA1sum%% *}
		fi
		if [[ $VALUE_HashSum == *h* ]]; then
			theSHA256sum=$(sha256sum "${filesizeof}")
			theSHA256sum=${theSHA256sum%% *}
		fi
	else
		theMD5sum=$(md5sum "${filesizeof}")
		theMD5sum=${theMD5sum%% *}
	fi
	if [[ $VAR_PackagesFile == "y" ]]; then
		if [[ $VALUE_PackagesFile != "" ]]; then
			PackageIt ""$dir"_fabius/DEBIAN/control" "$VALUE_PackagesFile" "$APPNAME" "$onlinefolder$newname" "$theSize" "$theMD5sum" "$theSHA1sum" "$theSHA256sum"
		else
			if [[ $CONST_PackagesFile != "" ]]; then
				PackageIt ""$dir"_fabius/DEBIAN/control" "$CONST_PackagesFile" "$onlinefolder$newname" "$theSize" "$theMD5sum" "$theSHA1sum" "$theSHA256sum"
			else
				PackageIt ""$dir"_fabius/DEBIAN/control" "Packages" "$onlinefolder$newname" "$theSize" "$theMD5sum" "$theSHA1sum" "$theSHA256sum"
			fi
		fi
	elif [[ $VAR_PackagesFile == "n" ]]; then
		if [[ $CONST_PackagesFile != "" ]]; then
			PackageIt ""$dir"_fabius/DEBIAN/control" "$CONST_PackagesFile" "$onlinefolder$newname" "$theSize" "$theMD5sum" "$theSHA1sum" "$theSHA256sum"
		else
			PackageIt ""$dir"_fabius/DEBIAN/control" "Packages" "$onlinefolder$newname" "$theSize" "$theMD5sum" "$theSHA1sum" "$theSHA256sum"
		fi
	fi

	rm -rf "$dir"_fabius
	mv ""$dir"_fabius.deb" "${outdir}/${newname}"
	if [[ $CONST_AutoDelete -eq 1 || $VAR_AutoDelete -eq 1 ]]; then
		rm -rf "${theDEBname}"
	fi
}

###########################################
# Parameters check before taking any action
###########################################

if [[ -z $1 ]]; then
	ShowHelp
	exit 0
fi
if [[ $1 == "--help" ]]; then
	ShowHelp
	exit 0
fi
if [[ $1 == "--version" ]]; then
	ShowVersion
	exit 0
fi
if [[ $1 != "-A" ]]; then
	if [[ $1 != *.deb ]]; then
		echo $1
		echo "[ERROR] Input file is not a DEBIAN file! (bad file extension)"
		exit 0
	fi
	if [[ ! -e $1 ]]; then
		echo "[ERROR] Input file does not exist!"
		exit 0
	else
		if [[ ! -s $1 ]]; then
			echo "[ERROR] Input file is not a DEBIAN file! (0 bytes)"
			exit 0
		fi
		CheckDEB $1
		if [[ $? = 0 ]]; then
			echo "[ERROR] Input file is not a DEBIAN file! (different format)"
			exit 0
		fi
	fi
	
	############
	# Repack it!
	############

	DoRepackIt "$@"
	oldname=$(basename $1)
	if [[ $outdir = "." ]]; then
		echo "[SUCCESS] Repacked $oldname as $newname in $(dirs)/"
	else
		if [[ $outdir == */ ]]; then
			echo "[SUCCESS] Repacked $oldname as $newname in $outdir"
		else
			echo "[SUCCESS] Repacked $oldname as $newname in $outdir/"
		fi
	fi
	exit 0
else

	#################
	# Repack 'em ALL!
	#################
	
	debscountrer=0
	if [[ ${#2} -ne 2 && ${!2} != -* ]]; then
		inputFolder4A=$2/
	else
		inputFolder4A=""
	fi
	for i in ${inputFolder4A}*.deb
	do
		if [[ ! -e $i ]]; then
			echo "[ERROR] No DEB files to repack found!"
			exit 0
		fi
		DoRepackIt "$i" "$@"
		let debscountrer++
	done
	if [[ $outdir = "." ]]; then
		if [[ $debscountrer -eq 1 ]]; then
			echo "[SUCCESS] Repacked $debscountrer DEB in $(dirs)/"
		else
			echo "[SUCCESS] Repacked $debscountrer DEBs in $(dirs)/"
		fi
	else
		if [[ $outdir == */ ]]; then
			if [[ $debscountrer -eq 1 ]]; then
				echo "[SUCCESS] Repacked $debscountrer DEB in $outdir"
			else
				echo "[SUCCESS] Repacked $debscountrer DEBs in $outdir"
			fi
		else
			if [[ $debscountrer -eq 1 ]]; then
				echo "[SUCCESS] Repacked $debscountrer DEB in $outdir/"
			else
				echo "[SUCCESS] Repacked $debscountrer DEBs in $outdir/"
			fi
		fi
	fi
	exit 0
fi
####### Congrats! You just reached EOF! #######
