implemented patchfile and pre post action of package manager, renamed pkgrem -> pkgrm
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user