#!/bin/bash # Remote publish script for marcus-web # Pushes local changes if needed, then builds on SDF # Optionally deploys to gopher with --gopher or --gopher-only set -e SDF_HOST="mnw@sdf.org" REMOTE_DIR="~/marcus-web" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Flags GOPHER=false GOPHER_ONLY=false # Parse arguments while [[ $# -gt 0 ]]; do case $1 in --gopher) GOPHER=true shift ;; --gopher-only) GOPHER_ONLY=true GOPHER=true shift ;; --help|-h) echo "Usage: $0 [OPTIONS]" echo "" echo "Options:" echo " --gopher Deploy to both Hugo site and gopher" echo " --gopher-only Deploy only to gopher (skip Hugo)" echo " --help, -h Show this help message" exit 0 ;; *) echo -e "${RED}Unknown option: $1${NC}" echo "Use --help for usage information" exit 1 ;; esac done echo -e "${GREEN}=== Remote Publish ===${NC}" if [[ "$GOPHER" == "true" ]]; then if [[ "$GOPHER_ONLY" == "true" ]]; then echo -e "${YELLOW}Mode: Gopher only${NC}" else echo -e "${YELLOW}Mode: Hugo + Gopher${NC}" fi fi echo "" # Check if we're in a git repo if ! git rev-parse --git-dir > /dev/null 2>&1; then echo -e "${RED}Error: Not in a git repository${NC}" exit 1 fi # Fetch latest from remote to compare echo "Checking repository status..." git fetch origin 2>/dev/null || true # Check for uncommitted changes (staged or unstaged) if ! git diff-index --quiet HEAD -- 2>/dev/null || [ -n "$(git ls-files --others --exclude-standard)" ]; then echo -e "${YELLOW}You have uncommitted changes:${NC}" git status --short echo "" read -p "Would you like to commit these changes? (y/n) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then # Stage all changes git add -A read -p "Custom commit message? (y/n) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then read -p "Enter commit message: " COMMIT_MSG else # Try to find a post title from recently modified markdown files COMMIT_MSG="" RECENT_MD=$(git diff --cached --name-only | grep '\.md$' | head -1) if [ -n "$RECENT_MD" ] && [ -f "$RECENT_MD" ]; then # Extract title from frontmatter TITLE=$(grep -m1 "^title:" "$RECENT_MD" 2>/dev/null | sed "s/^title:[[:space:]]*['\"]*//" | sed "s/['\"].*$//") if [ -n "$TITLE" ]; then COMMIT_MSG="Remote Publish Auto Commit - $TITLE" fi fi # Fallback to date-based message if [ -z "$COMMIT_MSG" ]; then COMMIT_MSG="Auto Commit - $(date '+%Y-%m-%d %H:%M')" fi echo "Using commit message: $COMMIT_MSG" fi git commit -m "$COMMIT_MSG" echo -e "${GREEN}Changes committed${NC}" echo "" else echo -e "${YELLOW}Continuing without committing...${NC}" read -p "Are you sure? Remote will not have these changes. (y/n) " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborted." exit 1 fi fi fi # Check if local is ahead of remote LOCAL=$(git rev-parse HEAD 2>/dev/null) REMOTE=$(git rev-parse @{u} 2>/dev/null || echo "") BASE=$(git merge-base HEAD @{u} 2>/dev/null || echo "") if [ -z "$REMOTE" ]; then echo -e "${YELLOW}Warning: No upstream branch configured${NC}" elif [ "$LOCAL" != "$REMOTE" ]; then if [ "$LOCAL" = "$BASE" ]; then echo -e "${YELLOW}Local is behind remote. You may want to pull first.${NC}" elif [ "$REMOTE" = "$BASE" ]; then echo -e "${YELLOW}Local commits not pushed to remote:${NC}" git log --oneline @{u}..HEAD echo "" read -p "Push changes before publishing? (y/n) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then echo "Pushing..." git push echo -e "${GREEN}Pushed successfully${NC}" else echo -e "${YELLOW}Skipping push. Remote will not have latest changes.${NC}" read -p "Continue anyway? (y/n) " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborted." exit 1 fi fi else echo -e "${YELLOW}Warning: Local and remote have diverged${NC}" read -p "Continue anyway? (y/n) " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborted." exit 1 fi fi else echo -e "${GREEN}Repository is up to date with remote${NC}" fi echo "" echo -e "${GREEN}=== Connecting to SDF ===${NC}" echo "" # Test SSH connectivity SSH_AUTH="password" if ssh -o BatchMode=yes -o ConnectTimeout=5 "$SDF_HOST" "echo 'SSH key auth successful'" 2>/dev/null; then SSH_AUTH="key" echo "Using SSH key authentication..." else echo "Using password authentication..." fi # Hugo deployment (unless --gopher-only) if [[ "$GOPHER_ONLY" != "true" ]]; then echo "" echo -e "${GREEN}=== Deploying Hugo Site ===${NC}" REMOTE_CMD="cd $REMOTE_DIR && git pull && hugo && mkhomepg -p" ssh "$SDF_HOST" "$REMOTE_CMD" echo "" echo -e "${GREEN}Hugo site published!${NC}" echo "Site is live at: https://mnw.sdf.org/" fi # Gopher deployment (if --gopher or --gopher-only) if [[ "$GOPHER" == "true" ]]; then echo "" echo -e "${GREEN}=== Deploying to Gopher ===${NC}" # Build gopher content locally echo "Converting posts to gopher format..." cd "$PROJECT_ROOT" python3 "$SCRIPT_DIR/gopher/convert_to_gopher.py" --all --output "$PROJECT_ROOT/gopher_build/blog/" echo "Generating gophermaps..." python3 "$SCRIPT_DIR/gopher/generate_gophermaps.py" --output "$PROJECT_ROOT/gopher_build/blog/" # Sync to SDF (--delete removes posts where phlog was set to false) echo "Syncing to SDF gopherspace..." rsync -avz --delete "$PROJECT_ROOT/gopher_build/blog/" "$SDF_HOST:~/gopher/blog/" # Fix permissions on SDF echo "Setting permissions..." ssh "$SDF_HOST" "find ~/gopher/blog -type f -exec chmod 644 {} \; && find ~/gopher/blog -type d -exec chmod 755 {} \;" echo "" echo -e "${GREEN}Gopher published!${NC}" echo "Gopher is live at: gopher://sdf.org/1/users/mnw/blog/" echo "Web proxy: https://gopher.floodgap.com/gopher/gw?a=gopher://sdf.org/1/users/mnw/blog/" fi echo "" echo -e "${GREEN}=== All done! ===${NC}"