#!/bin/bash
#
# ##############################################################################
#
# Name: DPacman
#
# Version: 2010.06
#
# Description: Simple interface, using shell sript and dialog, for pacman.
# Archlinux Package Manager. This program was based on the code
# "PacGUI" created by "jorchube. Thanks! "Jorchube.
# 
# Author: smarch < silveriomm@bol.com.br >
#
# Date: 09/06/2010
#
# ##############################################################################
#
# Version: 2011.04
#
# Date 07/04/2011
# 
# Clean up and store the code.
# Modified the function to install and remove programs, now have an option
# only. They find and install a program name or by keyword.
# Only programs simply choose from a list.
# Also changes were made in search functions.
###############################################################################
# Version: 2014.01
#
# add read p command for pause after execute command pacman
# gettext localize from PuppyRus linux team
###############################################################################
#250107 sfs
#f="--force" ##sfs
[ ! -f /var/lib/pacman/sync/core.db ] && sudo pacman -Sy
export TEXTDOMAIN=dpacman
. gettext.sh

# ========== CREATE AND DEFINE THE LOCATION OF THE PROGRAM DIRECTORY ===========
	
	mkdir -p $HOME/.dpacman
	
# ==============================================================================

################################################################################
################################################################################

btitle="DPACMAN"
DIALOG=dialog

################ ABOUT #########################################################
################################################################################

function SOBRE {
	
MSG_SOBRE="$(gettext 'DPacman is a graphical interface based on dialog, for pacman,
Archlinux package manager.
It is very intuitive and easy to use. It is a totally free program!
You can use, modify and distribute as you wish.
But the author is not liable for any damage
that may cause the program to your system.\nSo use at your own risk!\n
I hope you find it useful!\n\nsilveriomm@bol.com.br\n2011')"
	
	$DIALOG					\
		--backtitle $btitle            	\
		--title "$(gettext 'About DPacman...')"	\
		--msgbox "$MSG_SOBRE"		\
		0 0
		MENUP
	
}

##################### INSTALL PROGRAM ##########################################
################################################################################

function INSTALAR {
	
MSG_INST="$(gettext 'Enter a keyword, or program name')"	
	
	BUSCA=$(
		$DIALOG					\
			--stdout			\
			--backtitle $btitle		\
			--title "$(gettext 'INSTALL PROGRAM')"	\
			--inputbox "$MSG_INST"		\
			08 70) || MENUP
	
	clear

	RESP_BUSCA=$(sudo pacman -Ss $BUSCA > $HOME/.dpacman/TMP.txt)
	
	cat $HOME/.dpacman/TMP.txt | cut -d " " -f1 | tr -s "/" " " |	\
	sed s/$/" off"/ | awk '{ print $2 " " $1}' > $HOME/.dpacman/TMP2.txt
	
	ORD_LISTA=$(cat $HOME/.dpacman/TMP2.txt)
	
MSG_LISTA="$(gettext '\nResults found for: $BUSCA\n\n
Select the programs you want to install')"
	
	$DIALOG 						\
		--stdout					\
		--backtitle $btitle				\
		--title "$(gettext 'INSTALL PROGRAM')"			\
		--separate-output				\
		--checklist "$MSG_LISTA"			\
		 0 75 0 $ORD_LISTA > $HOME/.dpacman/TMP3.txt
	
	if [ $? = 1 ]
		then
			MENUP
		else 
			PEGA=$(cat $HOME/.dpacman/TMP3.txt)
			clear && sudo pacman -S $f $PEGA
			MENUP
	fi
	
}

############# INSTALL LOCAL PROGRAM ############################################
################################################################################

function INSTALA_LOCAL {
	
MSG_LOCAL="$(gettext 'SELECT THE PROGRAM YOU WISH TO INSTALL')"
	
	LOCAL=$(
		$DIALOG				\
			--stdout		\
			--backtitle $btitle	\
			--title "$MSG_LOCAL"	\
			--fselect /home/$USER	\
			08 70)
	
	if [ $? = 1 ]
		then
			MENUP
		else
			clear && sudo pacman -U $LOCAL
			read -s -r -p "
$(gettext 'Press any key to continue...')" -n 1
			MENUP
	fi

}

################## REMOVE PROGRAM ##############################################
################################################################################

function REMOVER {	
	
MSG_REM="$(gettext 'Enter a keyword, or program name')"

	
	BUSCA=$(	
		$DIALOG                        		\
			--stdout                 	\
			--backtitle $btitle      	\
			--title "$(gettext 'REMOVE PROGRAM')"	\
			--inputbox "$MSG_REM" 		\
			 08 70) || MENUP
	
	clear
	
	RESP_BUSCA=$(sudo pacman -Ss $BUSCA > $HOME/.dpacman/TMP.txt)
	
	cat $HOME/.dpacman/TMP.txt | cut -d " " -f1 | tr -s "/" " " |	   \
	sed s/$/" off"/ | awk '{ print $2 " " $1}' > $HOME/.dpacman/TMP2.txt
	
	ORD_LISTA=$(cat $HOME/.dpacman/TMP2.txt)
	
MSG_LISTA_REM="$(gettext '\nResults found for: $BUSCA\n\n
Select the programs you want to remove')"
	
	$DIALOG							\
		--stdout					\
		--backtitle $btitle				\
		--title "$(gettext 'REMOVE PROGRAM')"			\
		--separate-output				\
		--checklist "$MSG_LISTA_REM"			\
		0 75 0  $ORD_LISTA > $HOME/.dpacman/TMP3.txt
	
	if [ $? = 1 ]
		then
			MENUP
		else 
			PEGA=$(cat $HOME/.dpacman/TMP3.txt)
			clear && sudo pacman -R $PEGA
			read -s -r -p "
$(gettext 'Press any key to continue...')" -n 1
			MENUP
	fi
	
}

################### PROGRAM SEARCH #############################################
################################################################################

function LOCALIZA {
	
MSG_LOCALIZA="$(gettext 'Enter a keyword, or program name')"
	
	BUSCA=$(
		$DIALOG					\
			--stdout			\
			--backtitle $btitle		\
			--title "$(gettext 'PROGRAM SEARCH')"	\
			--inputbox "$MSG_LOCALIZA"	\
			08 72)

	if [ $? = 1 ]
		then
			MENUP
		else
			clear

			sudo pacman -Ss $BUSCA > $HOME/.dpacman/busca.txt
			
MSG_BUSCA_MOSTRA="Results found for: $BUSCA"
		
	$DIALOG						\
		--backtitle $btitle			\
		--title "$MSG_BUSCA_MOSTRA"		\
		--textbox $HOME/.dpacman/busca.txt	\
		0 0
		MENUP
	fi
}

############## UPDATE SYSTEM ###################################################
################################################################################

function MENU_ATUALIZA {
	
	ATUALIZA=$(
		
		$DIALOG							\
			--stdout					\
			--cancel-label "$(gettext 'Return')"				\
			--backtitle $btitle				\
			--title "$(gettext 'UPDATE SYSTEM')"				\
			--menu "$(gettext 'What to do?')"				\
			0 0 0						\
				1 "$(gettext 'Update System')"			\
				2 "$(gettext 'Update only the database')"		\
				3 "$(gettext 'Update database forced')") ||
				MENUP
	
	case $ATUALIZA in
	
		1) 	clear
			sudo pacman -Syu $f
			read -s -r -p "
$(gettext 'Press any key to continue...')" -n 1
			MENUP ;;
	 
		2) 	clear	
			sudo pacman -Sy
			read -s -r -p "
$(gettext 'Press any key to continue...')" -n 1
			MENUP ;;
	 
		3)	clear	
			sudo pacman -Syy
			read -s -r -p "
$(gettext 'Press any key to continue...')" -n 1
			MENUP ;;
	esac
}

################### OTHER ACTIVITIES ###########################################
################################################################################

################# CLEAR CACHE ##################################################

function LIMPA_CACHE {
	
	CACHE=$(
		$DIALOG                    				\
			--stdout                               		\
			--cancel-label "$(gettext 'Return')"                		\
			--backtitle $btitle                    		\
			--title "$(gettext 'CLEAR CACHE')"                 		\
			--menu "$(gettext 'What to do?')"				\
			0 0 0     					\
				1 "$(gettext 'Clear old packages from cache')"	\
				2 "$(gettext 'Delete all cache package')") ||
				MENUO

	case $CACHE in
	
		1)	clear
			sudo pacman -Sc
			MENUO ;;
	
		2) 	clear	
			sudo pacman -Scc
			MENUO ;;

	esac
	
}

################# DETAILS OF PROGRAMS INSTALLED ################################

MSG_INFO="$(gettext 'Enter the program name to learn more details about the')"

function BUSCA_INFO {
	
	INFO=$(
		$DIALOG							\
			--stdout					\
			--backtitle $btitle				\
			--title "$(gettext 'DETAILS OF PROGRAMS INSTALLED')"		\
			--inputbox "$MSG_INFO"				\
			08 70)
	
	if [ $? = 1 ]
		then
			MENUO
		else
			RESP_INFO=$(pacman -Qi $INFO)
			echo "$RESP_INFO" > $HOME/.dpacman/INFO.TEMP
			$DIALOG						     \
				--backtitle $btitle			     \
				--title "$(eval_gettext "Program Information:  $INFO ")"       \
				--textbox $HOME/.dpacman/INFO.TEMP	     \
				0 0
				MENUO
	
	fi
}

##################### LIST OF PACKAGES LOST ####################################

function LISTA {		
	
	clear
	sudo pacman -Qdt > $HOME/.dpacman/LIST.TEMP
	$DIALOG						\
		--title "$(gettext 'LIST OF PACKAGES LOST')"		\
		--textbox $HOME/.dpacman/LIST.TEMP	\
		0 0
	MENUO
}

################# EDIT /etc/rc.conf  ###########################################

function RCCONF {
	
MSG_RCCONF="$(gettext '\nAre you sure? Want to edit the file /etc/rc.conf now?')"
	
	$DIALOG							\
		--backtitle $btitle				\
		--title "$(gettext 'DANGER, THIS CAN DAMAGE THE SYSTEM')"	\
		--yesno  "$MSG_RCCONF"				\
		07 70
	
	if [ $? == 1 ] 
		then
			MENUO
		else
			clear
			cp /etc/rc.conf $HOME/.dpacman/rc.conf.backup
			sudo mcedit /etc/rc.conf
			MENUO
			
	fi
}

################ EDIT /etc/pacman.conf  ########################################

MSG_PACCONF="$(gettext '\nAre you sure? Want to edit the file /etc/pacman.conf now?')"

function PACCONF {
	
	$DIALOG							\
		--backtitle $btitle				\
		--title "$(gettext 'DANGER, THIS CAN DAMAGE THE SYSTEM')"	\
		--yesno "$MSG_PACCONF"				\
		07 70

	if [ $? = 1 ] 
		then
			MENUO
		else
			clear
			cp /etc/pacman.conf $HOME/.dpacman/pacman.conf.backup &&
			sudo mcedit /etc/pacman.conf
			MENUO
			
	fi
}

####################### EDIT /etc/pacman.d/mirrorlist  #########################

MSG_MIRROR="$(gettext '\nAre you sure?
Want to edit the file /etc/pacman.d/mirrorlist now?')"

function MIRROR {
	
	$DIALOG							\
		--backtitle $btitle				\
		--title "$(gettext 'DANGER, THIS CAN DAMAGE THE SYSTEM')"	\
		--yesno "$MSG_MIRROR"				\
		07 70

	if [ $? = 1 ] 
		then
			MENUO
		else
			clear
			cp /etc/pacman.d/mirrorlist		\
			$HOME/.dpacman/mirrorlist.backup &&
			sudo mcedit /etc/pacman.d/mirrorlist
			MENUO
			
	fi

}

###################### Restore /etc/rc.conf ####################################

MSG_REST_RCONF="$(gettext '\nAre you sure? Want to restore the file /etc/rc.conf now?')"

function REST_RCONF {
		
	$DIALOG							\
		--backtitle $btitle				\
		--title "$(gettext 'DANGER, THIS CAN DAMAGE THE SYSTEM')"	\
		--yesno "$MSG_REST_RCONF"			\
		07 70

	if [ $? = 1 ]
		then
			MENU_RESTAURA
		else
			clear
			sudo cp $HOME/.dpacman/rc.conf.backup /etc/rc.conf
			MENU_RESTAURA 
	fi
	
}

################### Restore /etc/pacman.conf ###################################

MSG_REST_PACMANCONF="$(gettext '\nAre you sure?
Want to restore the file /etc/pacman.conf now?')"

function REST_PACMANCONF {
	
	$DIALOG							\
		--backtitle $btitle				\
		--title "$(gettext 'DANGER, THIS CAN DAMAGE THE SYSTEM')"	\
		--yesno "$MSG_REST_PACMANCONF"			\
		07 70
	
	if [ $? = 1 ]
		then
			MENU_RESTAURA
		else
			clear
			sudo cp $HOME/.dpacman/pacman.conf.backup	\
			/etc/pacman.conf
			MENU_RESTAURA
	fi
	
}

############### Restore /etc/pacman.d/mirrorlist ###############################

function REST_MIRROR {
	
MSG_REST_MIRROR="$(gettext '\nAre you sure?
Want to restore the file /etc/pacman.d/mirrorlist now?')"	
	
	$DIALOG							\
		--backtitle $btitle				\
		--title "$(gettext 'DANGER, THIS CAN DAMAGE THE SYSTEM')"	\
		--yesno "$MSG_REST_MIRROR"			\
		07 70

	if [ $? = 1 ]
		then
			MENU_RESTAURA
		else
			clear
			sudo cp $HOME/.dpacman/mirrorlist.backup 	\
			/etc/pacman.d/mirrorlist
			MENU_RESTAURA
	fi
	
}

################## Restore files ###############################################

function MENU_RESTAURA {
	
	RESTAURA=$(
		$DIALOG							\
			--stdout					\
			--cancel-label "$(gettext 'Return')"				\
			--backtitle $btitle				\
			--title "$(gettext 'RESTORE FILES')"				\
			--menu "$(gettext '\nWhat to do?')"				\
			0 0 0  						\
				1 "Restore /etc/rc.conf"		\
				2 "Restore /etc/pacman.conf"		\
				3 "Restore /etc/pacman.d/mirrorlist") ||
				MENUO

	case $RESTAURA in

		1)	REST_RCONF ;;
		
		2)	REST_PACMANCONF ;;
			
		3)	REST_MIRROR ;;

	esac
	
}

################ Unlock database ###############################################

MSG_DESTRAVA="$(gettext '\nAre you sure? Want to unlock the database now?')"

function DESTRAVA {
	
	$DIALOG						\
		--backtitle $btitle			\
		--title "$(gettext 'UNLOCK DATABASE')"	\
		--yesno "$MSG_DESTRAVA"			\
		07 60
	
	if [ $? = 1 ]
		then
			MENUO	
		else
			clear
			sudo rm /var/lib/pacman/db.lck
			$DIALOG						     \
				--backtitle $btitle			     \
				--msgbox "$(gettext 'The database has been unlocked')"    \
				08 30
			MENUO
	fi

}

############## OTHER ACTIVITIES MENU ###########################################
################################################################################

function MENUO {
	
	MENU_OUTROS=$(
		$DIALOG                  				\
			--stdout                               		\
			--cancel-label "$(gettext 'Return')"                		\
			--backtitle $btitle                    		\
			--title "$(gettext 'OTHER ACTIVITIES')"            		\
			--menu "$(gettext '\nWhat to do?')"				\
			0 0 0     					\
				1 "$(gettext 'Clear Cache')"				\
				2 "$(gettext 'Details of programs installed')"	\
				3 "$(gettext 'List of packages lost')"          	\
				4 "$(gettext 'Edit /etc/pacman.conf')"            	\
				5 "$(gettext 'Edit /etc/pacman.d/mirrorlist')"	\
				6 "$(gettext 'Restore files')"			\
				7 "$(gettext 'Unlock database')") || MENUP
#				4 "$(gettext 'Edit /etc/rc.conf')"                	\
	
	case $MENU_OUTROS in
	
		1)	LIMPA_CACHE ;;
		
		2)	BUSCA_INFO ;;
		
		3)	LISTA ;;
		
#		4)	RCCONF ;;
		
		4)	PACCONF ;;

		5)	MIRROR ;;
	
		6)	MENU_RESTAURA ;;
		
		7) 	DESTRAVA ;;

	esac

}

##################### MAIN MENU ################################################
################################################################################

function MENUP {
	
	MENU=$(
		$DIALOG                       			\
			--stdout                             	\
			--backtitle $btitle                  	\
			--title "$(gettext 'MAIN MENU')"             	\
			--no-cancel                          	\
			--menu "$(gettext '\n       What to do?')"		\
			0 0 0					\
				1 "$(gettext 'Install Program')"		\
				2 "$(gettext 'Install local program')"	\
				3 "$(gettext 'Remove Program')"		\
				4 "$(gettext 'Find Program')"		\
				5 "$(gettext 'System Upgrade')"		\
				6 "$(gettext 'Other activities')"		\
				7 "$(gettext 'About DPacman')"	\
				8 "$(gettext 'Exit')")
	
	case $MENU in
	
		1)	INSTALAR ;;
		
		2)	INSTALA_LOCAL ;;
		
		3) 	REMOVER ;;
		
		4) 	LOCALIZA ;;
		
		5) 	MENU_ATUALIZA ;;
		
		6) 	MENUO ;;
		
		7) 	SOBRE ;;
		
		8) 	clear && exit

	esac

}

MENUP




