#!/bin/bash
# Blinky - Yet another pacman GUI. (but it has aur support :-)
#   Copyright (C) 2010 hunterm <hunterm.haxxr@gmail.com>
#
#     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.,
#     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Check for config file
if [ -f $HOME/.blinkyconf ]; then
	. $HOME/.blinkyconf
else
	echo -e "# Set terminal (Default: xterm)
# Supported terminals: xterm,gnome-terminal,
# konsole,aterm,eterm,mrxvt,mlterm,rxvt
TERMINAL=xterm\n
# Set sudo command (Default: sudo)
# Supported implementations: sudo,kdesudo,gksudo
SUDO=sudo\n
# Enable debug mode
DEBUG=false\n
# Define log file
LOGFILE=$HOME/.blinkylog" > $HOME/.blinkyconf
	. $HOME/.blinkyconf
fi

# Enable debug mode?
[ "$DEBUG" != "[Tt][Rr][Uu][Ee]" ] && LOGFILE="/dev/null"

# Less typing for me. :-)
WHICH='type -fpP'

# Set variables
MY_DIR="/usr/share/blinky" # The directory Blinky is located in.

# Set sudo implementation
case $SUDO in
    sudo|gksudo|kdesudo)
    if ! $WHICH $SUDO &>/dev/null; then
    	echo "Sudo implementation not found, falling back to sudo." >> $LOGFILE
	SUDO='sudo'
    fi
    ;;
    *)
    echo "Invalid sudo implementation, falling back to sudo." >> $LOGFILE
    SUDO='sudo'
    ;;
esac
[ "$SUDO" == 'sudo' ] && SUDO="$TERMINAL -e sudo"

# Check terminal
case $TERMINAL in
    xterm|gnome-terminal|konsole|aterm|eterm|mrxvt|mlterm|rxvt)
    if ! $WHICH $TERMINAL &>/dev/null; then
    	echo "Terminal not found, falling back to xterm." >> $LOGFILE
	TERMINAL='xterm'
    fi
    ;;
    *)
    echo "Invalid terminal, falling back to xterm." >> $LOGFILE
    TERMINAL='xterm'
    ;;
esac

TEXE="$TERMINAL -e" # Set execution flag

# Make sure deps exist and set vars.
if $WHICH zenity &>/dev/null; then
	ZENITY=`which zenity`
else
	echo "`date` - Zenity not found!" >> $LOGFILE
	exit 1
fi
if $WHICH packer &>/dev/null; then
	PACKER=`type -p packer`

else
	echo "`date` - Packer not found!" >> $LOGFILE
	exit 1
fi
if $WHICH pacman &>/dev/null; then
	PACMAN=`type -p pacman`
else
	echo "`date` - Pacman not found!" >> $LOGFILE
	exit 1
fi

PACKER_VER=`pacman -Q packer | sed 's/packer //'`
PACMAN_VER=`pacman -Q pacman | sed 's/pacman //'`
BLINKY_VER=`pacman -Q blinky | sed 's/blinky //'`

cd $MY_DIR # Go to Blinky's installed directory.

# Make sure noone wants to be root.
case "$USER" in
	"root")
	zenity --warning --title="Blinky" --text="<b><big>Trying to run as root I see.</big></b>\n
Blinky, and Packer both do <b>not allow you to run as root, as doing it could cause massive damage to your system if you try to do that.</b> Sounds terrible.\n\nTry running as a regular user." --window-icon='blinky.png'
	exit 1
	;;
esac

check_errs() {
	if [ "${1}" -ne "0" ]; then
	       echo "ERROR ${1} : ${2}"
	       exit ${1}
	fi
}

main_menu() {
MAIN_MENU=`zenity --width=400 --height=400 --title='Blinky' --text="Select an action from the list." --window-icon='blinky.png' --list --column='' --column='Action' --radiolist FALSE "Install Packages" FALSE "Install Packages (No Questions)" FALSE "Search Packages" FALSE "Remove Packages" FALSE "Remove Packages (No Questions)" FALSE "Get Info on Packages" FALSE "Find what Package owns a File" FALSE "Update Package Lists" FALSE "Upgrade Packages" FALSE "Upgrade Packages (No Questions)" FALSE "Upgrade Devel Packages" FALSE "Upgrade Devel Packages (No Questions)" FALSE "List Installed Packages" FALSE "Upgrade Packer (Core)" FALSE "Upgrade Blinky" FALSE "Configuration" FALSE "About..."`

if [ -n "${MAIN_MENU}" ];then
    case "${MAIN_MENU}" in
    "Install Packages")
    PACKAGES=
    PACKAGES=`zenity --entry --width=320 --title='Blinky' --text="Type in the packages you want. (space-seperated)" --window-icon='blinky.png'`
    echo "`date` - Installing the following packages: $PACKAGES" >> $LOGFILE
    $SUDO $TEXE "$PACKER -S $PACKAGES; read -rsN 1 -p 'Press any key to close me.'"
    main_menu
    ;;
    "Install Packages (No Questions)")
    PACKAGES=
    PACKAGES=`zenity --entry --width=320 --title='Blinky' --text="Type in the packages you want. (space-seperated)" --window-icon='blinky.png'`
    echo "`date` - Installing the following packages: $PACKAGES" >> $LOGFILE
    $SUDO $TEXE "$PACKER -S --noconfirm $PACKAGES"
    main_menu
    ;;
    "Search Packages")
    PACKAGES=
    PACKAGES=`zenity --entry --width=320 --title='Blinky' --text="Type in a query." --window-icon='blinky.png'`
    TEMP_FILE=`mktemp`
    ($PACKER -Ss $PACKAGES | cat | sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g") >$TEMP_FILE
    zenity --text-info --filename=$TEMP_FILE --window-icon='blinky.png' --width=470 --height=500 --title='Blinky'
    main_menu
    ;;
    "Remove Packages")
    PACKAGES=
    PACKAGES=`zenity --entry --width=320 --title='Blinky' --text="Type in packages to remove. (space-seperated)" --window-icon='blinky.png'`
    echo "`date` - Removing the following packages: $PACKAGES" >> $LOGFILE
    $SUDO $TEXE "$PACMAN -R $PACKAGES; read -rsN 1 -p 'Press any key to close me.'"
    main_menu
    ;;
    "Remove Packages (No Questions)")
    PACKAGES=
    PACKAGES=`zenity --entry --width=320 --title='Blinky' --text="Type in packages to remove. (space-seperated)" --window-icon='blinky.png'`
    echo "`date` - Removing the following packages: $PACKAGES" >> $LOGFILE
    $SUDO $TEXE "$PACMAN -R --noconfirm $PACKAGES"
    main_menu
    ;;
    "Get Info on Packages")
    PACKAGES=
    PACKAGES=`zenity --entry --width=320 --title='Blinky' --text="Type in a package to query." --window-icon='blinky.png'`
    TEMP_FILE=`mktemp`
    ($PACKER -Si $PACKAGES | sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g") >$TEMP_FILE
    zenity --text-info --filename=$TEMP_FILE --window-icon='blinky.png' --width=470 --height=500 --title='Blinky'
    main_menu
    ;;
    "Update Package Lists")
    echo "`date` - Ran pacman -Sy" >> $LOGFILE
    $SUDO $TEXE "$PACMAN -Sy"
    main_menu
    ;;
    "Upgrade Packages")
    echo "`date` - Ran packer -Syu" >> $LOGFILE
    $SUDO $TEXE "$PACKER -Syu"
    main_menu
    ;;
    "Upgrade Packages (No Questions)")
    echo "`date` - Ran packer -Syu" >> $LOGFILE
    $SUDO $TEXE "$PACKER -Syu --noconfirm; read -rsN 1 -p 'Press any key to close me.'" # This is put on a --noconfirm option on purpose, because as we all know, doing an Syu and then ignoring all the messages can cause disasterous results.
    main_menu
    ;;
    "Upgrade Packer (Core)")
    echo "`date` - Ran Packer (Core) Upgrade" >> $LOGFILE
    TEMP_FILE=`mktemp`
	$SUDO $TEXE "$PACKER -S packer; read -rsN 1 -p 'Press any key to close me.'"
    rm $TEMP_FILE
    zenity --info --window-icon='blinky.png' --title='Blinky' --text="Packer has been updated to version $PACKER_VER"
    main_menu
    ;;
    "Upgrade Blinky")
	echo "`date` - Ran Blinky Upgrade" >> $LOGFILE
	$TEXE "bash -c 'packer -S --noconfirm blinky || (read -s -n 1 -p 'An error occurred with packer. Press [ENTER] to continue.'; exit 1)'"
	exec blinky
	;;
	"Upgrade Devel Packages")
    echo "`date` - Ran packer -Syu --devel" >> $LOGFILE
    $SUDO $TEXE "$PACKER -Syu --devel || (read -s -n 1 -p 'An error occurred with packer. Press [ENTER] to continue.'; exit 1)"
    main_menu
    ;;
    "Upgrade Devel Packages (No Questions)")
    echo "`date` - Ran packer -Syu --devel" >> $LOGFILE
    $SUDO $TEXE "($PACKER -Syu --devel --noconfirm; read -rsN 1 -p 'Press any key to close me.') || (read -s -n 1 -p 'An error occurred with packer. Press [ENTER] to continue.'; exit 1)"
    main_menu
    ;;
    "Find what Package owns a File")
    FILE="`zenity --entry --width=320 --title='Blinky' --text="Type in a file or command to find the package that owns it." --window-icon='blinky.png'`"
    TEMP_FILE=`mktemp`
    (pkgfile --search $FILE) >$TEMP_FILE
    RESULTS=`cat $TEMP_FILE`
    zenity --info --title='Blinky' --text="File/Command '$FILE' is owned by package '$RESULTS'." --window-icon='blinky.png'
    main_menu
    ;;
    "List Installed Packages")
    TEMP_FILE=`mktemp`
    (echo "`$PACMAN -Qq | wc -l` packages total, `$PACMAN -Qm | wc -l` of which are AUR packages"; echo; $PACMAN -Q)>$TEMP_FILE
    zenity --text-info --filename=$TEMP_FILE --window-icon='blinky.png' --width=470 --height=500 --title='Blinky'
    main_menu
    ;;
    "Configuration")
	zenity --info --title="Blinky" --text="The configuration file is located at $HOME/.blinkyconf. Open it up with your favorite editor." --window-icon='blinky.png'
	;;
    "About...")
    zenity --no-wrap --info --title="Blinky" --text="<b><big>Blinky - Yet another pacman GUI, with AUR support!</big></b>\n
\tVersion $BLINKY_VER, created by hunterm, help from Ghost1227\n
\tPacker version $PACKER_VER, created by Matthew Bruenig\n
\tPacman version $PACMAN_VER.
\n<b>Now 25% meat!</b>"
    main_menu
    ;;
    esac
fi
}

if [ -n "$1" ];then
    case "$1" in
    "-h" | "--help" | "-help" | "-v" | "-V" | "--version" | "-version")
    echo "Blinky - Yet another pacman GUI. (but with aur support!)"
	echo "Version $BLINKY_VER, created by hunterm <hunterm.haxxr@gmail.com>, help from Ghost1227."
    echo "Packer version $PACKER_VER, created by Matthew Bruenig <matthewbruenig@gmail.com>"
    echo "Pacman version $PACMAN_VER."
    echo
    exit 0
    ;;
    *)

    ;;
    esac
fi

main_menu
