implemented patchfile and pre post action of package manager, renamed pkgrem -> pkgrm

This commit is contained in:
levasseur
2021-10-25 17:33:23 +02:00
parent c45078fbe7
commit 6fa6dcc24d
7 changed files with 208 additions and 12 deletions

View File

@@ -177,9 +177,39 @@ isdirempty()
# ------------------------------------------------------------------------------
# patch a file replacing all @var@ by the corresponding value in environment
# or the variable list given in parameter
patchfile()
{
: # todo
local dstfile=$1 && shift
local workfile=${dstfile}.work
# Create a sub-process, to avoid bash environment pollution
(
local varlist= pattern=
if [[ $# -eq 0 ]] ; then
pattern="-e s/<\(.*\)>/\$\1\$\1/g"
else
local var=
for var in $* ; do
if ! declare -p $var >/dev/null 2>&1 ; then
local $var=$(eval echo \$$var)
fi
export $var
pattern="$pattern -e s/@$var@/\$$var/g"
varlist=$varlist\$$var
done
fi
# sed replace <VAR> with \$$VAR and envsubst do the replace by value
sed $pattern $dstfile | envsubst ${varlist:+"$varlist"} > "$workfile"
)
local -a rights=( $(stat --printf="%a %u %g" "$dstfile") )
mv "$workfile" "$dstfile"
chmod ${rights[0]} "$dstfile"
chown ${rights[1]}:${rights[2]} "$dstfile"
unset dstfile
}
# EOF