dotfiles/overlays/symlink-state/symlink-state
magic_rb a1bd85b6b8
Always run symlink-state with bash
Signed-off-by: magic_rb <magic_rb@redalder.org>
2024-02-09 12:46:03 +01:00

66 lines
1.4 KiB
Bash

# -*- mode: shell-script -*-
export PATH=@path@:$PATH
if [[ -z "$BASH_VERSION" ]] ; then
exec bash "$0" "$@"
fi
export _operation="${1}"
export _source="${2}"
export _destination="${3}"
if [[ $# < 3 ]]; then
echo "3 arguments required, OP SRC DST" ; exit 1
fi
function linkFile()
{
declare -a _rw_paths
declare -a _ignore_paths
IFS=':' read -a _rw_paths <<< "$RW_PATHS"
IFS=':' read -a _ignore_paths <<< "$IGNORE_PATHS"
ignore=0
for ignore_path in ${_ignore_paths[@]} ; do
if [ "${1##ignore_path}" != "$1" ] || [ "$ignore_path" = "$1" ] ; then
ignore=1
break
fi
done
if [ -f "$1" ] || [ $ignore = 1 ] ; then
return
fi
mkdir -p "$_destination/$(dirname "$1")"
copy=0
for rw_path in ${_rw_paths[@]} ; do
if [ "${1##$rw_path}" != "$1" ] || [ "$rw_path" = "$1" ] ; then
copy=1
break
fi
done
if [ "$copy" = 1 ] ; then
cp --no-preserve=mode,ownership "$_source/$1" "$_destination/$1"
else
ln -s "$_source/$1" "$_destination/$1"
fi
}
export -f linkFile
case "$_operation" in
"setup")
find $_source -type f -printf '%P\n' | tr '\n' '\0' | xargs -0 -I {} sh -c 'linkFile "$1"' sh {}
;;
"destroy")
find $_destination -type l -lname "$_source"'*' -delete
find $_destination -mindepth 1 -type d -empty -delete
;;
*)
echo "unknown operation" ; exit 1
esac