1 #!/usr/bin/env bash
2
3 set -euo pipefail
4
5 # =============================================================================
6 # Define helper functions
7 # =============================================================================
8
9 text_bold() {
10 echo -e "\033[1m$1\033[0m"
11 }
12 text_title() {
13 echo ""
14 text_bold "$1"
15 if [ "$2" != "" ]; then echo "$2"; fi
16 }
17 text_title_error() {
18 echo ""
19 echo -e "\033[1;31m$1\033[00m"
20 }
21
22 # =============================================================================
23 # Define base variables
24 # =============================================================================
25
26 NAME="smbcloud-cli"
27 VERSION="0.3.26"
28 GITHUB_REPO="https://github.com/smbcloudXYZ/smbcloud-cli"
29 DOWNLOAD_BASE_URL="https://github.com/$GITHUB_REPO/releases/download/$VERSION"
30
31 if [ "$VERSION" == "latest" ]; then
32 # The latest version is accessible from a slightly different URL
33 DOWNLOAD_BASE_URL="https://github.com/$GITHUB_REPO/releases/latest/download"
34 fi
35
36 # =============================================================================
37 # Define binary list for supported OS & Arch
38 # - this is a map of "OS:Arch" -> "download binary name"
39 # - you can remove or add to this list as needed
40 # =============================================================================
41
42 declare -A BINARIES=(
43 ["Linux:x86_64"]="smb"
44 ["Darwin:x86_64"]="smb"
45 ["Darwin:arm64"]="smb"
46 )
47
48 # =============================================================================
49 # Get the user's OS and Arch
50 # =============================================================================
51
52 OS="$(uname -s)"
53 ARCH="$(uname -m)"
54 SYSTEM="${OS}:${ARCH}"
55
56 # =============================================================================
57 # Match a binary to check if the system is supported
58 # =============================================================================
59
60 if [[ ! ${BINARIES["$SYSTEM"]+_} ]]; then
61 text_title_error "Error"
62 echo " Unsupported OS or arch: ${SYSTEM}"
63 echo ""
64 exit 1
65 fi
66
67 # =============================================================================
68 # Set the default installation variables
69 # =============================================================================
70
71 INSTALL_DIR="/usr/local/bin"
72 BINARY="${BINARIES["$SYSTEM"]}"
73 DOWNLOAD_URL="$DOWNLOAD_BASE_URL/$BINARY"
74
75 # =============================================================================
76 # Handle script arguments if passed
77 # -u: install to user bin directory
78 # -d <path>: specify installation directory
79 # =============================================================================
80
81 if [ $# -gt 0 ]; then
82 while getopts ":ud:" opt; do
83 case $opt in
84 u)
85 # Set default install dir based on OS
86 [ "$OS" == "Darwin" ] && INSTALL_DIR="$HOME/bin" || INSTALL_DIR="$HOME/.local/bin"
87
88 # Check that the user bin directory is in their PATH
89 IFS=':' read -ra PATHS <<< "$PATH"
90 INSTALL_DIR_IN_PATH="false"
91 for P in "${PATHS[@]}"; do
92 if [[ "$P" == "$INSTALL_DIR" ]]; then
93 INSTALL_DIR_IN_PATH="true"
94 fi
95 done
96
97 # If user bin directory doesn't exist or not in PATH, exit
98 if [ ! -d "$INSTALL_DIR" ] || [ "$INSTALL_DIR_IN_PATH" == "false" ]; then
99 text_title_error "Error"
100 echo " The user bin directory '$INSTALL_DIR' does not exist or is not in your environment PATH variable"
101 echo " To fix this error:"
102 echo " - Omit the '-u' option and to install system-wide"
103 echo " - Specify an installation directory with -d <path>"
104 echo ""
105 exit 1
106 fi
107
108 ;;
109 d)
110 # Get absolute path in case a relative path is provided
111 INSTALL_DIR=$(cd "$OPTARG"; pwd)
112
113 if [ ! -d "$INSTALL_DIR" ]; then
114 text_title_error "Error"
115 echo " The installation directory '$INSTALL_DIR' does not exist or is not a directory"
116 echo ""
117 exit 1
118 fi
119
120 ;;
121 \?)
122 text_title_error "Error"
123 echo " Invalid option: -$OPTARG" >&2
124 echo ""
125 exit 1
126 ;;
127 :)
128 text_title_error "Error"
129 echo " Option -$OPTARG requires an argument." >&2
130 echo ""
131 exit 1
132 ;;
133 esac
134 done
135 fi
136
137 # =============================================================================
138 # Create and change to temp directory
139 # =============================================================================
140
141 cd "$(mktemp -d)"
142
143 # =============================================================================
144 # Download binary
145 # =============================================================================
146
147 text_title "Downloading Binary" " $DOWNLOAD_URL"
148 curl -LO --proto '=https' --tlsv1.2 -sSf "$DOWNLOAD_URL"
149
150 # =============================================================================
151 # Make binary executable and move to install directory with appropriate name
152 # =============================================================================
153
154 text_title "Installing Binary" " $INSTALL_DIR/$NAME"
155 chmod +x "$BINARY"
156 mv "$BINARY" "$INSTALL_DIR/$NAME"
157
158 # =============================================================================
159 # Display post install message
160 # =============================================================================
161
162 text_title "Installation Complete" " Run $NAME --help for more information"
163 echo ""