#!/bin/sh DEST_DIR=$HOME PHP_DIR= TMP_DIR=/tmp DELETE=no PEAR=pear PECL=pecl PROGRAM_NAME=`basename $0` VERBOSE=no function check_directory() { if [ -d $1 ]; then return fi echo "$1 is not directory or does't exist" >&2 return 1 } function delete_packages() { if [ x$DELETE != xyes ]; then return fi local library=$1 local line get_local_packages $library|while read line; do local package=`echo $line|awk '{print $1}'` local version=`echo $line|awk '{print $2}'` if ! get_remote_packages $library $package $version > /dev/null; then remove_local_package $library $package $version fi done } function download_package() { (cd `get_dest_dir $1`;`get_library_command $1` download ${2}-${3}) } function download_packages() { local library=$1 local line get_remote_packages $library|while read line; do local package=$(basename `echo $line|awk '{print $1}'`) local version=`echo $line|awk '{print $2}'` if ! get_local_packages $library $package $version > /dev/null; then download_package $library $package $version fi done } function finish() { remove `get_pkgs_file pear` remove `get_pkgs_file pecl` exit $1 } function get_dest_dir() { echo $DEST_DIR/$1 } function get_library_command() { eval echo $`echo $1|tr "[:lower:]" "[:upper:]"` } function get_local_packages() { local dest_dir=`get_dest_dir $1` local package=$2 local version=$3 if [ x$package = x ]; then package='[^-]+' fi if [ x$version = x ]; then local line for line in `ls $dest_dir/|grep -Ei "^${package}-[0-9]+\..+\.[^.]+$"`; do local file=$dest_dir/$line if [ -f $file]; then echo `basename $file`|sed -E 's/^([^-]+)-([0-9]+\..+)\.[^.]+$/\1 \2/' else return 1 fi done else ls $dest_dir/${package}-${version}.* > /dev/null 2>&1 if [ $? = 0 ]; then echo $package $version else return 1 fi fi } function get_pkgs_file() { echo $TMP_DIR/${1}_pkgs.txt } function get_remote_packages() { local library_command=`get_library_command $1` local pkgs_file=`get_pkgs_file $1` local package=$2 local version=$3 if [ ! -f $pkgs_file ]; then $library_command list-all|grep -E '^[^\S]+ +[0-9]+\.'|sort > $pkgs_file fi if [ x$package = x ]; then cat $pkgs_file else if [ x$version = x ]; then grep -Ei "^$package " $pkgs_file else grep -Ei "^$package +$version" $pkgs_file fi fi } function get_long_option_value() { echo $1|sed -E 's/^--[-A-z]+=(.*)$/\1/' } function initialize() { if [ -n "$1" ]; then mkdir -p `get_dest_dir $1` return fi remove `get_pkgs_file pear` remove `get_pkgs_file pecl` check_directory $TMP_DIR || usage 1 check_directory $DEST_DIR || usage 1 if [ -n "$PHP_DIR" ]; then PEAR=$PHP_DIR/bin/pear PECL=$PHP_DIR/bin/pecl fi } function local_package_file_to_package() { if [ -f $1 ]; then echo `basename $1`|sed -E 's/^([^-]+)-([0-9]+\..+)\.[^.]+$/\1 \2/' else return 1 fi } function mirror() { if [ ! -f `get_library_command $1` ]; then return fi initialize $1 delete_packages $1 download_packages $1 } function parse_options() { while [ -n "$1" ]; do case $1 in --dest-dir=*) DEST_DIR=`get_long_option_value $1` ;; --delete) DELETE=yes ;; --help) usage 0 ;; --php-dir=*) PHP_DIR=`get_long_option_value $1` ;; --verbose) VERBOSE=yes ;; *) usage 1 ;; esac shift done } function remove() { if [ -f $1 ]; then rm $1 fi } function remove_local_package() { remove_verbose `get_dest_dir $1`/$2-$3.tar remove_verbose `get_dest_dir $1`/$2-$3.tgz } function remove_verbose() { if [ ! -f $1 ]; then return fi if [ x$VERBOSE = xyes ]; then echo Remove $1 fi remove $1 } function usage() { echo Usage: $PROGRAM_NAME echo ' [--delete]' echo ' [--dest-dir=DIRECTORY]' echo ' [--help]' echo ' [--php-dir=PHP DIRECTORY]' echo ' [--verbose]' finish $1 } ######## # main # ######## parse_options $* initialize mirror pear mirror pecl finish