#!/bin/bash  


# this little script is just to automate the creation of the .deb package under debian and also the automatic installation of the bashtop program in the system. 
# How does it work? 
# ----
# It parses the bashtop file to retrieve the last version specified in the script to allow a fresh creation of the .deb package 
# +by also retrieving the most recent version of the script then it proceeds to the installation ... 

set  -o errexit 
#set  -x   # just  for debuging  

readonly   file_src_location=../bashtop             #  bashtop location  ^ 
readonly   ubin=usr/bin/
readonly   file_name=${file_src_location##*/}         
readonly   ctrl_file=DEBIAN/control 
readonly   architecture=`dpkg --print-architecture` # for all architectures 
readonly   root_uid=0
declare    version  build_version  

[[  ROOT::PERMISSION ]] 
{
    [[ $UID -ne ${root_uid} ]]  && {
        echo -e  "require root user"
        exit  $UID
    }
}

[[  ARGUMENTS::HANDLER ]] 
{ 
    if [[ -n $1 ]] ; then
        case $1 in  
            "--remove")
                [[  -x  /${ubin}/${file_name} ]] && {
                    dpkg --remove  ${file_name}
                    test $? -eq 0 && exit 0   
                }||{
                    echo -e  "~ nothing  todo:  bashtop is removed " 
                    exit 0        
                } 
                ;;
        esac 
    fi 
}

echo -e "+ building package ..."
sleep 1   
[[ FILECHECK ]]  
{
    #+ require  bashtop file to read inside  
    [[ !  -f  ${file_src_location} ]]  &&   {
        echo  -e  "undefine  ${file_name}"
        exit  3  #  just a basic exit   
    }|| {
        echo  -e "+ populate   DEB folder "
        [[  -d  $ubin  ]] || mkdir -p  $ubin
        `cp  $file_src_location  $ubin`
    }

    #+ require  control file  to write  inside  
    [[ ! -f  ${ctrl_file}  ]]  && {
      echo -e  "undefined ${ctrl_file##*/}"  
      exit  3   
    } 
} 
[[ IO::SEMVERS ]]  
{
    echo -e "+ fetching the lastest  version of  ${file_name}"
    
    get_current_version  () {
        local watch_version=`grep  -i "declare version"  ${file_src_location}`
        local semvers=${watch_version##*=}
        echo  ${semvers:1:-1}  
    }

    set_new_version_ctrl() {
        local catch_package_version=`grep -i  version  ${ctrl_file}` 
        version=${catch_package_version%%:*}
        build_version=${catch_package_version##*:} 
        [[  -n $1  ]] && build_version=$1 
        version+=": ${build_version}"  
     
        echo -e  "+ set  new version control"
        `sed -i "s/$catch_package_version/${version}/g" ${ctrl_file}`
    }

set_new_version_ctrl $(get_current_version)  
} 

[[ PACKAGER_BUILD::DEB ]] 
{
    build_for_debian_base (){ 
        local debian_package_name=${file_name}_${build_version}-${architecture}.deb  
        #echo  ${debian_package_name}
        dpkg-deb --build   ../DEB   ${debian_package_name} 
        test  $?  -eq 0  &&  {
           if [[ -f   ${debian_package_name} ]] ;then
               dpkg -i ${debian_package_name}
               [[ $? -eq 0 ]] &&  {
                 exit $?
               }||{ 
                    echo -e  "Installation failed" 
                    exit $?  
               }
           fi
        }||{
           echo -e "build failed"
           exit 5 
        }
    }

build_for_debian_base
}