2018-11-22 03:37:47 -06:00
|
|
|
#! /usr/bin/env bash
|
2018-10-19 21:01:30 -05:00
|
|
|
#
|
|
|
|
# Salt's bootstrap script
|
|
|
|
# Copyrithgt (C) 2018 salt <salt@lap-th-e560-0>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license
|
|
|
|
#
|
2018-09-16 12:13:54 -05:00
|
|
|
|
2018-11-22 03:37:47 -06:00
|
|
|
# shellcheck disable=1117
|
|
|
|
|
2018-10-19 21:01:30 -05:00
|
|
|
## Helper functions
|
2018-09-16 12:13:54 -05:00
|
|
|
log() {
|
|
|
|
[ -z ${1+x} ] && return 1
|
2018-10-19 21:05:08 -05:00
|
|
|
out=1
|
|
|
|
[ -z ${2+x} ] || out="$2"
|
|
|
|
col_message="\e[39m"
|
|
|
|
[ "$out" -gt "1" ] && col_message="\e[31m"
|
|
|
|
[ "$out" -lt "1" ] && out=1 && col_message="\e[37m"
|
2018-11-22 03:37:47 -06:00
|
|
|
printf "\e[94m%s\e[0m: %b%s\e[0m\n" \
|
|
|
|
"$name" \
|
|
|
|
"$col_message" \
|
|
|
|
"$1" >&${out}
|
2018-09-16 12:13:54 -05:00
|
|
|
}
|
|
|
|
validatedep() {
|
2018-11-22 03:37:47 -06:00
|
|
|
if ! command -v "$1" > /dev/null 2>&1; then
|
2018-09-16 12:13:54 -05:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
2018-10-19 21:01:30 -05:00
|
|
|
help() {
|
|
|
|
cat << EOF
|
|
|
|
Usage: $name [OPTION]
|
|
|
|
Bootstrap a git repository as a bare repo, also perform some bootstrap tasks.
|
2018-12-25 21:45:06 -06:00
|
|
|
-d DIRECTORY Bootstrap directory. Defaults to ~.
|
|
|
|
-r REPOSITORY Target repository. Defaults to
|
2019-06-07 01:11:12 -05:00
|
|
|
git@gitlab.com:rehashedsalt/home
|
2018-12-25 21:45:06 -06:00
|
|
|
-l FILE File to log command output to. Defaults to
|
2018-12-25 21:47:53 -06:00
|
|
|
$name.log
|
2018-10-19 21:01:30 -05:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
cleanup() {
|
|
|
|
[ -z ${tmpdir+x} ] || rm -rf -- "$tmpdir"
|
|
|
|
}
|
|
|
|
|
|
|
|
## Core functions
|
|
|
|
step_validate_logs() {
|
|
|
|
# Rotate the logfile, if necessary
|
|
|
|
if [ -f "$logfile" ]; then
|
2018-10-19 21:08:59 -05:00
|
|
|
log "Found old logfile. Rotating" 0
|
2018-10-19 21:01:30 -05:00
|
|
|
mv "$logfile" "$logfile.old"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
step_validate_deps() {
|
|
|
|
# Ensure we have the proper dependencies
|
|
|
|
log "Validating dependencies"
|
2019-06-06 16:59:38 -05:00
|
|
|
local nodeps=0
|
2018-10-19 21:01:30 -05:00
|
|
|
for dep in $deps; do
|
|
|
|
if ! validatedep "$dep"; then
|
2018-10-19 21:05:08 -05:00
|
|
|
log "Could not find critical dependency \"$dep\"" 2
|
2019-06-06 16:59:38 -05:00
|
|
|
nodeps=1
|
2018-10-19 21:01:30 -05:00
|
|
|
fi
|
|
|
|
done
|
2019-06-06 16:59:38 -05:00
|
|
|
if [ "$nodeps" == "1" ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
2018-10-19 21:01:30 -05:00
|
|
|
}
|
|
|
|
step_make_skeleton() {
|
|
|
|
# Build Home folder skeleton
|
|
|
|
log "Building skeleton folder layout"
|
2019-09-14 04:17:06 -05:00
|
|
|
pushd "$bootstrapdir"
|
2018-10-19 21:01:30 -05:00
|
|
|
if ! mkdir Desktop Documents Downloads Games Music Pictures Projects Public ROMs System Templates Videos > /dev/null 2>&1; then
|
2018-10-19 21:05:08 -05:00
|
|
|
log "Failed to build skeleton layout" 2
|
2018-10-19 21:01:30 -05:00
|
|
|
return 1
|
|
|
|
fi
|
2019-09-14 04:17:06 -05:00
|
|
|
popd
|
2018-10-19 21:01:30 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
step_repo_clone() {
|
|
|
|
# Clone the dotfiles
|
|
|
|
log "Cloning repository into \"$tmpdir\""
|
2018-10-19 21:10:37 -05:00
|
|
|
git clone --recursive --depth 50 --separate-git-dir="$gitdir" "$repo" "$tmpdir" >> "$logfile" 2>&1
|
2018-10-19 21:01:30 -05:00
|
|
|
error="$?"
|
|
|
|
if ! [ "$error" -eq "0" ]; then
|
2018-10-19 21:05:08 -05:00
|
|
|
log "Failed cloning repository \"$repo\": git returned $error" 2
|
2018-10-19 21:01:30 -05:00
|
|
|
log "See $logfile for details"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
step_repo_move() {
|
|
|
|
# Move them to where they should be
|
|
|
|
log "Moving cloned repo from \"$tmpdir\" into \"$bootstrapdir\""
|
|
|
|
if ! rsync -rvl --exclude ".git" "$tmpdir/" "$bootstrapdir/" >> "$logfile"; then
|
2018-10-19 21:05:08 -05:00
|
|
|
log "Failed to move from temp directory to bootstrap directory" 2
|
2018-10-19 21:01:30 -05:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
## Main
|
|
|
|
main() {
|
|
|
|
# Set up some non-controlled variables
|
|
|
|
name="$(basename "$0" .sh)" # Name of the program, used in logging
|
|
|
|
deps="git rsync mktemp basename" # Critical dependencies
|
|
|
|
tmpdir="$(mktemp -d "tmp.$USER.$name.XXXXXXXX" --tmpdir)"
|
|
|
|
gitdir="$PWD/.dotfiles"
|
2018-09-16 12:13:54 -05:00
|
|
|
|
2018-10-19 21:01:30 -05:00
|
|
|
# Parse out arguments
|
2018-11-22 03:37:47 -06:00
|
|
|
while getopts ":d:r:lh\?" opt; do
|
2018-10-19 21:01:30 -05:00
|
|
|
case $opt in
|
|
|
|
d)
|
|
|
|
if [ "$OPTARG" == "" ]; then
|
2018-10-19 21:05:08 -05:00
|
|
|
log "Option -d requires an argument" 2
|
2018-10-19 21:01:30 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
bootstrapdir="$OPTARG"
|
|
|
|
;;
|
|
|
|
r)
|
|
|
|
if [ "$OPTARG" == "" ]; then
|
2018-10-19 21:05:08 -05:00
|
|
|
log "Option -r requires an argument" 2
|
2018-10-19 21:01:30 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
repo="$OPTARG"
|
|
|
|
;;
|
|
|
|
l)
|
|
|
|
if [ "$OPTARG" == "" ]; then
|
2018-10-19 21:05:08 -05:00
|
|
|
log "Option -l requires an argument" 2
|
2018-10-19 21:01:30 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
logfile="$OPTARG"
|
|
|
|
;;
|
|
|
|
\?|h)
|
|
|
|
help
|
|
|
|
;;
|
|
|
|
*)
|
2018-10-19 21:05:08 -05:00
|
|
|
log "Invalid option: \"$opt\"" 2
|
2018-10-19 21:01:30 -05:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
# Set up some user-controllable variables
|
|
|
|
if [ -z ${bootstrapdir+x} ]; then
|
|
|
|
bootstrapdir="$PWD"
|
2018-10-19 21:08:30 -05:00
|
|
|
log "Using default bootstrap dir \"$bootstrapdir\"" 0
|
2018-10-19 21:01:30 -05:00
|
|
|
fi
|
|
|
|
if [ -z ${repo+x} ]; then
|
2018-12-15 20:35:57 -06:00
|
|
|
repo='git@gitlab.com:rehashedsalt/home.git'
|
|
|
|
log "Using default repository \"$repo\"" 0
|
2018-10-19 21:01:30 -05:00
|
|
|
fi
|
|
|
|
if [ -z ${logfile+x} ]; then
|
|
|
|
logfile="${PWD}/${name}.log" # Log file
|
2018-08-24 17:30:41 -05:00
|
|
|
fi
|
|
|
|
|
2018-10-19 21:01:30 -05:00
|
|
|
# Do the do
|
|
|
|
trap cleanup EXIT
|
|
|
|
step_validate_logs || exit $?
|
|
|
|
step_validate_deps || exit $?
|
2018-12-15 20:36:55 -06:00
|
|
|
step_make_skeleton
|
2018-10-19 21:01:30 -05:00
|
|
|
step_repo_clone || exit $?
|
|
|
|
step_repo_move || exit $?
|
|
|
|
log "Dotfiles set up successfully"
|
|
|
|
exit 0
|
|
|
|
}
|
2018-08-24 17:30:41 -05:00
|
|
|
|
2018-11-22 03:37:47 -06:00
|
|
|
main "$@"
|
2018-09-16 12:13:54 -05:00
|
|
|
|