sapphrc/lib/sapphrc/sapphrc-filelists
2024-05-30 22:33:49 -06:00

95 lines
2 KiB
Bash

#!/bin/bash
sapphrc_addfile(){
[ ${#@} -eq 0 ] && echo "No files selected" && exit 1
for file in "$@";do
fullpath=$(realpath "${file}")
if ! [[ "$fullpath" =~ ^"$HOME/".* ]];then
echo "file not in home dir"
break
fi
if ! [[ -d "$file" || -f "$file" ]];then
echo "file does not exist"
break
fi
filelist+=(${fullpath#$HOME/})
done
for e in ${filelist[@]};do
echo $e
done
read -p "${bold}Add the listed files/directories to profile '${PROFILE_NAME}'?${normal} (Y/n)" yn
if ! [[ "${yn}" =~ ^([nN][oO]|[nN])$ ]];then
for e in ${filelist[@]};do
if grep -q ^"${e}"$ "${sapphrc_config}/backups/${PROFILE_NAME}/${inex}";then
echo "already in file"
else
echo $e|tee -a "${sapphrc_config}/backups/${PROFILE_NAME}/${inex}" >/dev/null
fi
done
fi
}
sapphrc_rmfile(){
i=0
while read line; do
rmfiles+=("$i");i=$(($i+1))
rmfiles+=("$line")
rmfiles+=("$line")
done < "${sapphrc_config}/backups/${PROFILE_NAME}/${inex}"
[ ${#rmfiles[@]} -eq 0 ] && echo "No files in list" && exit 1
choices=($(dialog --checklist --output-fd 1 "Select options:" 0 0 0 "${rmfiles[@]}"))
if [ $? -ne 0 ]; then
echo "User canceled."
exit 1
fi
for choice in "${choices[@]}"; do
rmlines="${rmlines}$((${choice}+1))d;"
done
sed -i "${rmlines}" "${sapphrc_config}/backups/${PROFILE_NAME}/${inex}"
echo "Removed ${#choices[@]} paths from ${PROFILE_NAME} ${inex} list"
}
sapphrc_filelists(){
case "$1" in
include|exclude)
LIST="$1"
shift 1;;
*)
usage 1;;
esac
case "$1" in
add|remove)
OPERATION="$1"
shift 1;;
*)
usage 1;;
esac
while getopts 'hp:' flag; do
case "${flag}" in
p ) shift 2;PROFILE_NAME="${OPTARG}";;&
h ) usage 1 ;;
esac
done
validate_backupdir
validate_profile "${PROFILE}"
case "${OPERATION}" in
add)
echo "add"
sapphrc_addfile $@
;;
remove)
echo "remove"
sapphrc_rmfile $@
;;
*)
usage 1;;
esac
echo "$@"
}