forked from MinchinWeb/wm_todo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·92 lines (77 loc) · 2.39 KB
/
install.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
#!/bin/bash
INSTALL_DIR=$HOME/bin
ALIAS_FILE=$HOME/.bashrc
prog="[""$(basename $0)""] "
Usage(){
echo "Usage: $(basename $0) [options]"
echo
echo "Options:"
echo -e " -h, --help\tdisplays this message and exits"
echo -e " --install-dir=/path/to/dir [Default: $INSTALL_DIR]
Uses path provided as home directory for todo.py"
echo -e " --alias-file=/path/to/file [Default: $ALIAS_FILE]
Uses file to store alias for \$INSTALL_DIR/todo.py"
}
Update(){
if ! [[ -z "$(diff $INSTALL_DIR/todo.py todo.py)" ]] && \
[[ todo.py -nt $INSTALL_DIR/todo.py ]]; then
cp -u todo.py $INSTALL_DIR/todo.py
echo $prog"An old version of todo.py was found and updated."
else
echo $prog"todo.py is already installed. Exiting..."
fi
exit
}
die(){
Usage
exit
}
set -- $(getopt -l "help,install-dir:,alias-file:" "h" "$@")
[[ $? -gt 0 ]] && die # If you fail to give me parameters
# I'll fail to install it for you. Simple as that.
while [[ $# -gt 0 ]] ; do
case "$1" in
"-h" | "--help" ) die
;;
"--install-dir" ) shift
[[ ! -z "$1" ]] && INSTALL_DIR=${1:1:${#1}-2} || die
;;
"--alias-file" ) shift
[[ ! -z "$1" ]] && ALIAS_FILE=${1:1:${#1}-2} || die
#[1]
;;
"--" ) break
;;
* ) shift
esac
done
[[ -d $INSTALL_DIR ]] || mkdir -p $INSTALL_DIR
echo $prog"$INSTALL_DIR exists."
[[ -s $INSTALL_DIR/todo.py ]] && Update
[[ -s $ALIAS_FILE ]] || echo "# Bash RC File" >> $ALIAS_FILE
echo $prog"$ALIAS_FILE exists."
## Believe it or not, >> is faster than >.
echo $prog"Copying todo.py to $INSTALL_DIR/todo.py"
if [[ ! -f $INSTALL_DIR/todo.py ]] ; then
cp ./todo.py $INSTALL_DIR
else
cp -u ./todo.py $INSTALL_DIR
fi
## Establish alias
pre="\n\n#Alias for todo.py\n"
if grep -q -e "todo.sh" -e "todo.py" "$ALIAS_FILE" ; then
alias="tpy"
else
alias="t"
fi
alias_rc=$pre"alias "$alias"='$INSTALL_DIR/todo.py'\n"
echo -e $alias_rc >> $ALIAS_FILE
echo $prog"Alias '$alias' added to $ALIAS_FILE."
echo $prog"To use alias, please run \`source $ALIAS_FILE\`."
echo $prog"You can also add '$INSTALL_DIR' to your PATH variable."
echo $prog"Installation complete."
### Footnote(s)
#[1] the argument is actually passed as "'/path/to/file'" so that instead of
# - checking /path/to/file or using that, it uses '/path/to/file' (with the
# - apostrophes). So to remove then you need to start at index 1 take a
# - substring up until length - 2