#!/usr/bin/env zsh PDIR="$HOME/packages" PKG_ROOT="ssh://derf.homelinux.org/packages" VCS_CMD="hg" VCS_OPTIONS="--quiet" VCS_ADD="clone" VCS_UPDATE="pull" VCS_UPDATE_OPTIONS="--update" CL_OPTIONS="-q" local info=$'\e[0;36m' local reset=$'\e[0m' echo_status () { echo "${info}$*${reset}" } if [ ! -d $PDIR ]; then echo_status "$PDIR not found!!" exit 100 fi check_deps () { [ -r $PDIR/$1/.deps ] || return 0 DEPS=($(cat $PDIR/$1/.deps)) for dep in $DEPS; { if [ ! -d $PDIR/$dep ]; then echo -n "$1 depends on $dep. Install dependency? [Y/n] " read if [ ! "$REPLY" = 'n' ]; then pkg add "$dep" fi fi } } pkg_add () { if [ -d $PDIR/$1 ]; then echo_status "Package already installed!" return 100 fi check_deps "$1" cd $PDIR || return 255 echo_status 'Retrieving package...' $VCS_CMD $VCS_OPTIONS $VCS_ADD $PKG_ROOT/$1 || return 255 if [ -f $1/hooks/post-add ]; then echo_status 'Executing post-add hook' . $1/hooks/post-add fi echo_status 'Checking symlinks...' cd $1 checklinks $CL_OPTIONS return 0 } pkg_remove () { if [ ! -d $PDIR/$1 ]; then echo_status "Package not installed!" return 100 fi if [ -f $PDIR/$1/hooks/pre-remove ]; then echo_status 'Executing pre-remove hook' . $PDIR/$1/hooks/pre-remove fi rm -r $PDIR/$1 echo_status "Package removed." } pkg_update () { cd $PDIR/$1 echo_status "Updating package $1" check_deps $1 $VCS_CMD $VCS_OPTIONS $VCS_UPDATE $VCS_UPDATE_OPTIONS checklinks $CL_OPTIONS if [ -f hooks/post-update ]; then echo_status 'Executing post-update hook' . hooks/post-update fi cd .. } pkg_update_wrapper () { if [ -n "$1" ]; then pkg_update "$1" else cd $PDIR for i in *(/); { pkg_update "$i" } fi } pkg_changesrc () { cd $PDIR for i in *(/); { if [ -f $i/.hg/hgrc ]; then sed -ri "s!(default = )[^:]*://[^/]*/!\1$1/!" $i/.hg/hgrc fi } } pkg_list_installed () { =ls -1 $PDIR } case "$1" in add) pkg_add "$2" ;; changeroot) pkg_changesrc "$2" ;; list) pkg_list_installed ;; remove) pkg_remove "$2" ;; update) pkg_update_wrapper "$2" ;; esac