Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Minimal Development Setup
#!/bin/sh
# shellcheck disable=SC2039
set -eu
if [ -n "${DEBUG:-}" ]; then set -x; fi
main() {
init
ensure_root
info "Starting setup"
install_system_packages
install_ripgrep
install_bashrc
install_dot_configs
info "Finished setup. Start a new shell or login again and enjoy!"
return 0
}
ensure_root() {
need_cmd id
if [ "$(id -u)" -ne 0 ]; then
exit_with "Must be run as root (maybe use \`sudo $0'?)" 1
fi
}
init() {
program="$(basename "$0")"
}
install_system_packages() {
local system
system="$(uname -s)"
case "${system}" in
Linux)
need_cmd apt-get
info "Updating package index"
apt-get update
info "Installing packages"
apt-get install -y bash git-core htop mosh tmux
;;
*)
exit_with "Unsupported system: $system" 50
;;
esac
}
install_ripgrep() {
need_cmd grep
need_cmd mkdir
need_cmd rm
need_cmd tar
local version="0.5.0"
if command -v rg > /dev/null; then
if rg --version | grep -q "${version}"; then
info "ripgrep $version installed, skipping"
return 0
fi
fi
local arch
local filename
arch="$(uname -m)"
filename="ripgrep-${version}-${arch}-unknown-linux-musl.tar.gz"
rm -fv "/tmp/${filename}"
dl_file "https://github.com/BurntSushi/ripgrep/releases/download/${version}/${filename}" \
"/tmp/${filename}"
mkdir -pv /usr/local/bin
(cd /usr/local/bin \
&& tar xf "/tmp/${filename}" "${filename%.tar.gz}/rg" --strip-components 1)
rm -f "/tmp/${filename}"
}
install_bashrc() {
need_cmd bash
need_cmd rm
if [ -f /etc/bash/bashrc.local ]; then
info "bashrc already installed, skipping"
return 0
fi
info "Installing fnichol/bashrc"
dl_file https://raw.githubusercontent.com/fnichol/bashrc/master/contrib/install-system-wide \
/tmp/install.sh
bash /tmp/install.sh
rm -f /tmp/install.sh
}
install_dot_configs() {
need_cmd cut
need_cmd git
need_cmd su
local user
local homedir
local repo
local repo_dir
user="${SUDO_USER:-${USER}}"
homedir="$(homedir_for "$user")"
if [ -z "$homedir" ]; then
exit_with "Failed to determine home dir for $user" 9
fi
if [ -f "${homedir}/.homesick/repos/homeshick/homeshick.sh" ]; then
info "homeshick already installed for $user, skipping"
else
info "Installing homeshick for $user"
su -l "$user" -c "
set -e
git clone --depth 1 git://github.com/andsens/homeshick.git \
$homedir/.homesick/repos/homeshick
"
fi
for repo in fnichol/dotfiles fnichol/dotvim; do
repo_dir="${homedir}/.homesick/repos/$(echo "$repo" | cut -d '/' -f 2)"
if [ -d "$repo_dir" ]; then
info "Repo $repo already installed for $user, skipping"
else
info "Installing repo $repo for $user"
su -l "$user" -c "
set -e
. $homedir/.homesick/repos/homeshick/homeshick.sh
homeshick --batch clone $repo
"
fi
done
info "Updating dotfile configurations links for $user"
su -l "$user" -c "
set -e
. $homedir/.homesick/repos/homeshick/homeshick.sh
homeshick --force link
"
}
info() {
echo "--> ${program}: $1"
}
warn() {
echo "xxx ${program}: $1" >&2
}
exit_with() {
warn "$1"
exit "${2:-10}"
}
need_cmd() {
if ! command -v "$1" > /dev/null 2>&1; then
exit_with "Required command '$1' not found on PATH" 127
fi
}
dl_file() {
local _url="${1}"
local _dst="${2}"
local _code
# Attempt to download with wget, if found. If successful, quick return
if command -v wget > /dev/null; then
info "Downlading via wget: ${_url}"
wget -q -O "${_dst}" "${_url}"
_code="$?"
if [ $_code -eq 0 ]; then
return 0
else
local _e="wget failed to download file, perhaps wget doesn't have"
_e="$_e SSL support and/or no CA certificates are present?"
warn "$_e"
fi
fi
# Attempt to download with curl, if found. If successful, quick return
if command -v curl > /dev/null; then
info "Downlading via curl: ${_url}"
curl -sSfL "${_url}" -o "${_dst}"
_code="$?"
if [ $_code -eq 0 ]; then
return 0
else
local _e="curl failed to download file, perhaps curl doesn't have"
_e="$_e SSL support and/or no CA certificates are present?"
warn "$_e"
fi
fi
# If we reach this point, wget and curl have failed and we're out of options
exit_with "Required: SSL-enabled 'curl' or 'wget' on PATH with" 6
}
homedir_for() {
need_cmd cut
need_cmd getent
local user="$1"
local home
home="$(getent passwd "$user" | cut -d ':' -f 6)"
if [ -z "$home" ]; then
warn "Could not find home for user $user"
return 5
else
echo "$home"
return 0
fi
}
main "$@" || exit 99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.