You know, there are moments in life that spark a bit of annoyance—like when you see double quotes misaligned on a shop sign. My wife has a talent for identifying these little quirks, often asking, “Do you know what I hate?” It’s endearing, really. While I might not keep a formal list of grievances with the world at large, I certainly have a catalog of digital frustrations.

Take, for example, the maintenance of my personal website. I cherish having a digital space to call my own, but the upkeep? Not so much. After a few weeks away, I find myself grappling with the intricacies of updating git submodules or deciphering the commit hashes that seem to have a vendetta against me. It’s enough to make anyone wish for a bit of automation.

So, like any seasoned developer, I channeled my frustration into productivity and crafted a script to make my life easier. Below is a streamlined solution that not only checks the installed versions of Hugo and PaperMod but also updates them if necessary—because who has time for manual updates?

#!/bin/bash

# ===============================================
# Hugo Update & Deployment Script
# ===============================================
# This script checks the installed version of Hugo and PaperMod,
# compares them to the latest available versions, and provides
# an option to update them if an update is available.
#
# Usage:
#   ./deploy.sh            # Default: Checks versions and deploys if up to date
#   ./deploy.sh --help     # Show this help message
#
# How It Works:
# 1. It checks the installed and latest versions of Hugo and PaperMod.
# 2. If both are up to date, the script automatically deploys the site.
# 3. If an update is available, it:
#    - Displays the installed and latest versions.
#    - Asks if you want to update Hugo.
#    - Asks if you want to update PaperMod.
#    - Asks if you want to deploy after updating.
#
# Requirements:
# - Hugo installed and accessible via `hugo version`
# - Homebrew installed (for updating Hugo)
# - Git installed (for checking/updating the PaperMod submodule)
#
# Example Runs:
# - Default behavior (check and deploy if up to date):
#     ./deploy.sh
# - Show help:
#     ./deploy.sh --help
# ===============================================

# Function to display help message
show_help() {
    sed -n '2,34p' "$0" | sed 's/# //'
    exit 0
}

# Handle the --help option
if [[ "$1" == "--help" ]]; then
    show_help
fi

# Function to get installed Hugo version
get_installed_hugo_version() {
    hugo_version=$(hugo version 2>/dev/null | awk '{print $2}' | cut -d'+' -f1)
    echo "${hugo_version#v}"  # Remove leading 'v' if present
}

# Function to get latest stable Hugo version
get_latest_hugo_version() {
    latest_version=$(curl -s https://api.github.com/repos/gohugoio/hugo/releases/latest | grep '"tag_name":' | cut -d '"' -f 4)
    echo "${latest_version#v}"
}

# Function to get installed PaperMod version (commit hash)
get_installed_papermod_version() {
    git submodule status themes/PaperMod 2>/dev/null | awk '{print $1}'
}

# Function to get the latest PaperMod version (commit hash from the latest release)
get_latest_papermod_version() {
    latest_papermod_tag=$(curl -s https://api.github.com/repos/adityatelange/hugo-PaperMod/releases/latest | jq -r .tag_name)

    if [[ -z "$latest_papermod_tag" ]]; then
        echo "Error: Could not fetch latest PaperMod release tag."
        exit 1
    fi

    latest_commit=$(git ls-remote https://github.com/adityatelange/hugo-PaperMod.git "refs/tags/$latest_papermod_tag^{}" | awk '{print $1}')
    
    if [[ -z "$latest_commit" ]]; then
        echo "Error: Could not retrieve commit hash for latest PaperMod release."
        exit 1
    fi

    echo "$latest_commit"
}

# Function to update Hugo using Homebrew
update_hugo() {
    echo "Updating Hugo..."
    brew upgrade hugo || echo "Homebrew upgrade failed."
}

# Function to update PaperMod theme
update_papermod() {
    echo "Updating PaperMod to the latest release..."

    cd themes/PaperMod || { echo "Error: PaperMod submodule not found."; exit 1; }

    git fetch --tags origin
    latest_papermod_tag=$(curl -s https://api.github.com/repos/adityatelange/hugo-PaperMod/releases/latest | jq -r .tag_name)

    if [[ -z "$latest_papermod_tag" ]]; then
        echo "Error: Could not fetch latest PaperMod release tag."
        exit 1
    fi

    git checkout "$latest_papermod_tag"

    cd - >/dev/null

    echo "Committing updated PaperMod submodule reference..."
    git add themes/PaperMod
    git commit -m "Updated PaperMod submodule to latest $latest_papermod_tag"
}

# Function to deploy Hugo site
deploy_hugo() {
    echo "Deploying Hugo site..."

    hugo --cleanDestinationDir
    if [[ $? -ne 0 ]]; then
        echo "Hugo Build command failed!  Exit..."
        exit 1
    fi

    rsync -av /papermod/public/* root@REMOTESYSTEM/path/to/public_html/ --delete
    if [[ $? -ne 0 ]]; then
        echo "Rsync command failed!  Exit..."
        exit 1
    fi
}

# Get current and latest versions
installed_hugo_version=$(get_installed_hugo_version)
latest_hugo_version=$(get_latest_hugo_version)
installed_papermod_version=$(get_installed_papermod_version)
latest_papermod_version=$(get_latest_papermod_version)

hugo_outdated=false
papermod_outdated=false

# Check if updates are needed
if [[ "$installed_hugo_version" != "$latest_hugo_version" ]]; then
    hugo_outdated=true
fi

if [[ "$installed_papermod_version" != "$latest_papermod_version" ]]; then
    papermod_outdated=true
fi

# If everything is up to date, deploy immediately
if [[ "$hugo_outdated" == false && "$papermod_outdated" == false ]]; then
    echo "Hugo and PaperMod are up to date."
    deploy_hugo
    exit 0
fi

# Show version differences if any updates are available
echo "Updates Available:"
if [[ "$hugo_outdated" == true ]]; then
    echo "  - Hugo:"
    echo "      Installed = $installed_hugo_version"
    echo "      Latest    = $latest_hugo_version"
fi
if [[ "$papermod_outdated" == true ]]; then
    echo "  - PaperMod:"
    echo "      Installed = $installed_papermod_version"
    echo "      Latest    = $latest_papermod_version"
fi

# Prompt the user for updates
if [[ "$hugo_outdated" == true ]]; then
    read -p "Do you want to update Hugo? (y/n): " update_hugo_choice
    if [[ "$update_hugo_choice" == "y" || "$update_hugo_choice" == "Y" ]]; then
        update_hugo
    fi
fi

if [[ "$papermod_outdated" == true ]]; then
    read -p "Do you want to update PaperMod? (y/n): " update_papermod_choice
    if [[ "$update_papermod_choice" == "y" || "$update_papermod_choice" == "Y" ]]; then
        update_papermod
    fi
fi

# Ask if the user wants to deploy after updating
read -p "Do you want to deploy the site now? (y/n): " deploy_choice
if [[ "$deploy_choice" == "y" || "$deploy_choice" == "Y" ]]; then
    deploy_hugo
else
    echo "Skipping deployment."
fi

This script is more than just a convenience; it’s a safeguard against the vagaries of version management and deployment. By automating these processes, we can reclaim time and mental energy—allowing us to focus on what truly matters: creating and sharing our ideas. So, the next time you find yourself frustrated by a minor detail, remember: there’s always a way to streamline your workflow. Happy scripting!