-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINSTALL
executable file
·138 lines (116 loc) · 2.74 KB
/
INSTALL
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
134
135
#!/bin/bash
#
# FILE: INSTALL
#
# ABSTRACT: Install files to $HOME and $HOME/bin
#
# AUTHOR: Ralf Schandl
#
script_dir="$(cd "$(dirname "$0")" && pwd)"
script_name="$(basename "$0")"
cd "$script_dir" || exit 1
ts=$(date "+%Y-%m-%dT%H.%M.%S")
bin_target_dir="$HOME/bin"
completion_target_dir="$HOME/.local/share/bash-completion/completions"
execute()
{
if [ "$execute" == "true" ]; then
"$@"
else
echo "$@"
fi
}
do_install()
{
local fqfn="$1"
local tgt="$2"
if [ -e "$tgt" ]; then
if [ "$mode" = "symlink" ] && [ -L "$tgt" ]; then
tgtln="$(realpath "$tgt")"
if [ "$fqfn" = "$tgtln" ]; then
echo "Up-to-date: $tgt"
return 0
fi
fi
case "$exit_handling" in
backup)
# create backup copy
echo "Backing up existing file: $tgt"
execute mv "$tgt" "$tgt.$ts"
;;
overwrite)
# do nothing
;;
*)
echo >&2 "ERROR: File exists: $tgt"
echo >&2 " Use either '-b' or '-f'."
echo >&2 ""
usage
;;
esac
fi
if [ "$mode" == "copy" ]; then
echo "Copying $fqfn -> $tgt"
execute cp -f "$fqfn" "$tgt"
else
echo "Linking $tgt -> $fqfn"
execute ln -fs "$fqfn" "$tgt"
fi
}
install_file()
{
typeset fn="$1"
typeset tgt_dir="$2"
# shellcheck disable=SC2155
typeset fqfn="$(realpath "$fn")"
typeset tgt="$tgt_dir/$fn"
do_install "$fqfn" "$tgt"
}
usage()
{
echo >&2 "Usage: $script_name [-nfb] <copy|symlink>"
echo >&2 " -n Just print what would be done."
echo >&2 " -f force - overwrite existing files"
echo >&2 " -b backup - backup existing files (with time stamp)"
exit 1
}
#---------[ MAIN ]-------------------------------------------------------------
execute="true"
exit_handling=""
while getopts "nfb" o "$@"; do
case $o in
n) execute="false"
;;
f) exit_handling="overwrite"
;;
b) exit_handling="backup"
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ $# -ne 1 ]; then
echo >&2 "ERROR: one argument needed"
usage
fi
case "$1" in
copy) mode="copy"
;;
symlink) mode="symlink"
;;
*) echo >&2 "Unknown mode: $1"
usage
;;
esac
do_install "$script_dir/_maven-completion.bash" "$completion_target_dir/mvn"
if [ ! -d "$bin_target_dir" ]; then
execute mkdir -p "$bin_target_dir"
fi
cd bin || exit 1
for fn in [!_]*[!~]; do
if [ -f "$fn" ]; then
install_file "$fn" "$bin_target_dir"
fi
done