-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateutil.sh
executable file
·121 lines (103 loc) · 2.57 KB
/
updateutil.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
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
#!/bin/sh
# Quick script to update all executable scripts/programs from the utils
# directory. Just copies them to the $HOME/bin/ folder. This is really
# just intended to make my life easier, modify to fit your needs.
FILELIST=()
LOG="$HOME/share/utiltrack.log"
UTILDIR="$HOME/pkgs/utils/"
BINDIR="$HOME/bin/"
FNAMEREGEX='(\w*\.?\w+)$'
RED='\033[0;31m'
GREEN='\033[0,32m'
track ()
{
# check if the file exists
if [[ ! -f "$1" ]];
then
echo "File doesn't exit."
exit 1
fi
# check if file is already being tracked
for file in "${FILELIST[@]}"
do
if [[ "$1" == "$file" ]];
then
echo "File is already being tracked"
exit 2
fi
done
# append new file to the end of the log
echo "Adding $1 to update list"
echo "$1" >> "$LOG"
}
update () {
for file in "${FILELIST[@]}"
do
cp "$file" "$BINDIR"
done
printf "${GREEN} Files updated.\n"
}
isoutdated () {
if [[ ! -f "$1" ]]
then
( >&2 echo "Error: non-file passed to isoutdated()" )
exit 1
fi
homefile="$1"
repofile=""
if [[ $homefile =~ $FNAMEREGEX ]]
then
repofile="$BINDIR${BASH_REMATCH[1]}"
else
( >&2 echo "Error: couldn't parse filename in isoutdated()")
exit 1
fi
homedate=$(stat -c %Y "$homefile")
repodate=$(stat -c %Y "$repofile")
if [[ $homedate -lt $repodate ]]
then
return 1
else
return 0
fi
}
list () {
printf "Files being tracked:\n\n"
for file in "${FILELIST[@]}"
do
if [[ $file =~ $FNAMEREGEX ]]
then
output="${BASH_REMATCH[0]}"
if isoutdated $file;
then
printf "\t${RED}${output}\n"
else
printf "\t${GREEN}${output}\n"
fi
fi
done
}
help () {
printf "Usage:\n updateutil.sh [options]\n\n"
printf "Keep track of utils and their storage in a repository.\n\n"
printf "Options:\n"
printf "%s\n" " -t <file> add the file to the tracking list"
printf "%s\n" " -u update the files in the repo"
printf "%s\n\n" " -l list the files being tracked"
printf "%s\n" " -h displays this help message"
exit 0
}
# load tracked files
mapfile -t FILELIST < "$LOG"
while getopts 't:uhl' flag; do
case "${flag}" in
t) track "$OPTARG" ;;
u) update ;;
l) list ;;
h) help ;;
\?) list ;;
*) ( >&2 echo "Not a valid flag." )
help
;;
esac
done