-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.sh
More file actions
executable file
·117 lines (95 loc) · 2.89 KB
/
version.sh
File metadata and controls
executable file
·117 lines (95 loc) · 2.89 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
#!/bin/bash
#
# Time-stamp: <Saturday 2025-09-13 14:23:29 Jess Moore>
#
IFS=,
declare -a TYPES
TYPES=('pdf' 'docx' 'xlsx')
joined_types="${TYPES[*]}"
function usage() {
echo "Usage: version.sh [file] [type]"
echo ""
echo "Description: Copies the current output pdf, appending version"
echo "number, and writing to current_tagged_versions folder, and moves"
echo "earlier versions in the current folder to the earlier_versions"
echo "folder."
echo ""
echo "Arguments:"
echo " file: Filename to version excluding suffixes (Default: none)."
echo " type: File type to version (Options: ${joined_types}, default: pdf)."
echo ""
exit 1 # Exit with a non-zero status to indicate an error
}
if [[ $# -eq 0 || $* == *"help"* || $* == *"-h"* ]]; then
usage
fi
FILE=$1
# Default type
TYPE='pdf'
if [[ $# -eq 2 ]]; then
TYPE=$2
fi
# Check valid column provided
case "${TYPE}" in
pdf|docx|xlsx)
echo "Valid file type provided.";;
*)
echo "Supported types: ${TYPES[*]}.";
usage;;
esac;
FILE=$(basename "$FILE")
echo "Version tagging file: ${FILE}.${TYPE}"
# Setup directories
ARCHIVE_DIR="earlier_versions"
CURRENT_DIR="current_tagged_versions"
if [[ ! -d "$ARCHIVE_DIR" ]]; then
mkdir -p "$ARCHIVE_DIR"
echo "Created ${ARCHIVE_DIR}."
fi
if [[ ! -d "$CURRENT_DIR" ]]; then
mkdir -p "${CURRENT_DIR}"
echo "Created ${CURRENT_DIR}."
NEW_VERSION="v1"
fi
# Increment old version number to make new version number
NCURR_FILES=$(find ${CURRENT_DIR}/. -iname "${FILE}*.${TYPE}" 2>/dev/null | wc -l)
if [[ $NCURR_FILES -eq 0 ]]; then
NEW_VERSION="v1"
OLD_VERSION="nil"
echo "No current files found."
else
EXISTING_VERSION_NUM=$(find ${CURRENT_DIR}/. -iname "${FILE}*.${TYPE}" | grep -oE '[0-9]+')
# echo "Before: Found files version numbers in current folder:"
# echo "${EXISTING_VERSION_NUM}"
IFS=$'\n'
OLD_VERSION=$(echo "${EXISTING_VERSION_NUM[*]}" | sort -nr | head -n1)
NEW_VERSION=$((OLD_VERSION + 1))
OLD_VERSION="v${OLD_VERSION}"
NEW_VERSION="v${NEW_VERSION}"
# Earlier version file name
OLD_FILE="${FILE}_${OLD_VERSION}.${TYPE}"
echo ""
echo "Before:"
find ${CURRENT_DIR}/. -iname "${FILE}*.${TYPE}"
fi
# Version tagged file name for current file
NEW_FILE="${FILE}_${NEW_VERSION}.${TYPE}"
# Copy to current folder
cp -p "${FILE}.${TYPE}" "${CURRENT_DIR}/${NEW_FILE}"
# Move older version to earlier versions folder
if [[ $NCURR_FILES -gt 0 ]]; then
mv "${CURRENT_DIR}/${OLD_FILE}" "${ARCHIVE_DIR}/${OLD_FILE}"
fi
# Show results
echo ""
echo "After:"
# Matching files in current dir
find ${CURRENT_DIR}/. -iname "${FILE}*.${TYPE}" | while IFS='' read -r curr_line
do
ls -lt "$curr_line"
done
# Matching files in archive dir
find ${ARCHIVE_DIR}/. -iname "${FILE}*.${TYPE}" | while IFS='' read -r archive_line
do
ls -lt "$archive_line"
done