license headers
[log-quiet] / setup
1 #!/bin/bash
2 # Copyright (C) 2017 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -eE -o pipefail
17 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
18
19 [[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
20
21 usage() {
22 cat <<EOF
23 Usage: ${0##*/} []
24 Install or uninstall files to /usr/local/bin
25
26 -n|--dry-run Dry run
27 -u|--uninstall Uninstall. default is to install
28 -h|--help Print help and exit.
29
30 Note: Uses GNU getopt options parsing style
31 EOF
32 exit $1
33 }
34 dry=false
35 uninstall=false # default
36 temp=$(getopt -l help,uninstall,dry-run hun "$@") || usage 1
37 eval set -- "$temp"
38 while true; do
39 case $1 in
40 -n|--dry-run) dry=true; shift ;;
41 -u|--uninstall) uninstall=true; shift ;;
42 -h|--help) usage ;;
43 --) shift; break ;;
44 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
45 esac
46 done
47
48
49 x="$(readlink -f "$BASH_SOURCE")"; cd ${x%/*} # directory of this file
50
51 files=()
52 for f in *; do
53 if [[ -x $f && ! -d $f && ! -L $f && $f != setup ]]; then
54 files+=($f)
55 fi
56 done
57
58 if $uninstall; then
59 if $dry; then
60 echo "setup dry run: cd /usr/local/bin"
61 echo "setup dry run: rm -fv ${files[*]}"
62 else
63 cd /usr/local/bin
64 rm -fv ${files[@]}
65 fi
66 else
67 if $dry; then
68 echo "setup dry run: install -v ${files[*]} /usr/local/bin"
69 else
70 install -v ${files[@]} /usr/local/bin
71 fi
72 fi