#!/bin/sh
# pkg
# Vector Linux Packages utility
# Copyright : Vector Linux
# License   : GNU GPL 
# Creator   : Robert
# Credit    : The Slackware package tools by P. Volkerdi & Co. 
#             (installpkg, removepkg, etc ...)
#             Checkinstall by F. Duran
#
# 20040919 : Kocil
# - add feature: understand environment PKGFORMAT={tgz|tbz}
#                affect -m !
# - add feature: understand environment ROOT=[directory]
#                affect -i, -u, -o
#
# Changes
# 20040918 : Kocil
# Got to add dependency for the new package system, so:
# - add feature: -i add dependency to log/packages record
# - add feature: -u upgrade (upgradepkg --install-new)
# - add feature: -o overwrite (upgradepkg --reinstall --install-new)
# - add feature: -m makepkg, with dependency
# - add feature: -x explodepkg
# - add feature: -p checkinstall
#
# 20040801 : Kocil
# - add feature: -i can install tgz and tbz package
# - add feature: -f find a file
# - add feature: -l can list a specific package
# - fix expr syntax error
# - Tidy up the code 
#

if [ ! "$UID" = "0" ]; then
    echo
    echo "[$LOGNAME] You need to run this script as root."
    echo "Try one of these to run it as root: "
    echo "  sudo pkg -i package.tgz"
    echo "  su -c pkg -i package.tgz"
    echo
    exit 
fi

usage() {
cat<<EOF
pkg : Vector Linux package utility
Install/Remove:
    pkg -i <package>     :Install tgz or tbz2 package
    pkg -u <package>     :Upgrade or install newer package
    pkg -o <package>     :Overwrite or install package
    pkg -r <package>     :Remove package 
Query:
    pkg -l [package]     :List packages in /var/log/packages
    pkg -q <package>     :Find the exact package
    pkg -f[s] <file>     :Find file in any package [summary]
Make/convert:
    pkg -c <package.*>   :Convert rpm, deb, or slp package to tgz
    pkg -b <package.*>   :Convert rpm, deb, or slp package to tbz
    pkg -x <package>     :Explode a package to current directory
    pkg -m [package]     :Make a package from current directory
    pkg -n               :Monitor 'make install' to create log record
    pkg -p               :Create a package of 'make install'

options -n or -p, is very recomended for source code installation, e.g:
       ./configure ; make ; pkg -n
Tips, Midnight commander provides the menu for these (press F2)'
EOF

exit
}

## Settings
PKGFORMAT=${PKGFORMAT:-tgz}

## Safely make tmp directory
TMP=/var/tmp/pkg
if [ ! -d $TMP ]; then
  rm -rf $TMP 
  mkdir -p $TMP
  chmod 700 $TMP 
fi

## Set clean exit
trap "rm -rf $TMP" EXIT

##-----------------------------------------------------------
## Query tools

##-----------------------------------------------------------
## List package at /var/log/packages
list_pkg()
{
    if [ ! -d /var/log/packages ]; then
	echo "Strange, no /var/log/packages directory !" 1>&2
	echo "Are you using the right distro ?" 1>&2
	exit 1
    fi
    
    if [ "$1" ]; then
	ls -A -C /var/log/packages/* | grep $1 | while read LINE; do
	    basename $LINE
	done
	exit
    fi

    if [ ! `ls -a /var/log/packages | wc -l` -eq 2 ]; then
	if [ `ls -A /var/log/packages/* | wc -l` -gt 100 ]; then
	    if type -all less >/dev/null 2>&1 ; then
		pager="less"
	    elif type -all more >/dev/null 2>&1 ; then
		pager=more
	    else
		echo "Can't find pager (less or more)."
		ls -A -C /var/log/packages
		exit
	    fi
	    echo "Going to list packages page by page, press q to Quit!"
	    echo -n "Press enter to continue. "
	    read PAUSE
	    ls -A -C /var/log/packages | $pager
	    exit
	else
	    ls -A -C /var/log/packages 
	    exit
	fi
    fi
}

##----------------------------------------------------
## Find the exact pkgname
query_pkg()
{
    for PNAME in $*; do
	for DD in /var/log/packages/$DDNAME-*; do
	    DDBASE=`basename $PP`
	    if [ "$PNAME" = "`pkgname $DDBASE`" ]; then
		echo $DDBASE
		return 0
	    fi
	done
    done
    return 1
}

##---------------------------------------------------
## Find packages that contains a file
## Grep is our friend
find_pkg2()
{
    grep -l -e "/$1" /var/log/packages/* 
}

find_pkg()
{
    grep -l -e "$1" /var/log/packages/* | while read LINE; do
	echo "`basename $LINE` :"
	grep -h -e "$1" $LINE | while read LINE1; do
	    [ -f "/$LINE1" ] && echo "    $LINE1"
	done
    done
}


##---------------------------------------------------
## Create a packagename from current directory
pdirname()
{
    CWD=`pwd`
    PNAMEVER=`basename $CWD`
    # detect architecture
    MARCH=""
    if [ "$CFLAGS" ]; then
	MARCH=`echo ${CFLAGS##*march=} | cut -f1 -d ' '`
    fi
    if [ -z "$MARCH" ]; then
	MARCH=`uname -m`
    fi
    echo $PNAMEVER-$MARCH-1.$PKGFORMAT
}

##-----------------------------------------------------------------
## Monitor "make install" command and create record of install.
## TODO : Should add dependency record !
monitor_error() {
echo "
While running the command: 
$COMMAND 
I got this ERROR!:

`cat $TMP/error`
"
    unset INSTALLWATCHFILE
    unset LD_PRELOAD
    rm -f $TMP/$package*
    exit
}

monitor_pkg() {

if [ "$1" ]; then
    PLONG=$1
else
    PLONG=`pdirname`
fi
PNAME=`pkgname $package`
if query_pkg $PNAME; then
    echo "Package '$PNAME' has been installed."
    echo "Consider to remove it firt"
    exit
fi

package=${PLONG##*.}
logdir="/var/log/packages"
scriptdir="/var/log/scripts"
mkdir -p $scriptdir

echo
echo "Please enter the command you want to run:"
echo "The default is 'make install' just press enter for it."
echo -n "Command: "
read COMMAND

if [ "$COMMAND" = "" ]; then
    COMMAND="make install"
fi 

echo
echo "Running command '$COMMAND'."
sleep 2

IEXCLUDE="`pwd`,/dev,/proc,/tmp,/var/tmp"

export INSTALLWATCHFILE="$TMP/$package"
cat /dev/null > $INSTALLWATCHFILE
#export LD_PRELOAD="/usr/lib/installwatch.so"
# $COMMAND 2>$TMP/error || monitor_error 

installwatch --logfile=$INSTALLWATCHFILE --exclude="${IEXCLUDE}" \
   --root=${TMP} $COMMAND &> $TMP/error || monitor_error

unset INSTALLWATCHFILE
unset LD_PRELOAD

echo "PACKAGE NAME: $package" > $logdir/$package 
echo "INSTALL COMMAND: $COMMAND" >> $logdir/$package 
echo "DATE: `date`" >> $logdir/$package 
echo "FILE LIST:" >> $logdir/$package 
echo "./" >> $logdir/$package 

rm -f $TMP/$package.a
if [ -x "`type -path gawk`" ]; then
grep -v -E "/dev/|$PWD|/tmp/" $TMP/$package | gawk '{print $3}' | sort -u > $TMP/$package.a
if gawk '{print $2}' $TMP/$package | grep -q "symlink" ; then
grep "symlink" $TMP/$package | gawk '{print $4}' | sort -u > $TMP/$package.s
rm -f $TMP/$package.script 
cat $TMP/$package.s | while read file; do
echo "( cd `dirname $file` ; rm -rf `basename $file` )" >> $TMP/$package.script
echo "( cd `dirname $file` ; ln -sf `ls -l $file | gawk '{print $11}'` `basename $file` )" >> $TMP/$package.script
done 
fi
else
grep -v -E "/dev/|$PWD|/tmp/" $TMP/$package | cut -f3 | sort -u > $TMP/$package.a
if cut -f2 $TMP/$package | grep -q "symlink" ; then
grep "symlink" $TMP/$package | cut -f4 | sort -u > $TMP/$package.s
rm -f $TMP/$package.script 
cat $TMP/$package.s | while read file; do
echo "( cd `dirname $file` ; rm -rf `basename $file` )" >> $TMP/$package.script
echo "( cd `dirname $file` ; ln -sf `ls -l $file | cut -d' ' -f29` `basename $file` )" >> $TMP/$package.script
done 
fi
fi

rm -f $TMP/$package.b 
cat $TMP/$package.a | while read file; do
if [ -e "$file" ]; then
    echo $file >> $TMP/$package.b
fi
done 

cat $TMP/$package.b | sed "s/^\///g" >> $logdir/$package 
    
if [ -s $TMP/$package.script ]; then 
    mv $TMP/$package.script /var/log/scripts/$package
    chmod 755 /var/log/scripts/$package 2>/dev/null
fi

rm -f $TMP/$package*

echo
echo "Package Log: /var/log/packages/$package"
echo "Done..."
echo "You can remove this package later by command"
echo "  removepkg `pkgname $package`"
exit
}
# End of monitor.

##--------------------------------------------------------------------
## Create a package from make install
## Use checkinstall man :)
create_pkg()
{
   checkinstall $*
}

##--------------------------------------------------------------------
## Explode package to a directory
explode_pkg()
{
PKG=$1
echo "Exploding package $PKG in current directory:"
( umask 000 ; $TAR xzvf $PKG 2> /dev/null )
cat<<EOF
Package $1 has been exploded.
After examining it, you may repackaged it by using
   pkg -m [new_package_name]

EOF
}

##----------------------------------------------------------
## Make a package with dependency and txt description
## From the current directory

find_dependency()
{
    DDBASE=`query_pkg $PNAME`
    if [ "$DDBASE" ]; then
	if ! grep -qx "$DDBASE" install/dependency; then
	    echo $DDBASE >> install/dependency
	    echo "$DDBASE"
        fi
    fi
}

make_pkg()
{
PFULL=$1
PDIR=`dirname $1`
PLONG=`basename $1`
PNAME=`pkgname $PLONG`
PSHORT=${PLONG%.*}
PFORMAT=${PLONG##*.}

echo "Making symlinks creation script ..."
# Using readlink to read the link
# Old method using ls -l was broken since coreutil 5.0
find . -type l -print | while read LINE; do
    echo "$LINE"
    DIRNAME="`dirname $LINE`" 
    LINKNAME="`basename $LINE`"
    LINKTO="`readlink $LINE`"
    echo "( cd $DIRNAME ; rm -rf $LINKNAME; ln -sf $LINKTO $LINKNAME )" >> install/doinst.sh
    rm -f $LINE
done

echo "Checking shared libraries ..."
LDPATH=""
find . -name '*.so' -print | while read LINE; do
    DIRNAME="`dirname $LINE`"
    DIRNAME=`echo $DIRNAME | sed -e 's!^.!!'`
    if ! echo $LDPATH | grep -w $DIRNAME; then
	echo "if ! grep -qx $DIRNAME ./etc/ld.so.conf; then echo $DIRNAME >> ./etc/ld.so.conf; fi" >> install/doinst.sh
	LDPATH="$LDPATH $DIRNAME"
    fi
done
if [ "$LDPATH" ]; then
    echo "ldconfig" >> install/doinst.sh
fi

echo "Creating dependency ..."
if [ ! -f "install/dependency" ]; then
    echo "# dependency list" > install/dependency
fi

# find the executables and track the required packages
find . -perm +1 -print | while read LINE; do
    # Skip directories & links
    [ ! -f $LINE ] && continue
    # Check if this is a dynamic executable
    if ldd $LINE &>/dev/null; then
	ldd $LINE | while read LINE1; do
    	    LIB=`echo $LINE1 | cut -f1 -d ' '`
	    # continue if LIB is in itself
	    if find . -name $LIB >/dev/null; then
		continue
	    fi
	    # Find other packages that contais this LIB
	    grep $LIB /var/log/packages/* | while read LINE2; do
		DNAME=`echo $LINE2 | cut -f1 -d:`
		DBASE=`basename $DNAME`
		# Skip slackware cheating library
		[ "`pkgname $DBASE`" = "aaa_elflibs" ] && continue
		if ! grep -qx "$DBASE" install/dependency; then
	    	    echo $DBASE >> install/dependency
	    	    echo "$DBASE"
        	fi
	    done
	    if [ $? != 0 ]; then
		echoc "ERROR : Missing dependency" red
		echoc "ERROR : Cannot find the package of $LIB" red
	    fi
	done
     else
        # not a dynamic, possibly script
	FTYPE=`file $LINE`
	case $FTYPE in
	    *Bourne*) find_dependency bash
	    ;;
	#    *C shell*) find_dependency tcsh
	#    ;;
	    *perl*) find_dependency perl
	    ;;
	    *python*) find_dependency python
	    ;;
	    *ruby*) find_dependency ruby
	    ;;
	    *expect*) find_dependency expect tclx tcl
	    ;;
	    *tcl*) find_dependency tcl
	    ;;
	    *tix*) find_dependency tix
	    ;;
	esac
	# Unkown, ignored
     fi
done

# Strip the ELFs
( find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded &> /dev/null )

# Ok, pack the package
rm -f $PDIR/$PSHORT.tar 
tar cf $PDIR/$PSHORT.tar .  

# Warn of zero-length files:
find . -type f -size 0c | while read file ; do
     echo "WARNING: zero length file $file"
done
find . -type f -name '*.gz' -size 20c | while read file ; do
     echo "WARNING: possible empty gzipped file $file"
done
  
case $PFORMAT in
    tbz)
      bzip2 $PDIR/$PSHORT.tar
      mv $PDIR/$PSHORT.tar.bz2 $PDIR/$PSHORT.tbz
      ;;
    tbz2)
      bzip2 $PDIR/$PSHORT.tar
      mv $PDIR/$PSHORT.tar.bz2 $PDIR/$PSHORT.tbz2
      ;;
    *)
      gzip -9 $PDIR/$PSHORT.tar
      mv $PDIR/$PSHORT.tar.gz $PDIR/$PSHORT.tgz
      ;;
esac    

}

# Create Text description from current directory
function make_txt() {
  TXTNAME=$1

  ## Add description
  rm -f $TXTNAME
  if [ -f "install/slack-desc" ]; then
    grep -ve "^#" install/slack-desc | while read line; do 
       echo "$line" >> $TXTNAME
    done
  else
    echo "WARNING: slack-desc file is not exist"
    echo "$PNAME: $PNAME" >> $TXTNAME
  fi

  ## Add dependency
  if [ -f install/dependency ]; then
    echo "-------------------------------------" >> $TXTNAME
    grep -ve "^#" install/dependency | while read line; do 
       echo "DEPENDENCY: $line" >> $TXTNAME
    done
  else
    echo "WARNING: dependency file is not exist"
  fi
}

make_pkg_txt()
{
    if [ "$1" ]; then
	PFULL=$1
    else
	PFULL=`pdirname`
    fi
    PDIRSHORT=${PFULL%*.}
    make_pkg $PDIRSHORT.$PKGFORMAT
    make_txt $PDIRSHORT.txt
}

##--------------------------------------------------------------------
## Remove packages
## This is still using slack old tool.
## TODO: check dependency !
remove_pkg() {
    removepkg $1
}

##------------------------------------------------------------------
## install package 
## Difference than installpkg (slackware)
##  - add DEPENDENCY info
##  - do not delete directory symlink
install_pkg()
{
  ## Well, installpkg has been hacked, so use it instead
  installpkg $1
  return $?

  ## This is Vector Linux version, not used 
  ## Leave it here in case we need it someday
  package=$1
  ADM_DIR=$ROOT/var/log
  TMP=$ROOT/var/tmp

  mkdir -p $ADM_DIR/packages
  mkdir -p $ADM_DIR/scripts
  mkdir -p $TMP

  longname=`basename $package`
  packagetype=${longname##*.}
  shortname=${longname%.*}
  packagebase=`pkgname $longname`

  case $packagetype in
    tbz|tbz2)
	FILTER="j"
	;;
    *)
	FILTER="z"
	;;
  esac

  # Test tarball integrity, 
  mkdir -p $TMP
  rm -f $TMP/tmplist
  tar -t${FILTER}f $package 1> $TMP/tmplist 2> /dev/null
  TARERROR=$?
  if [ ! "$TARERROR" = "0" ]; then
    echo "Package $package is corrupted" >> $flog
    rm -f $TMP/tmplist
    return 1
  fi

  # make sure we're not installing files on top of existing symbolic links:
  # except it is a directory, add by Kocil
  cat $TMP/tmplist | grep -v "/$" | while read file ; do
    if [ -L "$ROOT/$file" ] && [ ! -d "$ROOT/$file" ]; then
      rm -f "$ROOT/$file"
    fi
  done
  rm -f $TMP/tmplist

  # Extract
  ( cd $ROOT/ ; tar -x${FILTER}lUpvf - ) < $package >> $TMP/$shortname 2> /dev/null

  # Write the package file database entry and install the package
  echo "PACKAGE NAME: $shortname" > $ADM_DIR/packages/$shortname
  echo "COMPRESSED PACKAGE SIZE: $COMPRESSED" >> $ADM_DIR/packages/$shortname
  echo "UNCOMPRESSED PACKAGE SIZE: $UNCOMPRESSED" >> $ADM_DIR/packages/$shortname
  echo "PACKAGE LOCATION: $package" >> $ADM_DIR/packages/$shortname
  echo "PACKAGE DESCRIPTION:" >> $ADM_DIR/packages/$shortname
  
  if [ -r $ROOT/install/slack-desc ]; then
    cat $ROOT/install/slack-desc | grep "^$packagebase:" >> $ADM_DIR/packages/$shortname 2> /dev/null
    if [ "$shortname" != "$packagebase" ]; then
      cat $ROOT/install/slack-desc | grep "^$shortname:" >> $ADM_DIR/packages/$shortname 2> /dev/null
    fi
  fi

  # add dependency, by Kocil
  if [ -r $ROOT/install/dependency ]; then
    grep -ve '^#' $ROOT/install/dependency | while read LINE; do
      echo "DEPENDENCY: $LINE" >> $ADM_DIR/packages/$shortname 2> /dev/null
    done
  fi
  
  echo "FILE LIST:" >> $ADM_DIR/packages/$shortname
  if [ "`cat $TMP/$shortname | grep '^./' | wc -l | tr -d ' '`" = "1" ]; then
    # Good.  We have a package that meets the Slackware spec.
    cat $TMP/$shortname >> $ADM_DIR/packages/$shortname
  else
    # Some dumb bunny built a package with something other than makepkg.  Bad!
    # Oh well.  Bound to happen.  Par for the course.  Fix it and move on...
    echo './' >> $ADM_DIR/packages/$shortname
    cat $TMP/$shortname | grep -v '^./$' | cut -b3- >> $ADM_DIR/packages/$shortname
  fi
  rm -f $TMP/$shortname

  if [ -f $ROOT/install/doinst.sh ]; then
    echo "Executing install script for $shortname..."
    ( cd $ROOT/ ; sh install/doinst.sh -install; )
    cp $ROOT/install/doinst.sh $ADM_DIR/scripts/$shortname
    chmod 755 $ADM_DIR/scripts/$shortname
  fi 

  # Clean up the mess...
  rm -rf $ROOT/install
  return 0
}

##------------------------------------------------------------------
## upgrade pkg no matter what, even if newer
## Use slackware tool, since installpkg has been hacked
overwrite_pkg() {
   upgradepkg --install-new --reinstall $1
}

##------------------------------------------------------------------
## upgrade pkg if newer 
upgrade_pkg() {
  PNAME=`pkgname $1`
  POLD=`query_pkg $PNAME`
  if [ "$POLD" ]; then
    if pkgnewer $PNAME $POLD > /dev/null; then
	echo Installed $POLD is newer than $PNAME
	echo Skip upgrading
	return 0
    fi
    overwrite_pkg $1
  else
    install_pkg $1
  fi
}

##------------------------------------------------------------------
## convert_pkg
## I don't think this is going to work.
## where is the unrpm, undep, unslp ??
convert_pkg() {
    PFULL=$1
    PLONG=`basename $PFULL`
    FROM_TYPE=${PLONG##*.}
    SHORTNAME=${PLONG%.*}
    
    if [ "$PKGFORMAT" = "tbz" ] && [ "$FROM_TYPE" = "tgz" ]; then
	# piece of cake
	cp $PFULL $SHORTNAME.tar.gz
	gunzip $SHORTNAME.tar.gz
	bzip2 $SHORTNAME.tar
	mv $SHORTNAME.tar.bz2 $SHORTNAME.tbz
	exit 
    fi
    
    if [ "$MKPKG_TYPE" = "tgz" ] && [ "$FROM_TYPE" = "tbz" ]; then
	# piece of cake too
	cp $PFULL $SHORTNAME.tar.bz2
	bunzip2 $SHORTNAME.tar.bz2
	gzip -9 $SHORTNAME.tar
	mv $SHORTNAME.tar.gz $SHORTNAME.tgz
	exit 
    fi

    if echo $PFULL | grep -v -E "\.src.rpm$" | grep -E -q "\.rpm$" ; then
    if [ ! -x "`type -path unrpm`" ]; then
    echo
    echo "Can't find 'unrpm' to check rpm package!"
    exit
    fi
    echo
    echo "Checking rpm package for errors '$PFULL'..."
    unrpm < $PFULL | gzip -d | cpio -i --only-verify-crc 2>/tmp/rpm.error 
    if [ $? -gt 0 ]; then
    echo
    echo "ERROR! reported for '$PFULL'..." 
    echo "There's something wrong with the package, you probably shouldn't convert it." 
    echo
    rm -f /tmp/rpm.error
    exit 
    elif `grep -q "checksum error" /tmp/rpm.error` ; then
    echo
    echo "ERROR! reported for '$PFULL'..." 
    echo "There's something wrong with the package, you probably shouldn't convert it." 
    echo
    rm -f /tmp/rpm.error
    exit 
    fi
    rm -f /tmp/rpm.error
    D="`basename $PFULL .rpm`"
    echo
    echo "Converting '$PFULL' to '`basename $PFULL .rpm`.tgz'..."
    echo
    dir=$PWD
    mkdir -p /tmp/$D 
    cd /tmp/$D || exit 
    unrpm < $dir/$PFULL | gzip -d | cpio -iumd --quiet 
    find . -type d -perm 700 -exec chmod 755 {} \; 2>/dev/null
    mkpkg $D 
    if [ $? -gt 0 ]; then
    cd $dir && rm -rf /tmp/$D
    exit 
    else
    mv $D.tgz $dir && cd $dir && rm -rf /tmp/$D
    exit
    fi
    elif echo $PFULL | grep -E -q "\.deb$" ; then
    if [ ! -x "`type -path undeb`" ]; then
    echo
    echo "Can't find 'undeb' to check deb package!"
    exit
    fi
    echo
    echo "Checking deb package for errors '$PFULL'..."
    undeb -p $PFULL data.tar.gz | tar -tz >/dev/null 2>&1
    if [ $? -gt 0 ]; then
    echo
    echo "ERROR! reported for '$PFULL'..." 
    echo "There's something wrong with the package, you probably shouldn't convert it." 
    echo
    exit 
    fi
    D="`basename $PFULL .deb`"
    echo
    echo "Converting '$PFULL' to '`basename $PFULL .deb`.tgz'..."
    echo
    dir=$PWD
    mkdir -p /tmp/$D && undeb -p $PFULL data.tar.gz | tar -xzpf - -C /tmp/$D && cd /tmp/$D && mkpkg $D 
    if [ $? -gt 0 ]; then
    cd $dir && rm -rf /tmp/$D
    exit 
    else
    mv $D.tgz $dir && cd $dir && rm -rf /tmp/$D
    exit
    fi
    elif echo $PFULL | grep -E -q "\.slp$" ; then
    D="`basename $PFULL .slp`"
    echo
    echo "Converting '$PFULL' to '`basename $PFULL .slp`.tgz' ..."
    echo
    dir=$PWD
    mkdir -p /tmp/$D && tar -xpf $PFULL -C /tmp/$D --use-compress-program bzip2 2>/dev/null
    cd /tmp/$D && mkpkg $D 
    if [ $? -gt 0 ]; then
    cd $dir && rm -rf /tmp/$D
    exit 
    else
    mv $D.tgz $dir && cd $dir && rm -rf /tmp/$D
    exit
    fi
    else
    echo
    echo "$PFULL" 
    echo "Doesn't have a .tgz .rpm, .deb, or .slp extension."
    echo
    exit
    fi
}

case $1 in
    --sourced)
        ;;
    -l) list_pkg $2
	;;
    -q) if [ "$2" ]; then
	    shift
	    query_pkg $*
	else
	    usage
	fi
	;;
    -f) 
	if [ "$2" ]; then
	    find_pkg $2
	else
	    usage
	fi
	;;
    -fs) 
	if [ "$2" ]; then
	    find_pkg2 $2
	else
	    usage
	fi
	;;
    -c) 
	if [ "$2" ]; then
	    PKGFORMAT="tgz"
	    convert_pkg $2
	else
	    usage
	fi
	;;
    -b)
	if [ "$2" ]; then
	    PKGFORMAT="tbz"
	    convert_pkg $2
	else
	    usage
	fi
	;;
    -x)
	if [ "$2" ]; then
	    explode_pkg $2
	else
	    usage
	fi
	;;
    -m) make_pkg_txt $2
	;;
    -n) monitor_pkg $2
	;;
    -p) create_pkg $2 $3 $4
	;;
    -r) 
	if [ "$2" ]; then
	    remove_pkg $2
	else
	    usage
	fi;;
    -i)
	if [ "$2" ]; then
	    install_pkg $2
	else
	    usage
	fi
	;;
    -o)
	if [ "$2" ]; then
	    overwrite_pkg $2
	else
	    usage
	fi
	;;
    -u)
	if [ "$2" ]; then
	    upgrade_pkg $2
	else
	    usage
	fi
	;;
    *)
	usage
	;;
esac

