File pin of Package pin

#! /bin/sh

#set -x 
export LC_ALL=C
# pin - Package InformatioN - mlasars@suse.de (c) 2001-2005 SUSE LINUX GmbH
#
# 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
# 2004 09 02      changed cd/dvd mounting, thanks to David Haller
# 2004 09 13      wrong mountpoint, its dvdram instead of dvd-ram
# 2004 09 17      forget one dvd-ram fix
# 2005 03 04	  fixed cp ARCHIVES.gz on update (#63010)
# 2005 05 16      several fixes by Christian Boltz (to fix the fix for #63010 ;-)
#                 - fixed copying ARCHIVES.gz to work with 9.3 mount points (subfs/HAL)
#                 - fixed target filenames when copying ARCHIVES.gz to ARCHIVES_$VERSION.gz
#                 - fixed filenames when symlinking ARCHIVES_$VERSION.gz to ARCHIVES.gz 
#                 - fixed several typos ($ARK_BASE vs. $ARC_BASE, $ARC_BASE vs. $ARC_DIR)
#
# 2005 07 16 0.31 fixed the update issue 
#                 thx again to Christian Boltz
#
# 2005 08 22 0.32 fixed bug #104800
#
# 2005 08 26 0.33 removed test
#
# 2005 09 06 0.34 updated text (bug #115333)
# 2006 01 23 0.35 fixed error in cd/dvd handling (bug #144395)
# 2006 08 01 0.36 fixed wrong su command (bug #188068)
# 2006 08 05      throw error message if cd/dvd is not mounted (instead of trying to copy "/ARCHIVES.gz")
# 2006 12 22      detect broken ARCHIVES.gz symlink (bug #227589)
# 2007 04 03 0.37 --nosignature --nodigest for rpm -qa to speedup (thanks Pascal)
#                 su & tcsh fix (bug #188068), thanks Volker
# 2007 05 06 0.38 fixed fix for bug #188068, copying ARCHIVES.gz should now work with any shell
#                 clean up tempfile if pin gets killed or Ctrl-c'ed (bug #218263)
# 2008-12-14 0.39 added stty sane to cleanup trap. (bug #458980, jw)


VER=0.38
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/"

USED_VERSION="`grep VERSION /etc/SuSE-release | cut -f3 -d " "`"

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"

# cleanup tempfile if pin gets killed
trap "rm -f \"$OUT_FILE\"; stty sane; exit 1" 1 2 3 7 13 15

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"

copy_archives () {
if [ ! -r "$ARC_DIR""ARCHIVES_""$USED_VERSION"".gz" ] ; then
  echo -e "\n no file "ARCHIVES_""$USED_VERSION"".gz" found "
  echo -e "\n please insert CD1/DVD in your CD-ROM/DVD drive / bitte CD1/DVD einlegen\n
 please eject all medias from other drives / bitte entfernen Sie andere Medien aus den Laufwerken.\n
 \n press enter to continue - Bitte enter druecken"

  read
  
  echo -e "\n please wait / bitte warten \n"

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

  # take care that we still use SuSE as tag!
  USED_DEVICE="`hwinfo --cdrom | grep -i SuSE -B 10 | grep Device\ File: | cut -f2 -d : | cut -f2 -d " "`"

  if [ "$USED_DEVICE" = "" ] ; then
    echo -e " no SUSE CD/DVD found, please copy ARCHIVES.gz manually to 
 /var/lib/pin/ARCHIVES_"$USED_VERSION".gz and execute 
 \"ln -s /var/lib/pin/ARCHIVES_"$USED_VERSION".gz /var/lib/pin/ARCHIVES.gz\" \n
 keine SUSE CD/DVD gefunden, bitte ARCHIVES.gz manuell nach 
 /var/lib/pin/ARCHIVES_"$USED_VERSION".gz kopieren und
 \"ln -s /var/lib/pin/ARCHIVES_"$USED_VERSION".gz /var/lib/pin/ARCHIVES.gz\" aufrufen \n"
    exit 
  fi

  USED_DIR="`mount | grep "$USED_DEVICE" | cut -f3 -d " "`"
  [ -z "$USED_DIR" ] && { echo -e "\n  ERROR: CD/DVD not mounted" >&2; exit 1; }

CDROM="$USED_DIR"


           echo -e "\n copy ARCHIVES.gz to $ARC_DIR...\n"
           if [ -f "$CDROM/ARCHIVES.gz" ] ; then 
              cp "$CDROM""/ARCHIVES.gz" "$ARC_DIR""ARCHIVES_""$USED_VERSION"".gz"
              if [ -r "$GREP_FILE" ] ; then 
                mv "$GREP_FILE" "$ARC_DIR""ARCHIVES_old_backup.gz"
              fi
	      # make link 
	      ln -s "ARCHIVES_""$USED_VERSION"".gz" "$GREP_FILE" 2>/dev/null
              if [ "$?" = 1 ] ; then
                echo -e "\n error, could not make link"
	      	echo " press return to delete old ARCHIVES.gz"
                read
                rm "$GREP_FILE"
               ln -s "ARCHIVES_""$USED_VERSION"".gz" "$GREP_FILE" 2>/dev/null

              fi 
           else
              echo -e "\n file not found, maybe wrong CD?\n please insert the first CD/DVD and restart\n\n"
           fi
fi
}

if [ ! -r "$ARC_DIR""ARCHIVES_""$USED_VERSION"".gz" ] ; then
  # roottest
  if test "$UID" != "0" -a "$USER" != root -a -z "$ROOT" ; then
    echo -e "\n *** pin found no ARCHIVES_"$USED_VERSION".gz file in $ARC_DIR\n
    pin will copy the files,  please enter root password :\n\n"
    su - root -s /bin/bash -- "$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
  # test if there is a symlink
  LINK_ARCHIVES="`readlink $GREP_FILE`"
  if [ "$LINK_ARCHIVES" != "" ] ; then
      # check if it is the latest version
      if [ -r ""$ARC_DIR""ARCHIVES_""$USED_VERSION"".gz ] ; then
        true 
      else
        copy_archives 
      fi
  fi

copy_archives 

# 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
elif [ -r "$ARC_DIR/ARCHIVES_$USED_VERSION.gz" ] ; then
  # ARCHIVES.gz symlink broken, but ARCHIVES_$USED_VERSION.gz is OK
  echo
  echo "  ${ARC_DIR}ARCHIVES.gz symlink is broken!"
  echo
  echo "  Please execute"
  echo "  ln -sf ${ARC_DIR}ARCHIVES_"$USED_VERSION".gz ${ARC_DIR}ARCHIVES.gz"
  echo
  exit 1
else
  echo -e "\n $GREP_FILE not found, copy ARCHIVES.gz from SuSE CD1 (or DVD) to 
 /var/lib/pin/ARCHIVES_"$USED_VERSION".gz and execute 
 \"ln -s /var/lib/pin/ARCHIVES_"$USED_VERSION".gz /var/lib/pin/ARCHIVES.gz\" \n
 $GREP_FILE nicht gefunden, bitte ARCHIVES.gz manuell nach 
 /var/lib/pin/ARCHIVES_"$USED_VERSION".gz kopieren und
 \"ln -s /var/lib/pin/ARCHIVES_"$USED_VERSION".gz /var/lib/pin/ARCHIVES.gz\" aufrufen \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 --nosignature --nodigest | 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
openSUSE Build Service is sponsored by