-
Notifications
You must be signed in to change notification settings - Fork 2
/
dl-gdocs-gdrive.sh
executable file
·67 lines (54 loc) · 1.79 KB
/
dl-gdocs-gdrive.sh
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
set -e
if [ $# -ne 4 ]; then
echo "Need 4 arguments"
exit 0
fi
PACKETS_DIR="$1" # packets/
GDOCS_FOLDER_ID="$2"
PACKET_FILENAME_TO_SLUG_START="$3"
PACKET_FILENAME_TO_SLUG_LENGTH="$4"
if [ -z "$GDOCS_FOLDER_ID" ]; then
echo "Need Google Drive folder ID."
echo "(As of 2022-09-01, folder ID is needed instead of name.)"
exit 0
fi
DATE=`date "+%F"`
DOCS_DIR="$PACKETS_DIR"docs/ # packets/docs/
TEMP_DOCS_DIR="$DOCS_DIR"tmp/ # packets/docs/tmp/
DATE_DOCS_DIR="$DOCS_DIR"$DATE/ # packets/docs/yyyy-mm-dd/
mkdir -p $TEMP_DOCS_DIR
mkdir -p $DATE_DOCS_DIR
if [ 1 ]; then
echo "clearing old docs in $PACKETS_DIR, $TEMP_DOCS_DIR, $DATE_DOCS_DIR"
rm -rf $PACKETS_DIR*.docx $TEMP_DOCS_DIR* $DATE_DOCS_DIR*.docx
echo "fetching docs into $TEMP_DOCS_DIR"
gdrive sync "'"$GDOCS_FOLDER_ID"' in parents" --path "$TEMP_DOCS_DIR"
fi
echo
echo "changing filenames and copying into $DATE_DOCS_DIR"
for DOC in $TEMP_DOCS_DIR*; do
OLDNAME=${DOC##*\/}
if [ "${OLDNAME: -5}" == ".docx" ]; then
echo "skipping \"$OLDNAME\": probably an imported Word doc, not a native Google Doc"
continue
fi
# commented out 2020-03-05
#if [ "$PACKET_FILENAME_TO_SLUG_START" -eq "0" ]; then
# NEWNAME=$OLDNAME
#else
if [ "$PACKET_FILENAME_TO_SLUG_LENGTH" -eq "0" ]; then
NEWNAME=${OLDNAME:$PACKET_FILENAME_TO_SLUG_START}
else
NEWNAME=${OLDNAME:$PACKET_FILENAME_TO_SLUG_START:$PACKET_FILENAME_TO_SLUG_LENGTH}
fi
#fi
NEWNAME=${NEWNAME// /_}
if [ ! -z "$NEWNAME" ]; then
NEWPATH=$DATE_DOCS_DIR$NEWNAME.docx
printf "copying %-42s → \"$NEWNAME\"\n" "\"$OLDNAME\""
cp "$DOC" "$NEWPATH" || true
fi
done
echo
echo "copying into $PACKETS_DIR"
cp $DATE_DOCS_DIR* $PACKETS_DIR