-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_doc.sh
More file actions
executable file
·133 lines (111 loc) · 3.32 KB
/
update_doc.sh
File metadata and controls
executable file
·133 lines (111 loc) · 3.32 KB
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
#!/bin/bash
#
# Time-stamp: Sunday 2026-04-26 Jess Moore
#
# Check how to share document or update local or remote of document
function usage() {
echo "Usage: update_doc.sh [-n|-p|-o folder] [remotesubdir] [filename]"
echo ""
echo "Description: Check how to share document or update local or remote of document."
echo ""
echo "Flags (exactly one required):"
echo " -n Nextcloud folder (~/Documents/Nextcloud)"
echo " -p Private folder (~/Documents/private)"
echo " -o folder Other folder (~/Documents/<folder>)"
echo ""
echo "Arguments:"
echo " filename: Name of file (must include extension)"
echo " remotesubdir: Subdirectory within the remote folder."
echo ""
exit 1 # Exit with a non-zero status to indicate an error
}
BASE_DIR=""
FLAG_ARG=()
[[ "$1" == "--help" ]] && usage
while getopts ":hnpo:" opt; do
case $opt in
h) usage ;;
n) BASE_DIR="${HOME}/Documents/Nextcloud"; FLAG_ARG=("-n") ;;
p) BASE_DIR="${HOME}/Documents/private"; FLAG_ARG=("-p") ;;
o) BASE_DIR="${HOME}/Documents/${OPTARG}"; FLAG_ARG=("-o" "${OPTARG}") ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
if [[ -z "$BASE_DIR" || $# -ne 2 ]]; then
usage
fi
REM_SUB_DIR=$1
FILE=$2
# User
user=$(whoami)
REM_DIR="${BASE_DIR}/${REM_SUB_DIR}"
# Check remote dir exists
if [[ ! -d ${REM_DIR} ]]; then
echo "Remote directory does not exist"
exit 1
else
echo "Remote directory: ${REM_DIR}"
fi
# Check local file exists
if [[ ! -e "$FILE" ]]; then
echo "Local $FILE does not exist."
exit 1
fi
if [[ "${user}" != "u9904893" ]]; then
echo "User must be jmoore's id"
exit 1
fi
MODTIME_REM=$(stat -f %Sm -t "%Y-%m-%d %H:%M" "${REM_DIR}/$FILE")
if [[ -f "${REM_DIR}/$FILE" ]]; then
echo "Remote file $FILE: ${MODTIME_REM}"
else
echo "Remote file $FILE does not yet exist"
fi
MODTIME_LOC=$(stat -f %Sm -t "%Y-%m-%d %H:%M" "$FILE")
# Extra white space for aligned modtimes
echo "Local file $FILE: ${MODTIME_LOC}"
ACTION=""
if [[ ${MODTIME_REM} > ${MODTIME_LOC} ]]; then
echo "Remote file is more recent -> pull FROM remote"
ACTION="pull"
elif [[ ${MODTIME_REM} == "${MODTIME_LOC}" ]]; then
echo "Files are up to date (same)"
else
echo "Local file is more recent -> push TO remote"
ACTION="push"
fi
BASEFILE=$(basename "${FILE}")
echo "Remote files:"
FILES_REM=$(find "${REM_DIR}" -name "${BASEFILE}*")
for f in $FILES_REM
do
ls -lt "$f"
done
echo "Local files:"
FILES_LOC=$(find . -name "${BASEFILE}*" -depth)
for f in $FILES_LOC
do
ls -lt "$f"
done
SCRIPT_DIR="$(dirname "$0")"
if [[ "$ACTION" == "pull" ]]; then
while true; do
read -r -p "Pull ${FILE} from remote now? [y/n]: " yn
case $yn in
[Yy]* ) bash "${SCRIPT_DIR}/my_pull" "${FLAG_ARG[@]}" "${REM_SUB_DIR}" "${FILE}"; break;;
[Nn]* ) echo "Skipping."; break;;
* ) echo "Please answer y or n.";;
esac
done
elif [[ "$ACTION" == "push" ]]; then
while true; do
read -r -p "Push ${FILE} to remote now? [y/n]: " yn
case $yn in
[Yy]* ) bash "${SCRIPT_DIR}/my_push" "${FLAG_ARG[@]}" "${REM_SUB_DIR}" "${FILE}"; break;;
[Nn]* ) echo "Skipping."; break;;
* ) echo "Please answer y or n.";;
esac
done
fi
echo "Done"