-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaddyify
executable file
·140 lines (117 loc) · 5.21 KB
/
kaddyify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bash
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/kaddyify
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/kaddyify
## https://davoudarsalani.ir
source "$HOME"/main/scripts/gb
source "$HOME"/main/scripts/gb-color
## --archive, -a archive mode is -rlptgoD (no -A,-X,-U,-N,-H)
## --recursive, -r recurse into directories
## --progress show progress during transfer
## -P same as --partial --progress
## --delete remove files on remote not present on local
## --dry-run -v, -n -v perform a trial run with no changes made (-v is added by me)
## --itemize-changes, -i output a change-summary for all updates
title="${0##*/}"
function display_help {
source "$HOME"/main/scripts/.help
kaddyify_help
}
function get_opt {
local options
options="$(getopt --longoptions 'help,sync,diff,directory:' --options 'hsfd:' --alternative -- "$@")"
eval set -- "$options"
while true; do
case "$1" in
-h|--help )
display_help ;;
-s|--sync )
flags='--archive --progress --delete' ;;
-f|--diff )
## https://unix.stackexchange.com/questions/57305/rsync-compare-directories
## -v not needed here
flags='--archive --itemize-changes --dry-run' ;; ## --archive OR --recursive
-d|--directory )
shift
selected_directory="$1"
reg='^\.$'
[[ "$selected_directory" =~ $reg ]] && selected_directory="$PWD"
## NOTE JUMP_1 remove trailing slash
## important because we want the directory itself
## to be synced/copeid to destination
## rather than its content
selected_directory="${selected_directory%/}"
dest_kaddy_dir="$home_kaddy_dir"/"${selected_directory##*/}" ## ~/kaddy/scripts
[ -d "$selected_directory" ] || {
red "${selected_directory/$HOME/\~} does not exist"
exit
}
[ -d "$dest_kaddy_dir" ] || {
red "${dest_kaddy_dir/$HOME/\~} does not exist"
exit
}
sync_prompt="$(get_single_input "sync ${selected_directory/$HOME/\~} to ${home_kaddy_dir/$HOME/\~}?")" && printf '\n'
[ "$sync_prompt" == 'y' ] || exit
;;
-- )
break ;;
esac
shift
done
}
home_kaddy_dir="$HOME"/kaddy
get_opt "$@"
heading "$title"
[ ! "$1" ] || [ ! "$flags" ] && display_help
if [ "$selected_directory" ]; then
## NOTE JUMP_2 keep $flags unquoted
rsync $flags "$selected_directory" "$home_kaddy_dir" | \grep -v 'sending incremental file list'
else
readarray -t kaddy_directories < <(find "$home_kaddy_dir" -mindepth 1 -maxdepth 1 -type d | sort) ## ! -path '*lost+found*'
for kaddy_dir in "${kaddy_directories[@]}"; {
base="${kaddy_dir##*/}"
## NOTE JUMP_1 remove trailing slash
## important because we want the directory itself
## to be synced/copeid to destination
## rather than its content
base="${base%/}"
home_dir="$HOME"/main/"$base"
blue "${home_dir/$HOME/\~}"
## NOTE JUMP_2 keep $flags unquoted
rsync $flags "$home_dir" "$home_kaddy_dir" | \grep -v 'sending incremental file list'
}
fi
exit 0
## Explanation of each bit position and value in rsync's output:
## (https://stackoverflow.com/questions/4493525/what-does-f-mean-in-rsync-logs)
##
## YXcstpoguax path/to/file
## |||||||||||
## ||||||||||╰- x: The extended attribute information changed
## |||||||||╰-- a: The ACL information changed
## ||||||||╰--- u: The u slot is reserved for future use
## |||||||╰---- g: Group is different
## ||||||╰----- o: Owner is different
## |||||╰------ p: Permission are different
## ||||╰------- t: Modification time is different
## |||╰-------- s: Size is different
## ||╰--------- c: Different checksum (for regular files), or changed value (for symlinks, devices, and special files)
## ||
## |╰---------- file type:
## | f: for a file,
## | d: for a directory,
## | L: for a symlink,
## | D: for a device,
## | S: for a special file (e.g. named sockets and fifos)
## |
## ╰----------- type of update being done:
## <: file is being transferred to the remote host (sent)
## >: file is being transferred to the local host (received)
## c: local change/creation for the item, such as:
## - the creation of a directory
## - the changing of a symlink,
## - etc.
## h: the item is a hard link to another item (requires --hard-links).
## .: the item is not being updated (though it might have attributes that are being modified)
## *: means that the rest of the itemized-output area contains a message (e.g. "deleting")