#!/bin/sh
nb_options=0
name=$(cat meta/name 2>/dev/null)
version=$(cat meta/version 2>/dev/null)
echo()
{
IFS=' '
printf %s\\n "$*"
}
echon()
{
IFS=' '
printf %s "$*"
}
die()
{
local r=$1
shift
echo "${0##*/}: $*" >&2
exit $r
}
dief()
{
local r=$1
shift
die $r "fatal: $*"
}
error()
{
dief "$@"
}
warn()
{
echo "${0##*/}: warning: $*" >&2
}
upper()
{
printf "%s" "$@" | tr '[:lower:]' '[:upper:]'
}
sanitize()
{
printf "%s" "$@" | tr '[:lower:]' '[:upper:]' | tr -c '[:alnum:]' _
}
quote()
{
printf %s\\n "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/" ;
}
trim()
{
echo $@
}
append()
{
local name=$1
shift
local v="$(eval echo "\$$name")"
if test -z "$v"; then
eval "$name=$(printf \"%s\" \"\$@\")"
else
eval "$name=$(printf \"%s\\n%s\" \$$name \"\$@\")"
fi
}
fnmatch()
{
case "$2" in $1) return 0 ;; *) return 1 ;; esac ;
}
save()
{
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
run()
{
"$@"
local r=$?
if test $r -ne 0; then
error 3 "Failed ($r): $@"
fi
}
tryrun()
{
"$@"
local r=$?
if test $r -ne 0; then
warn "Failed ($r): $@"
fi
}
foreach()
{
local name="$1"
local nb="$2"
local fn="$3"
shift 3
local i=1
nb=$(($nb+0))
while test $i -le $nb; do
"$fn" $i "$(eval "echo \$$name$i")" "$@"
i=$(($i+1))
done
}
vercmp()
{
got="$1"
need="$2"
if test "$got" = "$need"; then
echo 0
return
fi
l1="$(echo $got |tr -d '[:alnum:]')"
l2="$(echo $need|tr -d '[:alnum:]')"
l1=${#l1}
l2=${#l2}
if test $l1 -lt $l2; then
n=$l1
else
n=$l2
fi
n=$(($n+1))
i=1
while test $i -le $n; do
n1="$(echo $got |cut -d. -f$i)"
n2="$(echo $need|cut -d. -f$i)"
if test -z "$(echo $n1|tr -d '[:digit:]')" \
&& test -z "$(echo $n2|tr -d '[:digit:]')"; then
if test $n1 -lt $n2; then
echo -1
return
elif test $n1 -gt $n2; then
echo 1
return
fi
elif test "$n1" != "$n2"; then
# if there's a non-digit part somewhere we just assume to have a
# lower version
echo -1
return
fi
i=$(($i+1))
done
if test $l1 -gt $l2; then
echo 1
else
echo -1
fi
}
loadoptions()
{
if ! test -d meta/options; then return 1; fi
local name=
local desc=
local cvar=
local cflags=
local ldflags=
local disabled=
local invisible=
for name in meta/options/*; do
name=${name##*/}
if test "$name" != "$(echo $name | tr -cd '[[:alnum:]]')"; then
warn "invalid option name: $name"
continue
fi
if test ! -r meta/options/$name/desc; then
warn "decription missing for option $name"
continue
fi
desc="$(cat meta/options/$name/desc 2>/dev/null)"
if test -z "$desc" || test -e meta/options/$name/invisible; then
invisible=1
else
invisible=0
fi
cvar="$(cat meta/options/$name/cvar 2>/dev/null)"
if test -z "$cvar"; then cvar="WITH_$(sanitize $name)"; fi
cflags="$(cat meta/options/$name/cflags 2>/dev/null)"
ldflags="$(cat meta/options/$name/ldflags 2>/dev/null)"
disabled=-1
if test -e meta/options/$name/default; then
local s="$(cat meta/options/$name/default)"
if test "$s" = "0"; then
disabled=1
elif test "$s" = "1"; then
disabled=0
else
warn "invalid default value for option $name ($s)"
fi
fi
dep="$(cat meta/options/$name/dep 2>/dev/null)"
nb_options=$(($nb_options+1))
eval "optname$nb_options"="$name"
eval "optdesc$name"=\"$desc\"
eval "optcvar$name"=\"$cvar\"
eval "optcflags$name"=\"$cflags\"
eval "optldflags$name"=\"$ldflags\"
eval "optdisabled$name"=$disabled
eval "optinvisible$name"=$invisible
eval "optafter$name"=0
local tmp
eval tmp=\"\$need$dep\"
if test -z "$tmp"; then
eval need$dep="$name"
elif test "$tmp" = "1"; then
eval need$dep="$name"
else
eval need$dep=\"$\need$dep $name\"
fi
done
local opt=
for opt in meta/options/*/after; do
if test "$opt" = "meta/options/*/after"; then break; fi
opt=$(expr substr "$opt" 14 $((${#opt} - 19)))
if test "$opt" != "$(echo $opt | tr -cd '[[:alnum:]]')"; then
continue
fi
local after=0
for name in meta/options/$opt/after/*; do
if test "$name" = "meta/options/$opt/after/*"; then break; fi
name=${name##*/}
local idx=0
local n=$nb_options
while test $n -gt 0; do
if test "$(eval echo \$optname$n)" = "$name"; then
idx=$n
n=0
fi
n=$((n - 1))
done
if test $idx -eq 0; then continue; fi
n=$((1 << $idx - 1))
after=$(($after | $n))
done
eval "optafter$opt"=$after
done
return 1
}
setoptenv()
{
local name=$2
local disabled
eval disabled=\$optdisabled$name
if test $disabled -eq 1; then
vars="WITH_OPT_$name=0 $vars"
elif test $disabled -eq 0; then
vars="WITH_OPT_$name=1 $vars"
fi
}
setoptdef()
{
local name=$2
local disabled
eval disabled=\$optdisabled$name
if test $disabled -eq -1; then
local vars=
foreach "optname" $nb_options setoptenv
if env $vars meta/options/$name/isdefault 2>/dev/null; then
disabled=0
else
disabled=1
fi
eval "optdisabled$name"=$disabled
fi
}
setoptionsdefault()
{
foreach "optname" $nb_options setoptdef
}
optexist()
{
if test -e meta/options/"$1"; then echo 1; else echo 0; fi
}
setneeddep()
{
local dep=$2
local depname="${dep##*/}"
local optname=$3
local tmp
eval tmp=\"\$need$depname\"
tmp="$(echo $tmp | sed s/$optname//)"
eval need$depname=\"$tmp\"
}
setoptionsdep()
{
local name=$2
local disabled
eval disabled=\$optdisabled$name
if test $disabled -eq 1; then
foreach "cpnt" $nb_cpnt setneeddep $name
foreach "deps" $nb_deps setneeddep $name
fi
}
loaddeps()
{
nb_cpnt=0
nb_deps=0
if test -d meta/deps; then
for dep in meta/deps/*; do
local depname="${dep##*/}"
local tmp
if test -e "$dep/cpnt"; then
nb_cpnt=$(($nb_cpnt+1))
tmp="cpnt$nb_cpnt"
else
nb_deps=$(($nb_deps+1))
tmp="deps$nb_deps"
fi
eval $tmp=\"$dep\"
eval tmp=\"\$need$depname\"
if test -z "$tmp"; then
eval need$depname=\"1\"
fi
done
foreach optname $nb_options setoptionsdep
fi
}
getchangelog()
{
if test $# -eq 1 && test $1 -gt 0; then
n=1
else
n=2
fi
if test ! -f meta/HISTORY; then return; fi
cat meta/HISTORY | awk -v n=$n '
BEGIN { i=0 }
$1=="#" { if (++i>n) exit }
{ if ($1 != "#" && i==n) print }
'
}
test -n "$name" || error 2 "Cannot find project's name"
test -n "$version" || error 2 "Cannot find project's version"
prjname=$name
prjver=$version