mirror of
https://github.com/kevinnlsamuel/bak-old.git
synced 2025-12-06 10:05:58 +05:30
cleanup and rearrangement
- rearrange sections - convert tabs to spaces (thanks VSCode) - general indentation fixes
This commit is contained in:
114
bak-old.sh
114
bak-old.sh
@@ -7,21 +7,17 @@
|
||||
###################################
|
||||
version='0.1.0-beta'
|
||||
|
||||
### IFS precautions
|
||||
### IFS precautions #
|
||||
OLDIFS="$IFS"
|
||||
unset IFS
|
||||
|
||||
### parse parameters
|
||||
# file names
|
||||
# `-d` dest dir
|
||||
# `-b | -o` mode: cloned file suffix
|
||||
# ` ` follow symlinks
|
||||
# ` ` lightweight copies
|
||||
|
||||
debug(){
|
||||
set -xev
|
||||
debug(){ ### function to call when $DEBUG is set
|
||||
set -xv
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# f() FOR PRINTING PROMPTS
|
||||
#############################
|
||||
print_usage(){
|
||||
cat << HERE
|
||||
$(tput bold; tput setaf 3)${0##*/}$(tput sgr0)
|
||||
@@ -46,12 +42,48 @@ $(tput bold)OPTIONS$(tput sgr0)
|
||||
-r clone a directory and its contents
|
||||
HERE
|
||||
}
|
||||
|
||||
print_version(){
|
||||
cat << HERE
|
||||
$(tput bold)${0##*/}: v${version}$(tput sgr0)
|
||||
by https://github.com/kevinnls
|
||||
HERE
|
||||
}
|
||||
|
||||
print_err(){
|
||||
tput setaf 1
|
||||
case $1 in
|
||||
bo)
|
||||
echo "ERROR: modes backup and old cannot co-exist"
|
||||
;;
|
||||
01)
|
||||
echo "ERROR: lightweight copy modes cannot co-exist"
|
||||
;;
|
||||
x)
|
||||
echo "ERROR: unrecognised flags"
|
||||
tput sgr0
|
||||
print_usage
|
||||
;;
|
||||
*)
|
||||
echo "UNKNOWN ERROR"
|
||||
esac
|
||||
tput sgr0
|
||||
exit 1
|
||||
}
|
||||
|
||||
copy_succ(){
|
||||
echo " ✅ $(tput setaf 2; tput bold)${1}.${2:-bak}$(tput sgr0)"
|
||||
}
|
||||
copy_fail(){
|
||||
echo " ❌ $(tput setaf 1; tput bold)${1}$(tput sgr0)"
|
||||
}
|
||||
copy_nofile(){
|
||||
echo " ❌ $(tput setaf 1; tput bold)${1}$(tput sgr0) : file does not exist"
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# f() FOR SETTING OPTIONS
|
||||
############################
|
||||
set_dest(){
|
||||
if [[ -n "$1" ]] && ! realpath -e "$1" >/dev/null 2>&1 ;then
|
||||
echo $1 does not exist
|
||||
@@ -65,26 +97,31 @@ set_mode(){
|
||||
if [[ -z $mode ]]; then
|
||||
mode=$1
|
||||
else
|
||||
print_error 'bo'
|
||||
print_err 'bo'
|
||||
fi
|
||||
}
|
||||
set_weight(){
|
||||
if [[ -z "$weight" ]]; then
|
||||
weight="$1"
|
||||
else
|
||||
print_error '01'
|
||||
print_err '01'
|
||||
fi
|
||||
}
|
||||
add_file(){
|
||||
files=("${files[@]}" "$1")
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# f() FOR PARSING PARAMS AND OPTIONS
|
||||
#######################################
|
||||
|
||||
in_combi(){
|
||||
[[ "$combi" =~ [:alnum:]*($1){1}[:alnum:]* ]]
|
||||
}
|
||||
un_combi(){
|
||||
combi=${combi/$1/}
|
||||
}
|
||||
|
||||
parse_combined(){
|
||||
|
||||
combi="${1/-/}"
|
||||
@@ -107,52 +144,24 @@ parse_combined(){
|
||||
fi
|
||||
|
||||
[[ "$combi" =~ [:alnum:]*(d){1} ]] && set_dest $2 && ret=2
|
||||
# setting return value to 2 to shift 2 in caller
|
||||
# in case of -d <dest>
|
||||
|
||||
if [[ -z "$combi" ]]; then
|
||||
return ${ret:-1}
|
||||
else
|
||||
print_error 'x'
|
||||
print_err 'x'
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
print_error(){
|
||||
tput setaf 1
|
||||
case $1 in
|
||||
bo)
|
||||
echo "ERROR: modes backup and old cannot co-exist"
|
||||
;;
|
||||
01)
|
||||
echo "ERROR: lightweight copy modes cannot co-exist"
|
||||
;;
|
||||
x)
|
||||
echo "ERROR: unrecognised flags"
|
||||
tput sgr0
|
||||
print_usage
|
||||
;;
|
||||
*)
|
||||
echo "UNKNOWN ERROR"
|
||||
esac
|
||||
tput sgr0
|
||||
exit 1
|
||||
}
|
||||
copy_succ(){
|
||||
echo " ✅ $(tput setaf 2; tput bold)$file.${mode:-bak}$(tput sgr0)"
|
||||
}
|
||||
copy_fail(){
|
||||
echo " ❌ $(tput setaf 1; tput bold)$file$(tput sgr0)"
|
||||
}
|
||||
copy_nofile(){
|
||||
echo " ❌ $(tput setaf 1; tput bold)$file$(tput sgr0) : file does not exist"
|
||||
}
|
||||
|
||||
### enable debugging ###
|
||||
${DEBUG+debug} ###
|
||||
### if $DEBUG is set ###
|
||||
|
||||
###############################################################################
|
||||
# SANITISING PARAMS AND OPTIONS
|
||||
################################
|
||||
##################################
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
'-h' )
|
||||
@@ -199,22 +208,25 @@ while [[ $# -gt 0 ]]; do
|
||||
done
|
||||
|
||||
|
||||
################################################################################
|
||||
###############################################################################
|
||||
# THE ACTUAL COPYING CODE
|
||||
##########################
|
||||
############################
|
||||
for file in "${files[@]}"; do
|
||||
if realpath -e "$file" >/dev/null 2>&1 ;then
|
||||
|
||||
src_filepath="$(realpath "$file")"
|
||||
dst_filepath="${dest:-${src_filepath%/*}}"
|
||||
dst_filename="${src_filepath##*/}.${mode:-bak}"
|
||||
cp_weight="--reflink=${weight:-auto}"
|
||||
|
||||
cp ${recurse} ${cp_weight} "$src_filepath" "$dst_filepath/$dst_filename"
|
||||
[[ $? -eq 0 ]] && copy_succ || copy_fail
|
||||
[[ $? -eq 0 ]] && copy_succ $file $mode || copy_fail $file
|
||||
|
||||
else
|
||||
copy_nofile
|
||||
copy_nofile $file
|
||||
fi
|
||||
done
|
||||
|
||||
### reset IFS
|
||||
IFS="$OLDIFS"
|
||||
|
||||
### reset IFS###
|
||||
IFS="$OLDIFS" #
|
||||
################
|
||||
|
||||
Reference in New Issue
Block a user