125 lines
2.7 KiB
Bash
125 lines
2.7 KiB
Bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
##@require sysinc/functions
|
|
##@require envsetup
|
|
|
|
envsetup_tmpfile=
|
|
function envsetup_init() {
|
|
ac_set_tmpfile envsetup_tmpfile
|
|
}
|
|
|
|
function move_after() {
|
|
# dans le fichier $1, déplacer la ligne $2 après la ligne $3. Utiliser $4
|
|
# comme fichier temporaire.
|
|
# si $3==*, mettre la ligne $2 en dernier
|
|
|
|
if [ "$3" != "*" ] && ! quietgrep "$3" "$1"; then
|
|
# la ligne "$3" doit exister
|
|
return
|
|
fi
|
|
|
|
uawk <"$1" >"$4" -v line="$2" -v after="$3" '
|
|
BEGIN {
|
|
found_line = 0
|
|
found_after = 0
|
|
}
|
|
! found_line && $0 == line {
|
|
found_line = 1
|
|
if (! found_after) {
|
|
next
|
|
}
|
|
}
|
|
! found_after && $0 == after {
|
|
found_after = 1
|
|
if (found_line) {
|
|
# si nous avons trouvé la ligne avant after, la mettre juste après
|
|
# sinon, par la peine de faire de modification
|
|
print
|
|
print line
|
|
next
|
|
}
|
|
}
|
|
{ print }
|
|
END {
|
|
if (! found_after && after == "*") {
|
|
if (found_line) {
|
|
print line
|
|
}
|
|
}
|
|
}
|
|
'
|
|
/bin/cp "$4" "$1"
|
|
}
|
|
|
|
function move_before() {
|
|
# dans le fichier $1, déplacer la ligne $2 avant la ligne $3. Utiliser $4
|
|
# comme fichier temporaire
|
|
# si $3==*, mettre la ligne $2 en premier
|
|
|
|
uawk <"$1" >"$4" -v line="$2" -v before="$3" '
|
|
BEGIN {
|
|
found_line = 0
|
|
found_before = 0
|
|
}
|
|
! found_line && $0 == line {
|
|
found_line = 1
|
|
if (found_before) {
|
|
next
|
|
}
|
|
}
|
|
! found_before && ($0 == before || before == "*") {
|
|
found_before = 1
|
|
if (! found_line) {
|
|
print line
|
|
print
|
|
next
|
|
}
|
|
}
|
|
{ print }
|
|
'
|
|
/bin/cp "$4" "$1"
|
|
}
|
|
|
|
envsetup_update_dir() {
|
|
# mettre à jour un répertoire qui contient des inclusions. $1=le répertoire,
|
|
# $2 un fichier temporaire qui est utilisé par cette fonction, $3=le
|
|
# répertoire de destination, qui vaut $1 par défaut
|
|
local system host user before after dir destdir file sio tmpfile
|
|
|
|
tmpfile="$2"
|
|
dir="$(cd "$1"; pwd)"
|
|
destdir="${3:-$dir}"
|
|
sio="$dir/.source_in_order"
|
|
|
|
>"$sio"
|
|
list_files "$dir" | while read file; do
|
|
__envsetup_check_file "$dir" "$file" && echo "$file" >>"$sio"
|
|
done
|
|
|
|
echo "$(< "$sio")" | while read file; do
|
|
[ -z "$file" ] && continue
|
|
|
|
before="$(awk <"$dir/$file" '
|
|
$0 ~ /^##@before / {
|
|
print $2
|
|
exit 0
|
|
}
|
|
')"
|
|
if [ -n "$before" ]; then
|
|
move_before "$sio" "$file" "$before" "$tmpfile"
|
|
fi
|
|
|
|
after="$(awk <"$dir/$file" '
|
|
$0 ~ /^##@after / {
|
|
print $2
|
|
exit 0
|
|
}
|
|
')"
|
|
if [ -n "$after" ]; then
|
|
move_after "$sio" "$file" "$after" "$tmpfile"
|
|
fi
|
|
done
|
|
|
|
uawk <"$sio" >"$tmpfile" -v destdir="$destdir" '{ print "source " destdir "/" $0 }'
|
|
/bin/cp "$tmpfile" "$sio"
|
|
}
|