#Initialize Variables VERSION="0.0.1" 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 ROOTDIR="$ROOT" 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" PKGLIST="$DBDIR/PACKAGES.txt" 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."