#!/bin/bash # This script provides package information on zenwalk linux # Copyright (C) 2007 Matthew Bruenig # # This file is free software; the copyright holder gives unlimited # permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # # This file is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY, to the extent permitted by law; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. #Initialize Variables VERSION="0.0.1" DATE="+%a %b %d %T %Y" NETPKGCONF="/etc/netpkg.conf" INFO=0 LIST=0 OWNS=0 #Test $ROOT to see if it is set, set ROOTDIR accordingly if [ -n "$ROOT" ]; then if [ -n "${ROOT##*/}" ]; then ROOTDIR="$ROOT/" else ROOTDIR="$ROOT" fi else ROOTDIR="/" fi #Use netpkg configuration values if file exists if [ -f "$NETPKGCONF" ]; then LOGDIR="$(awk '/Package_logs/ {print $3}' <"$NETPKGCONF")" DBDIR="$(awk '/Netpkg_dir/ {print $3}' <"$NETPKGCONF")/db" else #assume some defaults LOGDIR="/var/log/packages" DBDIR="/var/netpkg/db" fi DEPFILE="$DBDIR/depfile" MIRROR="$(<$DBDIR/last_mirror)" #Some Functions err_msg() { echo "$1" exit 1 } usage() { echo "Usage: managepkg [options]" echo echo "Options:" echo " -h, --help Show this dialog" echo " -i, --info Display information about package" echo " -l, --list List all the files in package" echo " -o, --owns Display which package owns file" echo " -v, --version Display the version of managepkg" echo exit 0 } version_msg() { echo "managepkg version: $VERSION" exit } #Quick error check before I shift out all of the positional parameters [ "$#" -lt "1" ] && err_msg "Incorrect usage, see managepkg --help." #Parse options and reset variables accordingly while [ "$#" -gt "0" ]; do param="$1" if [ "${param:0:2}" = "--" ]; then case "${param:2}" in info) INFO="1" ; PACKAGE="$2" ; shift ;; list) LIST="1" ; PACKAGE="$2" ; shift ;; owns) OWNS="1" ; FILE="$2" ; shift ;; help) usage ;; version) version_msg ;; *) err_msg "Option \`${param:2}' not recognized, see managepkg --help." ;; esac elif [ "${param:0:1}" = "-" ]; then shortparam="$(sed 's/./& /g' <<<${param:1})" for character in $shortparam; do case $character in i) INFO="1" ; PACKAGE="$2" ; shift ;; l) LIST="1" ; PACKAGE="$2" ; shift ;; o) OWNS="1" ; FILE="$2" ; shift ;; h) usage ;; v) version_msg ;; *) err_msg "Option \`$character' not recognized, see managepkg --help." ;; esac done else err_msg "Error when parsing \`$param', see managepkg --help." fi shift done #Universal error checks [ "$((INFO+LIST+OWNS))" -ne "1" ] && err_msg "Must have only one option selected." [ "${PACKAGE:0:1}" = "-" ] && err_msg "Package must directly follow option." [ "${FILE:0:1}" = "-" ] && err_msg "File must directly follow option." #Info stuff if [ "$INFO" -eq "1" ]; then #Errors [ -z "$PACKAGE" ] && err_msg "No package specified." [ ! -d "$LOGDIR" ] && err_msg "Package log directory \`$LOGDIR' doesn't exist." [ ! -f "$DEPFILE" ] && err_msg "Dependency file \`$DEPFILE' doesn't exist." unset match for package in $LOGDIR/*; do file="${package##*/}" if [ "${file%-*-*-*}" = "$PACKAGE" ]; then match="$package" break fi done #Test to see if there is a match [ -z "$match" ] && err_msg "No matching log file found for \`$PACKAGE'" #Set information variables name="$PACKAGE" version="$(awk -F\- '{print $(NF-2)}' <<<$match)" installsize="$(awk '/UNCOMPRESSED PACKAGE SIZE/ {print $(NF-1) " " $(NF)}' <$match)" arch="$(awk -F\- '{print $(NF-1)}' <<<$match)" installdate="$(ls -l --time-style="$DATE" "$match" | awk '{print $6 " " $7 " " $8" " $9 " " $10}')" description="$(grep -m 1 "^$PACKAGE: " $match | awk '{$1=""}1')" #Find depends and requiredby if [ -z "$(grep "^$PACKAGE:" $DEPFILE)" ]; then depends="Cannot determine with currently selected mirror" requiredby=" Cannot determine with currently selected mirror" else depends="$(grep "^$PACKAGE:" $DEPFILE | awk -F\: '{print $2}')" unset requiredby for package in $LOGDIR/*; do file="${package##*/}" if [ -n "$(grep "^${file%-*-*-*}:.*$PACKAGE" $DEPFILE)" ]; then requiredby="$requiredby ${file%-*-*-*}" fi done fi #Echo out information echo "Name : $name" echo "Version : $version" #Needed to Indent Dependencies and Required By LEFTOFFSET="17" TTYWIDTH="$(stty size | awk '{print $2}')" MAXLINECHARS="$((TTYWIDTH-LEFTOFFSET))" #Required By echo -n "Required By : " charsonthisline=0 if [ -n "$requiredby" ]; then for req in $requiredby; do reqlength="${#req}" charsonthisline="$((charsonthisline+reqlength+1))" if [ "$charsonthisline" -le "$MAXLINECHARS" ]; then echo -n "$req " else echo echo -n " $req " charsonthisline="$((reqlength+1))" fi done fi echo #Dependencies echo -n "Depends On : " charsonthisline=0 if [ -n "$depends" ]; then for dep in $depends; do deplength="${#dep}" charsonthisline="$((charsonthisline+deplength+1))" if [ "$charsonthisline" -le "$MAXLINECHARS" ]; then echo -n "$dep " else echo echo -n " $dep " charsonthisline="$((deplength+1))" fi done fi echo #Continue echo "Installed Size : $installsize" echo "Architecture : $arch" echo "Install Date : $installdate" echo "Description :$description" fi #List stuff if [ "$LIST" -eq "1" ]; then #Errors [ -z "$PACKAGE" ] && err_msg "No package specified." [ ! -d "$LOGDIR" ] && err_msg "Package log directory \`$LOGDIR' doesn't exist." unset match for package in $LOGDIR/*; do file="${package##*/}" if [ "${file%-*-*-*}" = "$PACKAGE" ]; then match="$package" break fi done #Test to see if there is a match [ -z "$match" ] && err_msg "No matching log file found for \`$PACKAGE'" #Actual output stuff for line in $(sed -n '/FILE LIST:/,$p' $match | grep "^[a-hj-z]"); do echo "$PACKAGE: ${ROOTDIR}${line}" done fi #Owns stuff if [ "$OWNS" -eq "1" ]; then #Errors [ -z "$FILE" ] && err_msg "No file specified." [ ! -e "$FILE" ] && err_msg "File \`$FILE' doesn't exist" [ -d "$FILE" ] && err_msg "File \`$FILE' is a directory." [ ! -d "$LOGDIR" ] && err_msg "Package log directory \`$LOGDIR' doesn't exist." [ "${FILE:0:1}" != "/" ] && err_msg "File must be absolute path starting from /" #Strip FILE of ROOT prefix for grepping NOROOTFILE="${FILE/$ROOTDIR//}" [ "${NOROOTFILE:0:1}" = "/" ] && NOROOTFILE="${NOROOTFILE:1}" match="$(grep -l "^$NOROOTFILE" $LOGDIR/*)" name="${match##*/}" echo "${name%-*-*-*}" fi