Files
lnx-arch/dots/bin/lfMultiSelectHandler.sh
committer@tuxwarrior bc728ed1aa + lfMultiSelectHandler.sh
2025-04-12 15:11:33 -05:00

85 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# klevstul :: 25.04
this_file_name=`basename "$0"`
if [ $# -lt 2 ]; then
echo "error: path to file is missing."
echo "usage: '$this_file_name [callback] [input_array]'"
exit 1
fi
sync_dir_sym=/home/${USER}/syncDir
syncdir_env_var=SYNCDIR_${HOSTNAME}
sync_dir=${!syncdir_env_var} # '!' to use the name and not the value
cwd=$(pwd)
callback=$1
echo
echo "DEBUG: debugging values:"
echo "DEBUG: input args (all): $@"
echo "DEBUG: input args (one by one):"
for i in "$@"; do echo "DEBUG: $i"; done;
echo "DEBUG: sync_dir_sym: ${sync_dir_sym}"
echo "DEBUG: sync_dir: ${sync_dir}"
echo "DEBUG: cwd: ${cwd}"
echo "DEBUG: callback: ${callback}"
echo
# remove the first element (the callback) from $@
# https://stackoverflow.com/questions/2701400/remove-first-element-from-in-bash
shift
# loop through all files received as an parameter array
indexA=0
for input_file in "${@}"; do
indexA=$((indexA+1))
echo "- - - - - - - - - -"
echo "DEBUG [indexA]: ${indexA}"
echo "DEBUG [\$@]: $@"
echo "DEBUG [input_file]: ${input_file[@]}"
# lf file manager sends multiple files as one input parameter, split by with newlines
# https://superuser.com/questions/284187/bash-iterating-over-lines-in-a-variable
declare -a theArray
# reset array | https://stackoverflow.com/questions/28737493/resetting-an-array-and-filling-it-with-values-in-a-bash-script
theArray=()
while read -r arrayLine
do
theArray+=("$arrayLine")
done <<< "$input_file"
echo "DEBUG [theArray]: ${theArray[@]}"
indexB=0
for line in "${theArray[@]}"
do
indexB=$((indexB+1))
echo " DEBUG [indexB]: ${indexB}"
echo " DEBUG [line]: ${line}"
# if input file has no path specified
if [[ ${line} != *"/"* ]]; then
line="${cwd}/${line}"
fi
# substitute parts of the old path, from using the symlink folder, to the non-symlink folder
# example:
# "/home/poq/syncDir/0_downloads/topBanner.jpg" > "/home/poq/nextcloud/syncDir/0_downloads/topBanner.jpg"
# new_file=${original_string//old_substring/new_substring}
new_file=${line//$sync_dir_sym/$sync_dir}
if [[ -f "${new_file}" ]] || [[ -d "${new_file}" ]] ; then
echo " DEBUG: executing '${callback} ${new_file}'"
${callback} ${new_file}
else
echo "ERROR: file not found: \"${new_file}\""
fi
done
done