dev/runs/dotfiles

118 lines
3 KiB
Bash
Executable file

#!/usr/bin/env bash
# NAME: Setup dotfiles and configuration
# REQUIRES: interactive
set -euo pipefail
# Source common functions
source "$(dirname "$0")/../common.sh" || {
echo "[ERROR] Could not source common.sh" >&2
exit 1
}
readonly DOTFILES_REPO="https://git.mz.uy/marianozunino/dotfiles.git"
readonly DOTFILES_DIR="$HOME/dotfiles"
check_requirements() {
local required_commands=(git git-crypt stow)
for cmd in "${required_commands[@]}"; do
if ! command_exists "$cmd"; then
log_error "Required command not found: $cmd"
exit 1
fi
done
}
clone_dotfiles() {
log_info "Setting up dotfiles repository"
if [[ -d "$DOTFILES_DIR" ]]; then
log_info "Dotfiles directory already exists, updating"
cd "$DOTFILES_DIR"
git pull origin main || git pull origin master
else
log_info "Cloning dotfiles to $DOTFILES_DIR"
git clone "$DOTFILES_REPO" "$DOTFILES_DIR"
fi
}
handle_git_crypt() {
log_info "Checking git-crypt status"
cd "$DOTFILES_DIR"
if git-crypt status | grep -q "unlocked"; then
log_info "Repository already unlocked"
return 0
fi
log_info "Repository is encrypted, attempting to unlock"
if gpg --card-status &>/dev/null; then
log_info "YubiKey detected, fetching public key"
local key_id
key_id=$(gpg --card-status 2>/dev/null | grep "General key info" -A 1 | tail -1 | awk '{print $1}' | sed 's/.*\///')
if [[ -n "$key_id" ]] && ! gpg --list-keys "$key_id" &>/dev/null; then
gpg --card-edit --batch --command-fd 0 <<<"fetch" &>/dev/null ||
gpg --keyserver hkps://keys.openpgp.org --recv-keys "$key_id" &>/dev/null || true
fi
else
log_error "YubiKey not detected"
exit 1
fi
if git-crypt unlock 2>/dev/null; then
log_info "Repository unlocked successfully"
else
log_error "Failed to unlock repository"
log_info "Manual unlock required: cd $DOTFILES_DIR && git-crypt unlock"
exit 1
fi
}
create_directories() {
log_info "Creating necessary directories"
mkdir -p "$HOME/.config" "$HOME/Dev" "$HOME/.local/bin"
}
stow_packages() {
cd "$DOTFILES_DIR"
log_info "Available stow packages:"
for dir in */; do
if [[ -d "$dir" ]]; then
log_info " ${dir%/}"
fi
done
if confirm "Stow all packages?" "y"; then
log_info "Stowing all packages"
if stow */; then
log_info "All packages stowed successfully"
else
log_error "Some packages failed to stow. Check for conflicts"
fi
else
log_info "Skipping automatic stow. Run manually:"
log_info " cd $DOTFILES_DIR"
log_info " stow <package_name>"
fi
}
main() {
init_script
check_requirements
clone_dotfiles
handle_git_crypt
create_directories
stow_packages
log_info "Dotfiles location: $DOTFILES_DIR"
finish_script 0
}
main "$@"