#!/bin/sh
#
#########################################################################
# Copyright (C) 2007 2008 by Juergen Heinemann http://www.hjcms.de
#
# 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.
#########################################################################

. gettext.sh

set +x

RPM_XDG_CONFIG_TOOL_FILE=""

for i in ~/.rpmxdgtoolrc /etc/rpmxdgtool.conf ; do
  if test -f "$i" ; then
    RPM_XDG_CONFIG_TOOL_FILE="$i"
    break;
  fi
done

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

. $RPM_XDG_CONFIG_TOOL_FILE

critical()
{
  gettext -d rpmxdgtool -ns "can not find executable" $1
  exit 1
}

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

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

case "$1" in
  *-help|-h) 
    gettext -d rpmxdgtool -s "rpmxdgtool commands:"
    echo -n " maininfo : "; gettext -d rpmxdgtool -s "Display Package Main Info";
    echo -n " showcontents : "; gettext -d rpmxdgtool -s "Show Package Contents in KDialog";
    echo -n " unpack : "; gettext -d rpmxdgtool -s "Unpack package in current Directory";
    echo -n " unpackto : "; gettext -d rpmxdgtool -s "Unpack Package to Directory";
    echo -n " openHomepage : "; gettext -d rpmxdgtool -s "If %{url} exists open Package Homepage in Konqueror";
    echo -n " testinstall : "; gettext -d rpmxdgtool -s "Installation Test (-iv)";
    echo -n " forceinstalltest : "; gettext -d rpmxdgtool -s "Force Installation Test (-Uhv)";
    echo -n " installsrc : "; gettext -d rpmxdgtool -s "Install RPM Source Package in to Build Enviroment";
    gettext -d rpmxdgtool -s "Example: rpmxdgtool maininfo /tmp/rpmxdgtool-0.0.5.i586.rpm";
    exit 0
  ;;
esac

if [ ! -f "$2" ] ; then
  $KDIALOG_BIN --error "$(gettext -d rpmxdgtool -s "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')" || {
  $KDIALOG_BIN --error "$(gettext -d rpmxdgtool -s "no application/x-rpm file")"
  exit 1
}

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

unpackrpmfile()
{
  cd $1
    $RPM_BIN -qlp $2 > `basename $2`.log
    $KDIALOG_BIN --passivepopup "$(gettext -d rpmxdgtool -s "please wait...")" 4
    $RPM2CPIO_BIN $2 > ${TMPF}
    $CPIO_BIN -idv < ${TMPF}
    rm -f ${TMPF}
  cd -
}

case "$1" in
  maininfo)
    $KDIALOG_BIN --msgbox "`$RPM_BIN -qp --queryformat 'Name: %{Name}\nSummary: %{Summary}\nHomepage: %{URL}\nLicense: %{License}\nVersion: %{Version}-%{Release}\n' $2`"
  ;;
  showcontents)
    $RPM_BIN -qlp $2 > ${TMPF} && $KDIALOG_BIN --textbox ${TMPF} 450 450
  ;;
  unpack)
    unpackrpmfile `dirname $2` $2
  ;;
  unpackto)
    TO_DIR=`$KDIALOG_BIN --getexistingdirectory $(dirname $2)`
    if [ -d "${TO_DIR}" ] ; then
      unpackrpmfile ${TO_DIR} $2
      kfmclient newTab ${TO_DIR}
    fi
  ;;
  openHomepage)
    URI=`$RPM_BIN -qp --queryformat '%{url}' $2`
    if [ -n "`echo $URI | grep 'p://'`" ] ; then
      kfmclient openURL $URI
    else
      $KDIALOG_BIN --msgbox "$(gettext -d rpmxdgtool -s "No URL in RPM Package found")"
    fi
  ;;
  testinstall)
    $RPM_BIN --test -Uhv $2 2> ${TMPF}
    if  [ $? -eq 0 ] ; then
      $KDIALOG_BIN --msgbox "($RPM_BIN --test -Uhv $2) $(gettext -d rpmxdgtool -s "No Errors")"
    else
      $KDIALOG_BIN --msgbox "`cat ${TMPF}`"
    fi
  ;;
  forceinstalltest)
    $RPM_BIN --test --force -Uhv $2 2> ${TMPF}
    if  [ $? -eq 1 ] ; then
      $KDIALOG_BIN --msgbox "`cat ${TMPF}`"
    else
      $KDIALOG_BIN --msgbox "($RPM_BIN --test -Uhv $2) $(gettext -d rpmxdgtool -s "No Errors")"
    fi
  ;;
  installsrc)
    if [ -n "`echo $2 | grep -e '\.src\.rpm$'`" ] ; then
      $KDIALOG_BIN --passivepopup "$(gettext -d rpmxdgtool -s "Install Source...")" 3
      $RPM_BIN -iv $2 > /dev/null
    else
      $KDIALOG_BIN --error $(gettext -d rpmxdgtool -s "No Valid RPM Source Package"); 
    fi
  ;;
  *)
    exit 1
  ;;
esac
