nutools/lib/ulib/ulibsh

194 lines
6.4 KiB
Bash

##@cooked comments #-*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
## Version de ulib utilisable avec n'importe quel bourne shell: le script est
## relancé avec bash si nécessaire
##@cooked nocomments
##@inc[bash
## Relancer le script avec bash si nécessaire
if [ `basename "${BASH:-sh}"` = "sh" ]; then
bash=`which bash 2>/dev/null`
for bash in "$bash" /bin/bash /usr/bin/bash /usr/local/bin/bash; do
if [ -x "$bash" ]; then
exec "$bash" "$0" "$@"
fi
done
echo "error: this script requires bash"
exit 1
fi
##@inc]bash
##@inc[ulib
## Charger ulib. Nécessite bash.
function eerror() { echo "error: $*" 1>&2; }
function die() { [ -n "$*" ] && eerror "$*"; exit 1; }
ULIBDIR="${FORCED_ULIBDIR:-@@dest@@/lib/ulib}"
if [ "$ULIBDIR" = "@@""dest""@@/lib/ulib" ]; then
ULIBDIR="${BASH_SOURCE[0]}"
if [ -n "$ULIBDIR" -a -f "$ULIBDIR" ]; then
ULIBDIR="$(dirname "$ULIBDIR")"
else
ULIBDIR="$(dirname "$0")"
if [ -d "$ULIBDIR/ulib" ]; then
ULIBDIR="$ULIBDIR/ulib"
elif [ -d "$ULIBDIR/lib/ulib" ]; then
ULIBDIR="$ULIBDIR/lib/ulib"
fi
fi
fi
ULIBDIR="$(cd "$ULIBDIR" 2>/dev/null; pwd)"
ULIBDIRS=("$ULIBDIR")
ULIBINIT="$ULIBDIR"
[ -n "$ULIBPROVIDED" ] || ULIBPROVIDED=(ulib)
function uprovided() {
local module
for module in "${ULIBPROVIDED[@]}"; do
[ "$module" == "$1" ] && return 0
done
return 1
}
function uprovide() {
uprovided "$1" && return 1
ULIBPROVIDED=("${ULIBPROVIDED[@]}" "$1")
}
__ULIB_FORCE_RELOAD=
function urequire() {
[ -z "$__ULIB_NO_DISABLE_SET_X" ] && [[ $- == *x* ]] && { set +x; local __ULIB_UREQUIRE_SET_X=1; }; if [ -n "$__ULIB_UREQUIRE_SET_X" ]; then [ -n "$__ULIB_UREQUIRE_SET_X_RL1" ] || local __ULIB_UREQUIRE_SET_X_RL1; local __ULIB_UREQUIRE_SET_X_RL2=$RANDOM; [ -n "$__ULIB_UREQUIRE_SET_X_RL1" ] || __ULIB_UREQUIRE_SET_X_RL1=$__ULIB_UREQUIRE_SET_X_RL2; fi # désactiver set -x de manière réentrante
local __u_module __u_ulibdir __u_found
[ -n "$*" ] || set DEFAULTS
local __ulib_force_reload="$__ULIB_FORCE_RELOAD"
local __ULIB_FORCE_RELOAD
for __u_module in "$@"; do
__u_found=
for __u_ulibdir in "${ULIBDIRS[@]}"; do
if [ -f "$__u_ulibdir/$__u_module" ]; then
__u_found=1
if [ -n "$__ulib_force_reload" ] || ! uprovided "$__u_module"; then
uprovide "$__u_module"
source "$__u_ulibdir/$__u_module" || die
fi
break
fi
done
if [ -z "$__u_found" -a "$__u_module" == DEFAULTS ]; then
__u_found=1
for __u_module in base pretty sysinfos compat; do
if [ -n "$__ulib_force_reload" ] || ! uprovided "$__u_module"; then
uprovide "$__u_module"
source "$__u_ulibdir/$__u_module" || die
fi
done
fi
[ -n "$__u_found" ] || die "Unable to find $__u_module in ${ULIBDIR[*]}"
done
[ -n "$__ULIB_UREQUIRE_SET_X" -a "$__ULIB_UREQUIRE_SET_X_RL1" == "$__ULIB_UREQUIRE_SET_X_RL2" ] && set -x
return 0
}
function ulibadd() {
[ -d "$1" ] && ULIBDIRS=("${ULIBDIRS[@]}" "$(cd "$1"; pwd)")
}
function ulibsync() {
local destdir="$(abspath "${1:-.}")"
local -a __CPNOVCS_RSYNC_ARGS
__CPNOVCS_RSYNC_ARGS=(-q --delete)
[ "$destdir/ulib" != "$ULIBDIR" ] && cpdirnovcs "$ULIBDIR" "$destdir/ulib"
}
function __ulibver_parse_version() {
if [ -n "$2" ]; then
local version="${1:-${version:-000000000}}"
local major minor patch pversion
else
version="${1:-${version:-000000000}}"
fi
while [ ${#version} -lt 9 ]; do version="0$version"; done
major="${version:0:3}"; while [ ${#major} -gt 1 -a "${major#0}" != "$major" ]; do major="${major#0}"; done
minor="${version:3:3}"; while [ ${#minor} -gt 1 -a "${minor#0}" != "$minor" ]; do minor="${minor#0}"; done
patch="${version:6:3}"; while [ ${#patch} -gt 1 -a "${patch#0}" != "$patch" ]; do patch="${patch#0}"; done
pversion="$major.$minor.$patch"
[ -n "$2" ] && eval "${2}version=\$version; ${2}major=\$major; ${2}minor=\$minor; ${2}patch=\$patch; ${2}pversion=\$pversion"
}
function __ulibver_format_version() {
local major="${1:-${major:-0}}" minor="${2:-${minor:-0}}" patch="${3:-${patch:-0}}"
while [ ${#major} -lt 3 ]; do major="0$major"; done
while [ ${#minor} -lt 3 ]; do minor="0$minor"; done
while [ ${#patch} -lt 3 ]; do patch="0$patch"; done
echo "$major$minor$patch"
}
function __ulibver_parse_pversion() {
local v M m p
if [[ "$1" == *.* ]]; then
local v="$1"; shift
local M=0 m=0 p=0
if [[ "$v" == *.* ]]; then
p="${v##*.}"; v="${v%.*}"
if [[ "$v" == *.* ]]; then
m="${v##*.}"; v="${v%.*}"
if [[ "$v" == *.* ]]; then M="${v##*.}"; v="${v%.*}"
else M="$v"
fi
else m="$v"
fi
else p="$v"
fi
__ulibver_parse_version "$(__ulibver_format_version "$M" "$m" "$p")" "$@"
else
__ulibver_parse_version "$@"
fi
}
function ulibver() {
[ -f "$ULIBDIR/.ulibver" ] || return 1
local version=000000000 major minor patch pversion
__ulibver_parse_version "$(<"$ULIBDIR/.ulibver")"
if [ -n "$1" ]; then
local minversion=000000000 minmajor minminor minpatch minpversion
__ulibver_parse_pversion "$1" min
if [ "$major" -lt "$minmajor" ]; then
return 1
elif [ "$major" -eq "$minmajor" ]; then
if [ "$minor" -lt "$minminor" ]; then
return 1
elif [ "$minor" -eq "$minminor" ]; then
[ "$patch" -lt "$minpatch" ] && return 1
fi
fi
fi
if [ -n "$2" ]; then
local maxversion=000000000 maxmajor maxmaxor maxpatch maxpversion
__ulibver_parse_pversion "$2" max
if [ "$major" -gt "$maxmajor" ]; then
return 1
elif [ "$major" -eq "$maxmajor" ]; then
if [ "$minor" -gt "$maxminor" ]; then
return 1
elif [ "$minor" -eq "$maxminor" ]; then
[ "$patch" -ge "$maxpatch" ] && return 1
fi
fi
fi
return 0
}
function ulibver_require() {
ulibver "$@" && return 0
eerror "Ce script nécessite ulib version${1:+" >= $1"}${2:+" < $2"}"
return 1
}
##@inc]ulib
uprovide ulibsh