Files
lnx-arch/dots/bin/tp.sh

73 lines
2.1 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# klevstul :: 24.06
this_file_name=`basename "$0"`
if [ $# -lt 1 ]; then
echo "error: path to file is missing."
echo "usage: '$this_file_name [path_to_file]'"
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
2024-06-10 12:55:05 -05:00
cwd=$(pwd)
2024-06-10 12:56:52 -05:00
# nifty for debugging:
2024-06-12 17:45:29 -05:00
log_file="/home/poq/syncDir/0_downloads/tp.log"
2024-06-11 06:59:01 -05:00
#echo
#echo "debugging values:"
#echo "input args: $@"
#for i in "$@"; do echo " $i"; done;
#echo "sync_dir_sym: ${sync_dir_sym}"
#echo "sync_dir: ${sync_dir}"
#echo "cwd: ${cwd}"
#echo
2024-06-12 18:04:59 -05:00
#echo "---" >> ${log_file}
2024-06-12 17:49:40 -05:00
#echo "$@" >> ${log_file}
2024-06-10 12:55:05 -05:00
2024-06-10 13:13:27 -05:00
# loop through all files to delete
2024-06-11 06:59:01 -05:00
for input_file in "$@"; do
2024-06-12 17:45:29 -05:00
# lf file manager sends multiple files as one input parameter, split by with newlines
2024-06-12 18:04:59 -05:00
# https://superuser.com/questions/284187/bash-iterating-over-lines-in-a-variable
declare -a theArray
while read -r line
do
theArray+=("$line")
done <<< "$input_file"
for line in "${theArray[@]}"
do
#echo "$line" >> ${log_file}
# if input file has no path specified
2024-06-12 18:09:09 -05:00
if [[ ${line} != *"/"* ]]; then
line="${cwd}/${line}"
2024-06-12 18:04:59 -05:00
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"
#
# please note, if a file outside of the sync directory is tp'ed, then the replacement will not happen.
# this is great, as trash-put should work normally outside of the sync-dir. and for files residing outside,
# no replacemant should be done.
# new_file=${original_string//old_substring/new_substring}
2024-06-12 18:09:09 -05:00
new_file=${line//$sync_dir_sym/$sync_dir}
2024-06-12 18:04:59 -05:00
if [[ -f "${new_file}" ]] || [[ -d "${new_file}" ]] ; then
2024-06-12 18:11:42 -05:00
trash-put "${new_file}"
2024-06-12 18:04:59 -05:00
#echo "${new_file}"
2024-06-12 18:11:42 -05:00
#echo "${new_file}" >> ${log_file}
2024-06-12 18:04:59 -05:00
else
echo "error: unable to delete \"${new_file}\""
fi
done
2024-06-10 13:13:27 -05:00
done