#! /bin/sh
# pin - Package InformatioN - mlasars@suse.de (c) 2001,2002 SuSE AG
#
# gives u info about a package ( rpm -qi, ql)
# and greps the SuSE Archives.gz for files 
#
# very handy when compiling or installing stuff
# thanks to stefan for better sort
# henne for hosting & advertising :-)

# changes:
#
# 2001 05 27 0.02 first public :-)
# 2001 07 01 0.03 added * for executeable files
# 2001 07 16 0.05 filter .so files as non exe
#
# 2001 07 26 0.06 added support for different
# versions, just copy the ARCHIVES.gz to ARC_DIR/7.2-i386 or 7.2-ppc
# what u like (short names rules). 
# dont forget change ARC_BASE, if you want a different file as 
# ARCHIVES.gz as default
#
# 2001 07 30 0.07 changes the options, now pin <option> name
# 2001 08 12 0.08 unknown option fix
# 2001 08 13 0.09 more fixing
# 2001 08 27 0.10 release
# 2001 08 31 0.11 better sorting (thx stefan)
# 2001 09 03 0.13 cp ARCHIVES.gz from CD if not available
# 2001 09 07 0.14 added man page, help page 
#                 option check now before check for ARCHIVES.gz
# 2001 09 11      ;-( bad bad day, peace!
# 2001 09 17 0.15 cancel and no input fix, remove one cut
# 2002 02 17 0.20 fixed root/user permissions bug
# 2002 02 18 0.23 changed ARC_DIR to /usr/share/pin , $OUT_FILE_$$, hello phalanx
# 2002 02 20 0.24 fix rm tmp file at the end
# 2002 03 07 0.25 fix messages before copy files, now in right order
# 2002 08 23 0.26 fix /dvd/cdrom/cdrecorder mount
# 2003 02 21 0.27 corrected typos (# 23264)
#                 new location for ARCHIVES.gz (# 22409), now /var/lib/pin/
#                 no sorting for main section, makes reading easier
# 2003 09 01 0.28 added clear when using dialog
#		  clean up options
#		  -v uses now the version to search 
#		  thanks to Christian Boltz for suggestions & fixes
# 2004 01 05      fixed insecure tmp file handling

VER=0.29
OUT_FILE=$(/bin/mktemp /tmp/.pin_packinfo_$$.XXXXXX) || { echo "pin: can not create temporary file" >& 2; exit 1; }
ARC_DIR_OLD="/usr/share/pin/"
ARC_DIR="/var/lib/pin/"  # dont forget the / at end!
ARC_BASE="ARCHIVES.gz"
GREP_FILE_OLD=""$ARC_DIR_OLD""$ARC_BASE""
GREP_FILE=""$ARC_DIR""$ARC_BASE""
CDROM="/media/cdrom/" # dont forget the / at end!
CDROM_ALT="/media/dvd/"

umask 022

# u can also use gdialog or kdialog ... i prefer gdialog
DIALOG="dialog"
  
# if you want less instead of dialog output, OUT="less"
OUT="less"

unknown_option () {
  echo -e "\n Pin   searches the installed packages (rpm -qi, -ql) and/or
       the ARCHIVES.gz file for the desired information. It
       shows also README, README.SuSE, FAQ, when available.
       \n unknown option 
      options : pin                    open dialog inputbox
                pin string             searches for string
                pin -v version string  use another version, not the current-i386
                pin -f string          grepping also if package was found \n"
  exit 1
}

# check options
while getopts fv: OPTION ; do
  case $OPTION in
        v) PIN_OPTION="-v" ; PIN_VERSION="$OPTARG" ;;
	f) PIN_FULL="-f" ;;
	*) unknown_option ;;
  esac
done

eval SEARCH="\$$OPTIND"

if [ ! -r "$GREP_FILE" ] ; then
  # roottest
  if test "$UID" != "0" -a "$USER" != root -a -z "$ROOT" ; then
    echo -e "\n *** pin found no ARCHIVES.gz file in $ARC_DIR\n
    pin will copy the files,  please enter rootpassword :\n\n"
    su - -c "$(basename $0) $*"
    exit
  fi  
  # test for old ARC files
  if [ -r "$GREP_FILE_OLD" ] ; then
      echo -e "\n copy the files from $ARC_DIR_OLD to $ARC_DIR .... \n\n"
      if [ ! -r "$ARC_DIR" ] ; then
        mkdir $ARC_DIR
      fi
      ARC_DIR_OLD="$ARC_DIR_OLD""*"
      cp -v $ARC_DIR_OLD $ARC_DIR && echo -e "\n files copied, you can remove $ARC_DIR_OLD\n if you want."
      if [ -r "$GREP_FILE" ] ; then
        cd $ARC_DIR
        mv $ARC_BASE ARCHIVES_old_backup.gz
        echo -e "\n moved old $ARC_BASE to ARCHIVES_old_backup.gz \n rename it if you still need search this version"
      fi
  fi
fi

if [ ! -e "$GREP_FILE" ] ; then
 echo -e "\n no file $GREP_FILE found \n\n"
 echo -e "\n please insert CD1/DVD in your first CD-ROM/DVD drive and\n press enter, i will copy it for you!\n"
 read
 echo -e "\n please wait / bitte warten \n"

  if [ ! -e $ARC_DIR ] ; then
    mkdir $ARC_DIR
  fi

# cdrom check
if [ "`cat /etc/fstab | grep "/media/cdrom"`" = "" ] ; then
 if [ "`cat /etc/fstab | grep "/media/cdrecorder"`" = "" ] ; then
   if [ "`cat /etc/fstab | grep "/media/dvd"`" = "" ] ; then
     echo -e "\n no CD/DVD drive found, copy ARCHIVES.gz manually to $ARC_DIR\n"
     exit 1
   else
     CDROM="/media/dvd/"
   fi
 else
   CDROM="/media/cdrecorder/"
 fi
else
 CDROM="/media/cdrom/"
fi

mount $CDROM

 case $? in
       0) echo -e "\n copy ARCHIVES.gz to $ARC_DIR...\n"
           if [ -e "$CDROM""ARCHIVES.gz" ] ; then 
              cp "$CDROM""ARCHIVES.gz" "$GREP_FILE" 
           else
              echo -e "\n file not found, maybe wrong CD?\n please insert the first CD/DVD and restart\n\n"
           fi
          umount $CDROM ;;

       1) 
          echo -e "\n error, please copy it manually:"
          echo -e "\n no $GREP_FILE directory found, \n make or change it in $0\n\n mkdir $ARC_DIR \n\n" 
          echo -e " and then copy the ARCHIVES.gz  from the first CD/DVD to $ARC_DIR \n\n e.g. cp /media/cdrom/ARCHIVES.gz $ARC_DIR (dont forget to mount first)\n\n"
          umount $CDROM > /dev/null
          exit 1 ;;
 esac
fi

# try to find installed archives.gz
ARC_FILES=`ls -1 "$ARC_DIR"`

if [ "$PIN_OPTION" = "-v" ] ; then
  if [ -r ""$ARC_DIR""$PIN_VERSION"" ] ; then
    GREP_FILE=""$ARC_DIR""$PIN_VERSION""
  else
    echo -e " $OPTARG not found! \n"
    # display all versions found in ARC_DIR
    while true ; do
      echo "$ARC_FILES"
      echo -e "\n please choose a version from above, \n just enter the name"
      read GREP_FILE
      if [ "$GREP_FILE" = "" ] ;then
        GREP_FILE=""$ARC_DIR""$ARC_BASE""
        break
      else
        GREP_FILE=""$ARC_DIR""$GREP_FILE""
          if [ -f "$GREP_FILE" ] ; then
            break
          else
            echo $GREP_FILE "no found ! please choose again or press return for $ARC_BASE"
          fi
      fi
  done
  fi
fi


if [ -r $GREP_FILE ] ; then
  true
else
  echo -e "\n $GREP_FILE not found (or not readable)\n please change location in $0 or copy\n 
 ARCHIVES.gz from SuSE CD1 (or DVD) to $GREP_FILE\n"
 exit
fi

stty_size () { 
LINES=$1  COLUMNS=$2 
}

stty_size `stty size 2>/dev/null`

if [ "$SEARCH" = "" ] ; then
 if $DIALOG --title "pin - package infomation" \
            --inputbox "please input package name or file" \
            8 40 2> $OUT_FILE
 then
   SEARCH="`cat $OUT_FILE`"
   DIALOG_USE="yes"
   clear
 fi
fi

if [ "$SEARCH" = "" ] ; then
  rm -f $OUT_FILE 2> /dev/null 
  clear
  echo " you entered nothing or choose cancel ... bye"
  exit
fi

echo -e "pin $VER - package info for $SEARCH" > $OUT_FILE

if [ "$PIN_OPTION" != "-v" ] ; then
# check 
rpm -qi $SEARCH 2> /dev/null 1> /dev/null

if [ $? != 0 ] ; then
  echo -e "\n------------------------------------------------------------------" >> $OUT_FILE
  echo -e "*** no rpm named $SEARCH installed" >> $OUT_FILE
  echo -e "------------------------------------------------------------------\n" >> $OUT_FILE
  RPM_ALL="`rpm -qa | grep $SEARCH`"
   if [ "$RPM_ALL" != "" ] ; then
     echo -e " maybe just a typo? what about this:\n" >> $OUT_FILE
     echo "$RPM_ALL" >> $OUT_FILE
     echo -e "\n maybe better restart with another option? grepping anyway ...\n" >> $OUT_FILE
   fi
 PIN_FULL="-f"
else
  echo -e "\n------------------------------------------------------------------" >> $OUT_FILE
  echo -e "*** rpm info" >> $OUT_FILE
  echo -e "------------------------------------------------------------------\n" >> $OUT_FILE
  rpm -qi $SEARCH >> $OUT_FILE

  echo -e "\n\n------------------------------------------------------------------" >> $OUT_FILE
  echo -e "*** rpm filelist (*=executeable)" >> $OUT_FILE
  echo -e "------------------------------------------------------------------\n" >> $OUT_FILE
  rpm -ql $SEARCH | \

while read line
 do
  if [ -x $line ] && [ -f $line ] &&! [ "`echo $line | grep .so`" = "$line" ]; then
    echo "*" $line >> $OUT_FILE
  else
    echo " " $line >> $OUT_FILE
  fi
 done

  if [ -e /usr/share/doc/packages/$SEARCH ] ; then
    echo -e "\n\n------------------------------------------------------------------" >> $OUT_FILE
    echo -e "*** /usr/share/doc/packages/$SEARCH/" >> $OUT_FILE
    echo -e "------------------------------------------------------------------\n" >> $OUT_FILE
    ls -l /usr/share/doc/packages/$SEARCH/ >> $OUT_FILE
  fi

  if [ -e /usr/share/doc/packages/$SEARCH/README.SuSE ] ; then
    echo -e "\n\n------------------------------------------------------------------" >> $OUT_FILE
    echo -e "*** README.SuSE" >> $OUT_FILE
    echo -e "------------------------------------------------------------------\n" >> $OUT_FILE
    cat /usr/share/doc/packages/$SEARCH/README.SuSE >> $OUT_FILE
  fi

  if [ -e /usr/share/doc/packages/$SEARCH/README ] ; then
    echo -e "\n\n------------------------------------------------------------------" >> $OUT_FILE
    echo -e "*** README" >> $OUT_FILE
    echo -e "------------------------------------------------------------------\n" >> $OUT_FILE
    cat /usr/share/doc/packages/$SEARCH/README >> $OUT_FILE
  fi

  if [ -e /usr/share/doc/packages/$SEARCH/FAQ ] ; then
    echo -e "\n\n------------------------------------------------------------------" >> $OUT_FILE
    echo -e "*** FAQ" >> $OUT_FILE
    echo -e "------------------------------------------------------------------\n" >> $OUT_FILE
    cat /usr/share/doc/packages/$SEARCH/FAQ >> $OUT_FILE
  fi
fi

fi

if [ "$PIN_OPTION" = "-v" ] || [ "$PIN_FULL" = "-f" ] ; then
  echo -e "\n grepping $GREP_FILE ... please wait\n"
  echo -e "\n------------------------------------------------------------------" >> $OUT_FILE
  echo -e "*** zgrep $SEARCH $GREP_FILE" >> $OUT_FILE
  echo -e "------------------------------------------------------------------\n" >> $OUT_FILE
  ZGREP="`zgrep -i $SEARCH $GREP_FILE`"
  echo "$ZGREP" | grep -i "\-->" |sort >> $OUT_FILE
  echo >> $OUT_FILE
  echo "$ZGREP" >> $OUT_FILE
fi

if [ "$OUT" != "less" ] ; then
  $DIALOG --title "pin package information for $SEARCH" --textbox $OUT_FILE $LINES 
$COLUMNS 
else
  less $OUT_FILE
  if [ "$DIALOG_USE" = "yes" ] ; then
    clear
  fi
fi

rm -f $OUT_FILE 2> /dev/null
