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
|
|
|
|
# Code de support pour les architectures autre que Linux
|
|
|
|
##@cooked nocomments
|
|
|
|
##@require base
|
|
|
|
##@require sysinfos
|
|
|
|
uprovide compat
|
|
|
|
urequire base sysinfos
|
|
|
|
|
|
|
|
if check_sysinfos -s macosx; then
|
2013-12-04 01:11:09 +04:00
|
|
|
# implémentation en bash des variantes de readlink
|
|
|
|
function __rlnp() { local p="$1" np f; [ "${p#/}" == "$p" ] && p="$(pwd)/$p"; while [[ "$p" == *//* ]]; do p="${p//\/\///}"; done; p="${p%/}"; p="${p#/}"; np=; while [ -n "$p" ]; do if [[ "$p" == */* ]]; then f="${p%%/*}"; p="${p#*/}"; else f="$p"; p=; fi; if [ "$f" == . ]; then :; elif [ "$f" == .. ]; then if [[ "$np" == */* ]]; then np="${np%/*}"; else np=; fi; else [ -n "$np" ] && np="$np/"; np="$np$f"; fi; done; echo "/$np"; }
|
|
|
|
function readlinkm() { local p="$(__rlnp "$1")" np n; while [ -n "$p" ]; do local max=50; while [ -L "$p" -a $max -gt 0 ]; do n="$(readlink "$p")"; if [ "${n#/}" != "$n" ]; then p="$n"; else p="${p%/*}/$n"; fi; p="$(__rlnp "$p")"; max=$(($max-1)); done; [ -n "$np" ] && np="/$np"; if [[ "$p" == */* ]]; then np="${p##*/}$np"; p="${p%/*}"; else np="$p$np"; p=; fi; done; echo "/$np"; }
|
2013-08-27 15:14:44 +04:00
|
|
|
# la version de awk livrée sur macosx est braindead
|
|
|
|
function _nl2lf() { awk '{ sub(/\r$/, ""); gsub(/\r/, "\n"); print }'; }
|
|
|
|
function _nl2crlf() { _nl2lf | awk '{ print $0 "\r" }'; }
|
|
|
|
function _nl2cr() { _nl2lf | awk 'BEGIN { ORS="" } { print $0 "\r" }'; }
|
|
|
|
# sed -i demande un argument
|
|
|
|
function sedi() { sed -i '' "$@"; }
|
|
|
|
|
|
|
|
## gestion des arguments
|
|
|
|
function __po_check_options() {
|
|
|
|
# émuler gnu getopt puisque qu'il n'est pas disponible sur cette
|
|
|
|
# plateforme
|
|
|
|
local -a options args
|
|
|
|
local option value flag
|
|
|
|
if [ "${opts_#+}" != "$opts_" ]; then
|
|
|
|
# Arrêter au premier argument qui n'est pas une option
|
|
|
|
while [ -n "$*" ]; do
|
|
|
|
if [ "$1" == "--" ]; then
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
elif [[ "$1" == --* ]]; then
|
|
|
|
# option longue
|
|
|
|
option="${1%%=*}"
|
|
|
|
if flag="$(array_find options_ "$option" flags_)"; then
|
|
|
|
if [[ "$flag" == ::* ]]; then
|
|
|
|
# valeur optionnelle
|
|
|
|
if [[ "$1" == *=* ]]; then
|
|
|
|
value="${1#--*=}"
|
|
|
|
else
|
|
|
|
value=
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
elif [[ "$flag" == :* ]]; then
|
|
|
|
# valeur obligatoire
|
|
|
|
if [[ "$1" == *=* ]]; then
|
|
|
|
value="${1#--*=}"
|
|
|
|
shift
|
|
|
|
elif [ $# -gt 1 ]; then
|
|
|
|
value="$2"
|
|
|
|
shift 2
|
|
|
|
else
|
|
|
|
echo "option requires an argument: $option"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
else
|
|
|
|
# pas de valeur
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option")
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "invalid option: $1"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
elif [[ "$1" == -* ]]; then
|
|
|
|
# option courte
|
|
|
|
option="${1:0:2}"
|
|
|
|
if flag="$(array_find options_ "$option" flags_)"; then
|
|
|
|
if [[ "$flag" == ::* ]]; then
|
|
|
|
# valeur optionnelle
|
|
|
|
value="${1:2}"
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
elif [[ "$flag" == :* ]]; then
|
|
|
|
# valeur obligatoire
|
|
|
|
if [[ -n "${1:2}" ]]; then
|
|
|
|
value="${1:2}"
|
|
|
|
shift
|
|
|
|
elif [ $# -gt 1 ]; then
|
|
|
|
value="$2"
|
|
|
|
shift 2
|
|
|
|
else
|
|
|
|
echo "option requires an argument: $option"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
else
|
|
|
|
# pas de valeur
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option")
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "invalid option: $1"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
# ce n'est pas une option, on arrête là
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
args=("$@")
|
|
|
|
elif [ "${opts_#-}" != "$opts_" ]; then
|
|
|
|
# Les options peuvent être situées partout sur la ligne de commande,
|
|
|
|
# mais les arguments ne sont pas réordonnés.
|
|
|
|
while [ -n "$*" ]; do
|
|
|
|
if [ "$1" == "--" ]; then
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
elif [[ "$1" == --* ]]; then
|
|
|
|
# option longue
|
|
|
|
option="${1%%=*}"
|
|
|
|
if flag="$(array_find options_ "$option" flags_)"; then
|
|
|
|
if [[ "$flag" == ::* ]]; then
|
|
|
|
# valeur optionnelle
|
|
|
|
if [[ "$1" == *=* ]]; then
|
|
|
|
value="${1#--*=}"
|
|
|
|
else
|
|
|
|
value=
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
elif [[ "$flag" == :* ]]; then
|
|
|
|
# valeur obligatoire
|
|
|
|
if [[ "$1" == *=* ]]; then
|
|
|
|
value="${1#--*=}"
|
|
|
|
shift
|
|
|
|
elif [ $# -gt 1 ]; then
|
|
|
|
value="$2"
|
|
|
|
shift 2
|
|
|
|
else
|
|
|
|
echo "option requires an argument: $option"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
else
|
|
|
|
# pas de valeur
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option")
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "invalid option: $1"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
elif [[ "$1" == -* ]]; then
|
|
|
|
# option courte
|
|
|
|
option="${1:0:2}"
|
|
|
|
if flag="$(array_find options_ "$option" flags_)"; then
|
|
|
|
if [[ "$flag" == ::* ]]; then
|
|
|
|
# valeur optionnelle
|
|
|
|
value="${1:2}"
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
elif [[ "$flag" == :* ]]; then
|
|
|
|
# valeur obligatoire
|
|
|
|
if [[ -n "${1:2}" ]]; then
|
|
|
|
value="${1:2}"
|
|
|
|
shift
|
|
|
|
elif [ $# -gt 1 ]; then
|
|
|
|
value="$2"
|
|
|
|
shift 2
|
|
|
|
else
|
|
|
|
echo "option requires an argument: $option"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
else
|
|
|
|
# pas de valeur
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option")
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "invalid option: $1"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
# ce n'est pas une option, mais l'ajouter aux options parce
|
|
|
|
# qu'on ne réordonne pas
|
|
|
|
options=("${options[@]}" "$1")
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
args=("$@")
|
|
|
|
else
|
|
|
|
# Les options peuvent être situées partout sur la ligne de commande.
|
|
|
|
# Les arguments sont réordonnés pour placer les options en premier,
|
|
|
|
# séparées des non-options par '--'
|
|
|
|
while [ -n "$*" ]; do
|
|
|
|
if [ "$1" == "--" ]; then
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
elif [[ "$1" == --* ]]; then
|
|
|
|
# option longue
|
|
|
|
option="${1%%=*}"
|
|
|
|
if flag="$(array_find options_ "$option" flags_)"; then
|
|
|
|
if [[ "$flag" == ::* ]]; then
|
|
|
|
# valeur optionnelle
|
|
|
|
if [[ "$1" == *=* ]]; then
|
|
|
|
value="${1#--*=}"
|
|
|
|
else
|
|
|
|
value=
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
elif [[ "$flag" == :* ]]; then
|
|
|
|
# valeur obligatoire
|
|
|
|
if [[ "$1" == *=* ]]; then
|
|
|
|
value="${1#--*=}"
|
|
|
|
shift
|
|
|
|
elif [ $# -gt 1 ]; then
|
|
|
|
value="$2"
|
|
|
|
shift 2
|
|
|
|
else
|
|
|
|
echo "option requires an argument: $option"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
else
|
|
|
|
# pas de valeur
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option")
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "invalid option: $1"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
elif [[ "$1" == -* ]]; then
|
|
|
|
# option courte
|
|
|
|
option="${1:0:2}"
|
|
|
|
if flag="$(array_find options_ "$option" flags_)"; then
|
|
|
|
if [[ "$flag" == ::* ]]; then
|
|
|
|
# valeur optionnelle
|
|
|
|
value="${1:2}"
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
elif [[ "$flag" == :* ]]; then
|
|
|
|
# valeur obligatoire
|
|
|
|
if [[ -n "${1:2}" ]]; then
|
|
|
|
value="${1:2}"
|
|
|
|
shift
|
|
|
|
elif [ $# -gt 1 ]; then
|
|
|
|
value="$2"
|
|
|
|
shift 2
|
|
|
|
else
|
|
|
|
echo "option requires an argument: $option"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
options=("${options[@]}" "$option" "$value")
|
|
|
|
else
|
|
|
|
# pas de valeur
|
|
|
|
shift
|
|
|
|
options=("${options[@]}" "$option")
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "invalid option: $1"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
# ce n'est pas une option, l'ajouter aux arguments
|
|
|
|
args=("${args[@]}" "$1")
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
args=("${args[@]}" "$@")
|
|
|
|
fi
|
|
|
|
quoted_args "${options[@]}" -- "${args[@]}"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
## gestion des dates
|
|
|
|
function __pd_isleap() {
|
|
|
|
# tester si l'année $1 est bissextile
|
|
|
|
[ $(($1 % 4)) -eq 0 -a \( $(($1 % 100)) -ne 0 -o $(($1 % 400)) -eq 0 \) ]
|
|
|
|
}
|
|
|
|
function __pd_fix_month() {
|
|
|
|
# soit $1 le nom d'une variable contenant une année, et $2 le nom d'une
|
|
|
|
# variable contenant un mois, normaliser les valeurs de ces variables, en
|
|
|
|
# ajustant si nécessaire les valeurs.
|
|
|
|
local __pdfm_y="${!1}" __pdfm_m="${!2}"
|
|
|
|
let __pdfm_m=$__pdfm_m-1
|
|
|
|
while [ $__pdfm_m -gt 11 ]; do
|
|
|
|
let __pdfm_m=$__pdfm_m-12
|
|
|
|
let __pdfm_y=$__pdfm_y+1
|
|
|
|
done
|
|
|
|
while [ $__pdfm_m -lt 0 ]; do
|
|
|
|
let __pdfm_m=$__pdfm_m+12
|
|
|
|
let __pdfm_y=$__pdfm_y-1
|
|
|
|
done
|
|
|
|
let __pdfm_m=$__pdfm_m+1
|
|
|
|
eval "$1=$__pdfm_y; $2=$__pdfm_m"
|
|
|
|
}
|
|
|
|
__PD_MONTHDAYS=(0 31 28 31 30 31 30 31 31 30 31 30 31)
|
|
|
|
function __pd_monthdays() {
|
|
|
|
# calculer le nombre de jours du mois $2 de l'année $1
|
|
|
|
local y="$1" m="$2" mds
|
|
|
|
__pd_fix_month y m
|
|
|
|
mds="${__PD_MONTHDAYS[$m]}"
|
|
|
|
[ "$m" -eq 2 ] && __pd_isleap "$y" && let mds=$mds+1
|
|
|
|
echo $mds
|
|
|
|
}
|
|
|
|
function __pd_fix_day() {
|
|
|
|
# soit $1 le nom d'une variable contenant une année, $2 le nom d'une
|
|
|
|
# variable contenant un mois et $3 le nom d'une variable contenant un jour,
|
|
|
|
# normaliser les valeurs de ces variables, en ajustant si nécessaire les
|
|
|
|
# valeurs. cette fonction assume que la valeur de $2 est déjà corrigée avec
|
|
|
|
# __pd_fix_month
|
|
|
|
local __pdfd_y="${!1}" __pdfd_m="${!2}" __pdfd_d="${!3}" __pdfd_mds
|
|
|
|
let __pdfd_d=$__pdfd_d-1
|
|
|
|
let __pdfd_mds=$(__pd_monthdays $__pdfd_y $__pdfd_m)
|
|
|
|
while [ $__pdfd_d -gt $(($__pdfd_mds-1)) ]; do
|
|
|
|
let __pdfd_d=$__pdfd_d-$__pdfd_mds
|
|
|
|
let __pdfd_m=$__pdfd_m+1
|
|
|
|
__pd_fix_month __pdfd_y __pdfd_m
|
|
|
|
let __pdfd_mds=$(__pd_monthdays $__pdfd_y $__pdfd_m)
|
|
|
|
done
|
|
|
|
while [ $__pdfd_d -lt 0 ]; do
|
|
|
|
let __pdfd_m=$__pdfd_m-1
|
|
|
|
__pd_fix_month __pdfd_y __pdfd_m
|
|
|
|
let __pdfd_d=$__pdfd_d-$(__pd_monthdays $__pdfd_y $__pdfd_m)
|
|
|
|
done
|
|
|
|
let __pdfd_d=$__pdfd_d+1
|
|
|
|
eval "$1=$__pdfd_y; $2=$__pdfd_m; $3=$__pdfd_d"
|
|
|
|
}
|
|
|
|
function __pd_fix_date() {
|
|
|
|
# soit $1 le nom d'une variable contenant une année, $2 le nom d'une
|
|
|
|
# variable contenant un mois et $3 le nom d'une variable contenant un jour,
|
|
|
|
# normaliser les valeurs de ces variables, en ajustant si nécessaire les
|
|
|
|
# valeurs.
|
|
|
|
local __pdf_y="${!1}" __pdf_m="${!2}" __pdf_d="${!3}"
|
|
|
|
}
|
|
|
|
function parse_date() {
|
|
|
|
local value="$1" type="${2:-date}"
|
|
|
|
local d m y
|
|
|
|
# date courante
|
|
|
|
eval "$(date +%d/%m/%Y | awk -F/ '{
|
|
|
|
print "d=" $1 "; m=" $2 "; y=" $3
|
|
|
|
}')"
|
|
|
|
if [ "${value#+}" != "$value" ]; then
|
|
|
|
# ajouter $1 jours
|
|
|
|
d="$(($d+${value#+}))"
|
|
|
|
else
|
|
|
|
# parser une nouvelle date, en complétant avec les informations de la
|
|
|
|
# date du jour
|
|
|
|
eval "$(<<<"$value" awkrun FS=/ dn="$dn" mn="$mn" yn="$yn" type="$type" '{
|
|
|
|
d = $1 + 0; if (d < 1) d = dn + 0;
|
|
|
|
m = $2 + 0; if (m < 1) m = mn + 0;
|
|
|
|
if ($3 == "") y = yn + 0;
|
|
|
|
else { y = $3 + 0; if (y < 100) y = y + 2000; }
|
|
|
|
print "d=" d "; m=" m "; y=" y
|
|
|
|
}')"
|
|
|
|
fi
|
|
|
|
# ensuite corriger les champs si nécessaire
|
|
|
|
__pd_fix_month y m
|
|
|
|
__pd_fix_day y m d
|
|
|
|
# enfin formater la date selon les volontés de l'utilisateur
|
|
|
|
case "$type" in
|
|
|
|
d|date)
|
|
|
|
awkrun d="$d" m="$m" y="$y" 'BEGIN { printf "%02i/%02i/%04i\n", d, m, y }'
|
|
|
|
;;
|
|
|
|
l|ldap)
|
|
|
|
awkrun d="$d" m="$m" y="$y" 'BEGIN { printf "%04i%02i%02i000000+0400\n", y, m, d }'
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
function myhost() {
|
|
|
|
hostname
|
|
|
|
}
|
|
|
|
function myhostname() {
|
|
|
|
hostname -s
|
|
|
|
}
|
|
|
|
fi
|