10 Jul 2009

simplified deb package management

pkg shell script to the rescue. typing often used commands for software management on a debian based (deb) system can be boring an error prone. Some commands might be used often, such as «apt-get install», «apt-cache search» and «dpkg -l», etc. This script exposes the most used commands as easy to remebr commands (pkg* for package *i = install, *s = search, *u = update, etc.).

Modes:
pkgi [parameters] >name< Install package with >name<
pkgs [parameters] >name< Search for package >name<
pkgu Upgrade all packages
pkgl >name< List installed Packages filtered by >name<
pkgr [parameters] >name< Remove installed package
pkgp [parameters] >name< Purge installed package
pkgc Cleanup
pkg setup Setup symlinks
pkg remove|uninstall Remove symlinks

pkg (bash script):
#!/bin/bash

# shortcuts to frequently used package management tools like apt-get,
# dpkg and apt-cache
#
# NOTE: this script does not work if you havb spaces in your path.
#
# $Id$

# usage
function usage {
cat << EOT
usage: (pkgi|pkgs|pkgr|pkgp|pkgl|pkgu) [parameters] [name]

Modes:
pkgi [parameters] <name> Install package with <name>
pkgs [parameters] <name> Search for package <name>
pkgu Upgrade all packages
pkgl <name> List installed Packages filtered by <name>
pkgr [parameters] <name> Remove installed package
pkgp [parameters] <name> Purge installed package
pkgc Cleanup
pkg setup Setup symlinks
pkg remove|uninstall Remove symlinks
Mappings:
pkgi [parameters] <name> sudo apt-get install [parameters] <name>
pkgs [parameters] <name> apt-cache search [parameters] <name>
pkgu sudo apt-get update && sudo apt-get upgrade
pkgl <name> dpkg -l | grep <name>
pkgr [parameters] <name> sudo apt-get remove [parameters] <name>
pkgr [parameters] <name> sudo apt-get remove --purge [parameters] <name>
pkgc sudo apt-get autoremove
Exit Codes
0: success
1: unknown command (install, remove, autoclean, etc.)
2: parameter missing
3: no write permission (needed by setup)
EOT
}

function fn_help {
fn=$(basename $1)
echo -en "usage: $fn "

case $fn in
"pkgi")
echo "[parameters] <name> - Install package with <name>" ;;
"pkgu" )
echo "- Update repository" ;;
"pkgs" )
echo "[parameters] <name> - Search for package <name>" ;;
"pkgl" )
echo "<name> - List installed Packages filtered by <name>" ;;
"pkgr" )
echo "[parameters] <name> - Remove installed package " ;;
"pkgp" )
echo "[parameters] <name> - Purge installed package " ;;
"pkgc" )
echo "- Cleanup" ;;
* )
echo "Unknown program!"
exit 2
;;
esac
}

function remove {
for l in $@; do
p=$(basename $l)
if [[ "$p" == "pkg" ]]; then
continue
fi
rm $l
done

exit 0;
}

# parameters
filename=$(basename "$0");
command=""

if [[ "$filename" == "pkgi" ]]; then command="install"; fi
if [[ "$filename" == "pkgu" ]]; then command="upgrade"; fi
if [[ "$filename" == "pkgs" ]]; then command="search"; fi
if [[ "$filename" == "pkgl" ]]; then command="list"; fi
if [[ "$filename" == "pkgr" ]]; then command="remove"; fi
if [[ "$filename" == "pkgp" ]]; then command="purge"; fi
if [[ "$filename" == "pkgc" ]]; then command="autoremove"; fi

# setup ?
if [[ "$filename" == "pkg" ]]; then
basedir=$(dirname $0)
escaped=$(echo "$basedir" | sed -e 's/\//\\\//g')
pkgs=$(echo $basedir/pkg* | sed -e 's/'$escaped"\/pkg "'//')

if [[ "$1" == "remove" || "$1" == "uninstall" ]]; then
# if [[ $(basename $0) == "pkg" ]]; then
# exit 0;
# fi
#
echo "Removing ..."
remove $pkgs
fi

if [[ "$1" == "setup" ]]; then

# check if we can write to the directory to reate the symlinks
if [[ ! -w "$basedir" ]]; then
echo "No write permission to create symlinks in $basedir";
exit 2;
fi

echo "Installed links"
ls -la $pkgs;

echo
echo "Purging links"
if [[ $(basename $0) != "pkg" ]]; then
exit 0;
fi
# for l in $pkgs; do
# echo $l
# rm $l
# done

echo
echo "creating links"
for l in pkgc pkgi pkgl pkgr pkgp pkgs pkgu; do
echo $basedir/pkg "->" $basedir/$l
ln -s $basedir/pkg $basedir/$l
done
exit 0;
fi

if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" ]]; then
usage;
exit 0;
fi
fi

# show help text ?
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
fn_help "$0"
exit 0
fi

# show help if parameters are missing
if [[ "$filename" == "pkgi" || "$filename" == "pkgs" ||
"$filename" == "pkgr" || "$filename" == "pkg" ||
"$filename" == "pkgs" || "$filename" == "pkgp" ]]; then
if [[ -z "$1" ]]; then
fn_help "$0"
exit 2
fi
fi

# unknown command
if [[ -z "$command" ]]; then
fn_help "$0"
exit 1
fi

# execute
if [[ "$command" == "list" ]]; then
/usr/bin/dpkg -l | grep -v "^r" | \
sed -e 's/\(..\)[ \t]\+\([^ ]\+\)[ \t]\+\([^ ]\+\)[ \t]\+\(.*\)$/\2 - \4/'|\
grep "$1"
elif [[ "$command" == "search" ]]; then
/usr/bin/apt-cache search $@
elif [[ "$command" == "purge" ]]; then
sudo /usr/bin/apt-get remove --purge $@
elif [[ "$command" == "update" ]]; then
sudo /usr/bin/apt-get update $@ && sudo /usr/bin/apt-get upgrade $@
else
sudo /usr/bin/apt-get $command $@
fi

exit 0