#!/bin/bash

########################################################################################################     get Infos for package     #####################
getInfos () {

# INIT VARIABLES TO DEFAULT
     name="mkdeb"
     version="2.0"
     section="devel"
     priority="optional"
     arch="all"
     depends="bash,dpkg"
     size="7"
     maintainer="Marcel Hörz <marzel95@gmail.com>"
     homepage="marzl95.no-ip.org"
     description="You can make your own .deb-file out of one script with this tool."
     
     
     ok=0
     while [ $ok = 0 ]
     do
          clear
# READ INFORMATION OF PACKAGE
          echo ""
          echo "Information for 'control'-file will be read..."
          echo ""

          read -p "Package: [$name] " input
          name=${input:-$name}
          read -p "Version: [$version] " input
          version=${input:-$version}
          read -p "Section: (admin,devel,doc,editors,games,graphics,math,misc,science,utils) [$section] " input
          section=${input:-$section}
          read -p "Priority: (extra,optional) [$priority] " input
          priority=${input:-$priority}
          read -p "Architecture: (i386,amd64,all) [$arch] " input
          arch=${input:-$arch}
          read -p "Depends: [$depends] " input
          depends=${input:-$depends}
          read -p "Installed-Size: (in KB) [$size] " input
          size=${input:-$size}
          read -p "Maintainer: [$maintainer] " input
          maintainer=${input:-$maintainer}
          read -p "Homepage: [$homepage] " input
          homepage=${input:-$homepage}
          read -p "Description: [$description] " input
          description=${input:-$description}

          clear
# CHECK THESE INFORMATION.
          echo ""
          echo "Check information:"
          echo ""
          
          echo "Package: $name"
          echo "Version: $version"
          echo "Section: $section"
          echo "Priority: $priority"
          echo "Architecture: $arch"
          echo "Depends: $depends"
          echo "Installed-Size: $size"
          echo "Maintainer: $maintainer"
          echo "Homepage: $homepage"
          echo "Description: $description"

          echo ""
          echo -n "All right? [y/n] "
          read okk
               
          if [ $okk == y ]
          then
               ok=1
          fi
     done
}

#################################################################################################     writeout control-file     #########################

writeOutInfos () {
# Only able to do, if the directory "paket/${name}/DEBIAN" exists!!!

     echo ""
     echo "'control'-file will be written..."

     echo "Package: $name" > paket/${name}/DEBIAN/control
     echo "Version: $version" >> paket/${name}/DEBIAN/control
     echo "Section: $section" >> paket/${name}/DEBIAN/control
     echo "Priority: $priority" >> paket/${name}/DEBIAN/control
     echo "Architecture: $arch" >> paket/${name}/DEBIAN/control
     echo "Depends: $depends" >> paket/${name}/DEBIAN/control
     echo "Installed-Size: $size" >> paket/${name}/DEBIAN/control
     echo "Maintainer: $maintainer" >> paket/${name}/DEBIAN/control
     echo "Homepage: $homepage" >> paket/${name}/DEBIAN/control
     echo "Description: $description" >> paket/${name}/DEBIAN/control
     
     echo "'control'-file written."
     echo ""
}

####################################################################################################      make .deb-file     ##########################

makeDeb () {
# package the directory to deb-file

     echo ""
     echo "make deb-file..."
     dpkg -b ./paket/${name} $name.deb
     rm -rf paket
     echo ""

}

####################################################################################################     copy single-script-file    ##################

cpScript () {
# copy the Script to place

     echo ""
     echo "File will be copyed..."

     chmod +x $scipt
     mkdir -p paket/${name}/${spo}
     cp $scipt paket/${name}/${spo}/
     mkdir paket/${name}/DEBIAN

     echo "File copyed."
     echo ""
}

###########################################################################################################    Show help-page     ####################

showHelp () {
# show help-page
     echo ""
     echo "-mkdeb-helppage-"
     echo "You can make your own .deb-file out of one script with this tool."
     echo "Usage: mkdeb [package|--help] [bin|games|local/bin|...]"
     echo "To use this tool, you have to go to the directory, where the script is."
     echo ""
     echo "Usage: mkdeb [prepare|control|pack] <name of the package>"
     echo "       mkdeb script <name of the script>"
     echo ""
     echo "  "prepare"     "Make a directory, where you can copy manual your scripts to the right place.
     echo "  "control"     "Read information of the package and write it out to paket/"<package>"/DEBIAN/control.
     echo "  "pack"        "Pack directory to .deb-file
     echo ""
     echo "  "script"      "Make a debian-Package out of one script
     echo "              "Single scripts will be installed to /usr/bin on default. This could be changed with third parameter:
     echo "                "mkdeb script "<name of script> <new location for installation>"
     echo ""
}

##############################################################################################################      PREPARE       ####################

doPrepare () {
     echo ""
     echo "Erstelle paket/${name}/usr/bin"
     mkdir -p paket/${name}/usr/bin
     echo "Erstelle paket/${name}/DEBIAN"
     mkdir paket/${name}/DEBIAN
     echo "Place all in (this) folder. (Like it shall be installed later. '${name}' is the root-folder)"
     echo "You can add new folders, too. But you should not delete DEBIAN-folder!!!"
     echo ""
}

############################################################################################################     START     ###########


if [ "$1" == "prepare" ]
then
     if [ "$2" == "" ]
     then
          echo "No package-name given."
     else
          name="$2"
          doPrepare
     fi

elif [ "$1" == "control" ]
then
     if [ "$2" == "" ]
     then
          echo "No package-name given."
     else
          name="$2"
          if [ -d "paket/${name}/DEBIAN" ]
          then
               getInfos
               writeOutInfos
          else
              echo "Directory 'paket/${name}/DEBIAN' does not exists!"
          fi
     fi
     
elif [ "$1" == "pack" ]
then
     if [ "$2" == "" ]
     then
          echo "No package-name given."
     else
          name=$2
          makeDeb
     fi
     
elif [ "$1" == "script" ]
then
     if [ "$2" == "" ]
     then
          echo "No script-name given."
     else
          if [ -a $2 ]
          then
               scipt=$2
               if [ "$3" == "" ]
               then
                    spo="/usr/bin"
               else
                    spo="$3"
               fi
               getInfos
               cpScript
               writeOutInfos
               makeDeb
          else
               echo "Did not found file '${2}'."
          fi
     fi

else
     showHelp
fi