#!/usr/bin/bash
#
#########################################################################
# rpmxdgtool:
# Copyright (C) 2007 2008 by Juergen Heinemann http://www.hjcms.de
# tuncel:
# Copyright (C) 2013 Atilla ÖNTAŞ https://github.com/tarakbumba/tuncel
#
# 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.
#########################################################################

source /usr/bin/gettext.sh
export TEXTDOMAIN=tuncel
export TEXTDOMAINDIR='/usr/share/locale'

# Test and source configuration:
RPM_XDG_CONFIG_TOOL_FILE=""

for i in $HOME/.config/tuncel.conf /etc/tuncel.conf ; do
  if test -f "$i" ; then
    RPM_XDG_CONFIG_TOOL_FILE="$i"
    break;
  fi
done

test -f "$RPM_XDG_CONFIG_TOOL_FILE" || {
  gettext "Error: Missing tuncel configuration file"
  exit 1
}

. $RPM_XDG_CONFIG_TOOL_FILE

#########################################################################################
# Test binaries and display error message if not found:
#########################################################################################
critical()
{
  gettext "Executable file can not be found: " $1
  exit 1
}

if 
    test -x "$KDIALOG_BIN"
then
    DIALOG_BIN="$KDIALOG_BIN"
elif
    test -x "$ZENITY_BIN"
then
    DIALOG_BIN="$ZENITY_BIN"
else
    critical "kdialog|zenity"
fi

test -x "$MD5SUM_BIN" || critical "md5sum"
test -x "$RPM2CPIO_BIN" || critical "rpm2cpio"

if [ ! -d "$TMP_DIR" ] ; then
  TMP_DIR=/tmp
fi

#########################################################################################
# DIALOG BIN commands:
#########################################################################################
#Kdialog:
if [[ $DIALOG_BIN = $KDIALOG_BIN ]]; then
    DIALOG_ERROR="$KDIALOG_BIN --error"
    DIALOG_POPUP="$KDIALOG_BIN --passivepopup"
    DIALOG_INFOBOX="$KDIALOG_BIN --msgbox"
    DIALOG_TEXTBOX="$KDIALOG_BIN --textbox"
    DIALOG_GET_DIRECTORY="$KDIALOG_BIN --getexistingdirectory"
    SIZE="450 450"    
#Zenity:
elif [[ $DIALOG_BIN = $ZENITY_BIN ]]; then
    DIALOG_ERROR="$ZENITY_BIN --error --text"
    DIALOG_POPUP="$ZENITY_BIN --progress --percentage=0 --text="
    DIALOG_INFOBOX="$ZENITY_BIN --info --text"
    DIALOG_TEXTBOX="$ZENITY_BIN --text-info --html --filename"
    DIALOG_GET_DIRECTORY="zenity --file-selection --directory --filename"
    SIZE="--width 450 --height 450"
fi
    
#########################################################################################
#Show options:
#########################################################################################
case "$1" in
  *--help|-h) 
    gettext "tuncel commands:
maininfo :  Display Package Main Info
showcontents : Show Package Contents in Info Screen
unpack : Unpack package in current Directory
unpackto : Unpack Package to Directory
openHomepage : If %{url} exists open Package Homepage in Web Browser
testinstall : Installation Test (-iv)
forceinstalltest : Force Installation Test (-Uhv)
installsrc : Install RPM Source Package in to Build Enviroment
Example: tuncel maininfo /tmp/foo-0.0.5.i586.rpm
";
    exit 0
  ;;
esac

#########################################################################################
# Make rpm querytags translatable:
#########################################################################################
NAME=$(gettext "Name")
SUMMARY=$(gettext "Summary")
VERSION=$(gettext "Version")
DESCRIPTION=$(gettext "Description")
HOMEPAGE=$(gettext "Homepage")
LICENSE=$(gettext "License")

#########################################################################################
#make file tests
#########################################################################################
if [ ! -f "$2" ] ; then
  $DIALOG_ERROR "$(gettext "no file") $2"
  exit 1
fi

## @modified 2009/02/08
## @short Make compatible with file >= 0.5.0
test -n "$(file -iL "$2" | grep -e '\bapplication\/x\-rpm\b')" || {
  $DIALOG_ERROR "$(gettext "This file is not an application/x-rpm file!")"
  exit 1
}

TMPF=$TMP_DIR/$($MD5SUM_BIN $2 | awk '{ print $1 }')

#########################################################################################
# rpm unpacking function:
#########################################################################################
unpackrpmfile()
{
  cd $1
    $RPM_BIN -qlp $2 > $(basename $2).log | $DIALOG_POPUP "$(gettext "Please wait...")" 5
        $RPM2CPIO_BIN $2 > ${TMPF}
    $CPIO_BIN -idv < ${TMPF}
    rm -f ${TMPF}
  cd -
}

#########################################################################################
# rpm options:
#########################################################################################
case "$1" in
  maininfo)
        $RPM_BIN -qp --queryformat "<strong>$NAME:</strong> %{Name}\n<br/><strong>$SUMMARY:</strong> %{Summary}\n<br/><strong>$HOMEPAGE:</strong> %{URL}\n<br/><strong>$LICENSE:</strong> %{License}\n<br/><strong>$VERSION:</strong> %{Version}-%{Release}\n<br/>
<strong>$DESCRIPTION:</strong> %{Description}" $2 > ${TMPF} && $DIALOG_TEXTBOX ${TMPF} $SIZE && rm -fr ${TMPF}
  ;;
  showcontents)
    $RPM_BIN -qlp $2 > ${TMPF} && $DIALOG_TEXTBOX ${TMPF} $SIZE && rm -fr ${TMPF}
  ;;
  unpack)
    unpackrpmfile $(dirname $2) $2
  ;;
  unpackto)
    TO_DIR="$($DIALOG_GET_DIRECTORY $(dirname $2))"
    if [ -d "${TO_DIR}" ] ; then
      unpackrpmfile ${TO_DIR} $2
      xdg-open ${TO_DIR}
    fi
  ;;
  openHomepage)
    URI="$($RPM_BIN -qp --queryformat '%{url}' $2)"
    if [ -n "$(echo $URI | grep 'p://')" ] || [ -n "$(echo $URI | grep 'ps://')" ]; then
      xdg-open "$URI"
    else
      $DIALOG_INFOBOX "$(gettext "There was no valid URL in package!")"
    fi
  ;;
  testinstall)
    $RPM_BIN --test -Uhv $2 2> ${TMPF}
    if  [ $? -eq 0 ] ; then
      $DIALOG_INFOBOX "$($RPM_BIN --test -Uhv $2) $(gettext "No errors have been reported. Test was successful!")"
    else
      $DIALOG_INFOBOX "$(cat ${TMPF})"
    fi
  ;;
  forceinstalltest)
    $RPM_BIN --test --force -Uhv $2 2> ${TMPF}
    if  [ $? -eq 1 ] ; then
      $DIALOG_INFOBOX "$(cat ${TMPF})"
    else
      $DIALOG_INFOBOX "$($RPM_BIN --test -Uhv $2) $(gettext "No errors have been reported. Test was successful!")"
    fi
  ;;
  installsrc)
    if [ -n "$(echo $2 | grep -e '\.src\.rpm$')" ] ; then
      $DIALOG_POPUP "$(gettext "Source RPM package is being installed...")" 3
      $RPM_BIN -iv $2 > /dev/null
    else
      $DIALOG_ERROR "$(gettext "This package isn't a valid Source RPM package !")"; 
    fi
  ;;
  terminal)
    if test -d "$1" && test -n "$TERMINAL_APPLICATION" || critical "$TERMINAL_APPLICATION"; then
      TERMINAL_CMD="$TERMINAL_APPLICATION $TERMINAL_OPTS"        
      $TERMINAL_CMD $1
    fi
  ;;
  *)
    exit 1
  ;;
esac
