#!/bin/sh

validate_hugo () {
   # Exit with error if hugo is not installed
   if ! hash hugo 2>/dev/null ; then
     echo "Error: After Dark requires Hugo version 0.44 or greater" >&2; exit 1
   fi

   # Exit with error if not minimum required hugo version
   re="v(0\d*\.([4-9][4-9]|[5-9])|[1-9]).*"
   if ! hugo version | grep -qE "$re" ; then
      echo "Error: After Dark requires Hugo version 0.44 or greater" >&2; exit 1
   fi
}

create_site_dir () {
   SITE_DIR="flying-toasters"
   if [ "$1" != "" ] ; then
      SITE_DIR="$1"
   fi

   SITE_DIR_ABS="$PWD/$SITE_DIR"
   mkdir -p "$SITE_DIR"
}

create_site () {
   echo "Creating a new Hugo site ..."
   hugo new site "$SITE_DIR" 1>/dev/null
   cd "$SITE_DIR" || exit 1
}

download_theme () {
   echo "Downloading the latest version of After Dark ..."
   LATEST_META=$(wget -qO - https://registry.npmjs.org/after-dark/latest)
   vers=$(echo "$LATEST_META" | egrep -o "\"version\".*[^,]*," | cut -d ',' -f1 | cut -d ':' -f2 | tr -d '" ')
   mkdir -p themes/after-dark
   wget -qO - https://registry.npmjs.org/after-dark/-/after-dark-"$vers".tgz | tar --strip-components=1 -xz -C themes/after-dark
   echo "Version $vers downloaded to $SITE_DIR/themes/after-dark"
}

download_module () {
   [ -z "$1" ] && { echo "Error: Attempt to download undefined module" >&2; exit 1; }
   echo "Downloading $1 module for After Dark ..."
   meta=$(wget -qO - https://registry.npmjs.org/"$1"/latest)
   vers=$(echo "$meta" | egrep -o "\"version\".*[^,]*," | cut -d ',' -f1 | cut -d ':' -f2 | tr -d '" ')
   mkdir -p themes/"$1"
   wget -qO - https://registry.npmjs.org/"$1"/-/"$1"-"$vers".tgz | tar --strip-components=1 -xz -C themes/"$1"
   echo "Version $vers downloaded to $SITE_DIR/themes/$1"
}

configure_theme () {
   echo "Configuring basic After Dark theme settings ..."
   tee "config.toml" > /dev/null <<TOML
baseurl = "https://domain.example" # Controls base URL sitewide
languageCode = "en-US" # Controls site language
title = "After Dark" # Homepage title and page title suffix
paginate = 11 # Number of posts to show before paginating

# Controls default theme and theme components
theme = [
  "fractal-forest",
  "after-dark"
]

disableLiveReload = false # Optional, set true to disable live reload
enableRobotsTXT = true # Suggested, enable robots.txt file

pygmentsCodefences = true # Suggested, highlight fenced code blocks
pygmentsUseClasses = true # Required for custom syntax highlighting

sectionPagesMenu = "main" # Enable menu system for lazy bloggers
footnoteReturnLinkContents = "↩" # Provides a nicer footnote return link

[params]
  description = "" # Suggested, controls default description meta
  author = "" # Optional, controls author name display on posts
  hide_author = false # Optional, set true to hide author name on posts
  has_cookies = false # Optional, set true to disable cookie disclaimer
  disable_csp = false # Optional, set true to disable content security policy
  images = [
    "https://source.unsplash.com/collection/983219/2000x1322"
  ] # Suggested, controls default Open Graph images

[params.layout.menu.main]
  hidden = true # Optional, set false or remove to show section menu

[params.modules.fractal_forest]
  enabled = true # Optional, set false to disable module
  decoders = ["bpgdec8a"] # Optional, 8-bit javascript decoder with animation
TOML
}

update_archetypes () {
   echo "Updating the default content archetype ..."
   rm -f archetypes/default.md
   cp themes/after-dark/archetypes/default.md archetypes
}

create_welcome_post () {
   echo "Creating welcome post ..."
   hugo new post/welcome.md 1>/dev/null
}

serve_site () {
   echo "Starting site server ..."
   hugo serve --buildDrafts --navigateToChanged --port 1313 1>/dev/null &
}

generate_help_docs () {
   echo "Generating help documentation ..."
   THEME_PATH=themes/after-dark
   meta_path="$THEME_PATH"/data/npm
   mkdir -p "$meta_path" && echo "$LATEST_META" | tr '\r\n' ' ' > "$meta_path"/latest.json
   cd "$THEME_PATH"/docs && mkdir themes && ln -s ../.. "$THEME_PATH"
   hugo new validate.md --kind validate 1>/dev/null
}

serve_help () {
   echo "Starting help server ..."
   hugo serve --disableLiveReload --port 1414 1>/dev/null &
}

set -e

echo "Welcome to the After Dark quick installer. Press CTRL-C at any time to abort."

validate_hugo
create_site_dir "$1"
create_site
download_theme
update_archetypes
download_module "fractal-forest"
configure_theme
create_welcome_post
serve_site
generate_help_docs
serve_help

YELLOW='\033[0;33m'
NC='\033[0m'

printf "${YELLOW}Installation successful!${NC}\n"
echo "Site created in $SITE_DIR_ABS"
echo "Site server started at http://localhost:1313/"
echo "To stop it run \"kill \$(ps aux | awk '/[h]ugo.*1313/ {print \$2}')\"."
echo "Help server started at http://localhost:1414/"
echo "To stop and restart it run \"./themes/after-dark/bin/help\"."
echo "Thank you for choosing After Dark."
