nutools/lib/ulib/ulibsh

190 lines
5.9 KiB
Plaintext
Raw Normal View History

2013-08-27 15:14:44 +04:00
##@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
2014-07-08 10:24:03 +04:00
## Charger ulib. Nécessite bash.
2013-08-27 15:14:44 +04:00
function eerror() { echo "error: $*" 1>&2; }
function die() { [ -n "$*" ] && eerror "$*"; exit 1; }
2014-07-07 22:06:38 +04:00
ULIBDIR="${FORCED_ULIBDIR:-@@dest@@/lib/ulib}"
2013-08-27 15:14:44 +04:00
2014-07-07 22:06:38 +04:00
if [ "$ULIBDIR" = "@@""dest""@@/lib/ulib" ]; then
2013-08-27 15:14:44 +04:00
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
2013-08-27 15:14:44 +04:00
fi
fi
ULIBDIR="$(cd "$ULIBDIR" 2>/dev/null; pwd)"
2013-08-27 15:14:44 +04:00
ULIBDIRS=("$ULIBDIR")
ULIBINIT="$ULIBDIR"
[ -n "$ULIBPROVIDED" ] || ULIBPROVIDED=(ulib)
function uprovided() {
local module
for module in "${ULIBPROVIDED[@]}"; do
[ "$module" == "$1" ] && return 0
2013-08-27 15:14:44 +04:00
done
return 1
}
function uprovide() {
uprovided "$1" && return 1
2013-08-27 15:14:44 +04:00
ULIBPROVIDED=("${ULIBPROVIDED[@]}" "$1")
}
2014-09-29 21:00:07 +04:00
__ULIB_FORCE_RELOAD=
2013-08-27 15:14:44 +04:00
function urequire() {
local __u_module __u_ulibdir __u_found
2013-08-27 15:14:44 +04:00
[ -n "$*" ] || set DEFAULTS
2014-12-26 16:09:41 +04:00
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
2014-12-26 16:09:41 +04:00
if [ -n "$__ulib_force_reload" ] || ! uprovided "$__u_module"; then
uprovide "$__u_module"
source "$__u_ulibdir/$__u_module" || die
2013-08-27 15:14:44 +04:00
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
2014-12-26 16:09:41 +04:00
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[*]}"
2013-08-27 15:14:44 +04:00
done
return 0
2013-08-27 15:14:44 +04:00
}
function ulibadd() {
2013-08-27 15:14:44 +04:00
[ -d "$1" ] && ULIBDIRS=("${ULIBDIRS[@]}" "$(cd "$1"; pwd)")
}
function ulibsync() {
local destdir="$(abspath "${1:-.}")"
local -a __CPNOVCS_RSYNC_ARGS
__CPNOVCS_RSYNC_ARGS=(-q --delete)
2013-08-27 15:14:44 +04:00
[ "$destdir/ulib" != "$ULIBDIR" ] && cpdirnovcs "$ULIBDIR" "$destdir/ulib"
}
2014-09-15 08:12:04 +04:00
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
}
2013-08-27 15:14:44 +04:00
##@inc]ulib
uprovide ulibsh