sourcezap/libexec/srczap/srczap-install
2024-05-11 16:43:59 -03:00

78 lines
1.6 KiB
Bash
Executable file

#!/bin/sh
set -e
##
# variables
gitdir=$1
installdir=$2
revfile=$3
libexec=$(dirname "$0")
mode="u=rwX,g=rX,o="
##
# functions
perform_update()
{
rev=$(cat "${revfile}")
add=$("${libexec}"/git-changed-files "${gitdir}" "${rev}")
del=$("${libexec}"/git-removed-files "${gitdir}" "${rev}")
for file in ${del}; do
target="${installdir}/${file}"
parent=$(dirname "${target}")
echo "rm ${target}"
rm -f "${target}"
find "${parent}" -type d -maxdepth 0 -empty -delete
done
for file in ${add}; do
target="${installdir}/${file}"
parent=$(dirname "${target}")
parents=""
while [ ! -e "${parent}" ]; do
parents="${parent} ${parents}"
parent=$(dirname "${parent}")
done
for dir in ${parents}; do
run_install "-d" "${dir}"
done
run_install "${file}" "${target}"
done
}
perform_install()
{
find -s . \
-maxdepth 1 \
! -name "." \
! -name ".git" \
! -name ".gitignore" \
! -name ".hooks" \
! -name ".arcconfig" \
-exec cp -Rpv {} "${installdir}" \;
set -x
chown -R root "${installdir}"
}
run_install()
{
install -o root -g _srczap -m "${mode}" -v "$@"
}
##
# main
if [ "$(id -u)" != "0" ]; then
echo "[-] This command must be run by root"
exit 1
fi
set -x
umask ${mode}
cd "${gitdir}"
set +x
run_install "-d" "${installdir}"
chmod ${mode} "${installdir}"
if [ -e "${revfile}" ]; then
perform_update
else
perform_install
fi
"${libexec}"/git-rev "${gitdir}" > "${revfile}"