#!/bin/bash
#=========================HEADER===========================================|
#
# magiic - Simple frontend for Slackpkg.
#
# Copyright (C) 2020 Jefferson 'Slackjeff' Rocha
# Jefferson Rocha (aka Slackjeff) root@slackjeff.com.br
#
# LICENSE
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# CHANGELOG
# Check changelog.txt
#==========================================================================|

#==================== VARS
PRG='magiic'
VERSION='0.3.1'

#==================== FUNCTIONS
MENU() # Main menu
{
  # if exist, remove lock.
  rm /var/lock/slackpkg* &>/dev/null

  the_menu=$(
        dialog                                 \
        --item-help                            \
        --stdout                               \
        --title "Menu"                         \
        --backtitle "$PRG Version ${VERSION}." \
        --cancel-button "Exit"                 \
        --menu  "\nWelcome to $PRG, a slackpkg frontend...\n\nSelect one of the following operations:" \
        10 70 0                                                                                        \
        'Mirror'      'List of mirrors'                                               \
                      'archive in: /etc/slackpkg/mirrors'                             \
        'Check'       'Check for system updates'                                      \
                      'slackpkg check-updates'                                        \
        'Update'      'Update the mirror'                                             \
                      'slackpkg update'                                               \
        'Clean'       'List all unofficial packages for removal'                      \
                      'slackpkg clean-system'                                         \
        'Info'        'Check package information'                                     \
                      'slackpkg info'                                                 \
        'Search'      'Search for a package'                                          \
                      'slackpkg search package'                                       \
        'Install'     'Install a new package'                                         \
                      'slackpkg install package'                                      \
        'Install-new' 'Install packages that were added to Slackware.'                \
                      'slackpkg install-new'                                          \
        'Reinstall'   'Reinstall package'                                             \
                      'slackpkg reinstall package'                                    \
        'Remove'      'Remove a package'                                              \
                      'slackpkg remove package'                                       \
        'Upgrade'     'Upgrade packages'                                              \
                      'slackpkg upgrade-all'
  )
  case $? in
      1) exit 0          ;;
  esac
}

ENTER() # Press enter for some situations
{
    read -p "Press [ENTER] to continue." null
    return 0
}

MIRROR() # view and edit mirrors
{
    local mirrors='/etc/slackpkg/mirrors'
    dialog                                   \
        --title "$mirrors"                   \
        --editbox "/etc/slackpkg/mirrors"    \
        0 0 2> ${mirrors}.bkp
    [[ $? -eq '0' ]] && { cp ${mirrors}.bkp ${mirrors}; rm ${mirrors}.bkp ;}
}

CHECK() #Check for system updates.
{
    dialog                                                 \
        --title "Checking for a new updates"               \
        --infobox "Please wait: checking for updates..."   \
        3 50
    if slackpkg check-updates | grep -q "News on ChangeLog.txt"; then
        dialog                               \
            --title "News!"                  \
            --msgbox "Updates to Changelog.txt..." \
            5 30
    else
        dialog                              \
            --title "No news!"              \
            --msgbox "No updates to ChangeLog.txt..." \
            5 40
    fi
}

UPDATE() # Update mirror
{
    slackpkg update
}

CLEAN() # List all unofficial packages for remove.
{
    slackpkg clean-system
}

INFO()
{
    local tmp_archive=$(mktemp)
    local tmp_archive_head=$(mktemp) # no strangers characters.

    info_package=$(
      dialog                                                       \
        --stdout                                                   \
        --title "Info"                                             \
        --inputbox "What package name do you want to show info?\n" \
        0 60
   )
   [[ "$?" -eq '1' ]] && return 0
   if [[ -z "$info_package" ]]; then
       dialog                                                         \
           --title "Oops!"                                            \
           --msgbox "\nYou must provide a package name to show info." \
           6 50
       return 1
    fi
   # Capture info
   slackpkg info "$info_package" > "$tmp_archive"
   if grep -q "No packages found! Try:" $tmp_archive; then
       dialog                                                                  \
           --title "Oops!"                                                     \
           --msgbox "\nNo packages found!.\nChoose one (and ONLY one package)" \
           8 40
       rm $tmp_archive
       return 1
   fi
   #strangers characters remove.
   head -n -1 $tmp_archive > $tmp_archive_head
   dialog                       \
   --title "Info $info_package" \
   --textbox $tmp_archive_head  \
   0 0
   rm $tmp_archive $tmp_archive_head
}

SEARCH() # Search package
{
   search=$(
      dialog                                                      \
        --stdout                                                  \
        --title "Search"                                          \
        --inputbox "What package name do you want to find?\n"     \
        0 50
   )
   [[ "$?" -eq '1' ]] && return 0
   if [[ -z "$search" ]]; then
       dialog                                                      \
           --title "Oops!"                                         \
           --msgbox "\nYou must provide a package name to search." \
           6 50
       return 1
    fi

   slackpkg search "$search"
   ENTER
}

INSTALL() # Install package
{
   local install_pkg=$(
    dialog                                                \
        --stdout                                          \
        --title  "Install"                                \
        --inputbox "What package do you want to install?" \
        0 50
    )
    if [[ -z "$install_pkg" ]]; then
        dialog                                            \
            --title "Oops!"                               \
            --msgbox "\nYou must provide a package name." \
            6 50
        return 1
    fi

    slackpkg install "$install_pkg"
    ENTER
}

INSTALL_NEW() # Install-new
{
   slackpkg install-new
   ENTER
}

REINSTALL() # Reinstall package(s)
{
    local reinstall_pkg=$(
    dialog                                                  \
        --stdout                                            \
        --title  "Reinstall"                                \
        --inputbox "What package do you want to reinstall?" \
        0 50
    )
    if [[ -z "$reinstall_pkg" ]]; then
        dialog                                            \
            --title "Oops!"                               \
            --msgbox "\nYou must provide a package name." \
            6 50
        return 1
    fi

    slackpkg reinstall "$reinstall_pkg"
    ENTER

}

REMOVE() # Remove package
{
    local remove_pkg=$(
    dialog                                                \
        --stdout                                          \
        --title  "Remove"                                 \
        --inputbox "What package do you want to remove?"  \
        0 50
    )
    if [[ -z "$remove_pkg" ]]; then
        dialog                                            \
            --title "Oops!"                               \
            --msgbox "\nYou must provide a package name." \
            6 50
        return 1
    fi

    slackpkg remove "$remove_pkg"
    ENTER

}

UPGRADE()
{
    slackpkg upgrade-all
    ENTER
}


#=================== MAIN
while true ;do
    MENU
    case $the_menu in
        Mirror)
            MIRROR
        ;;
        Check)
            CHECK
        ;;
        Update)
            UPDATE
        ;;
        Clean)
            CLEAN
        ;;
        Info)
            INFO
        ;;
        Search)
            SEARCH
        ;;
        Install)
            INSTALL
        ;;
        Install-new)
            INSTALL_NEW
        ;;
        Reinstall)
            REINSTALL
        ;;
        Remove)
            REMOVE
        ;;
        Upgrade)
            UPGRADE
        ;;
    esac
done
