#!/usr/bin/env bash

run_user=$(id -un)
if [ "$run_user" = "gemini" ]; then
  # clear
  cd /var/gemini || return
  rm -rf ./*

  # copy gopher to start with all txt content
  cp -R /var/gopher/* /var/gemini > /dev/null 2>&1

  # generate index pages
  # curl -s gopher://localhost | /var/cosmic/awk/gophermap2gemini.awk > /var/gemini/index.gmi
  cp /var/cosmic/templates/geminiintro.tmpl /var/gemini/index.gmi
  curl -s -N gopher://localhost/1/listing.gophermap | head -n 20 | /var/cosmic/awk/gophermap2gemini.awk >> /var/gemini/index.gmi
  curl -s -N gopher://localhost/1/log | /var/cosmic/awk/gophermap2gemini.awk > /var/gemini/log/index.gmi
  curl -s -N gopher://localhost/1/ships | /var/cosmic/awk/gophermap2gemini.awk > /var/gemini/ships/index.gmi
  sed 's?gopher://cosmic.voyage/0/?gemini://cosmic.voyage/?' /var/gopher/rss.xml | sed 's?<link>gopher://cosmic.voyage?<link>gemini://cosmic.voyage?' > /var/gemini/rss.xml
  sed 's?gopher://cosmic.voyage/0/?gemini://cosmic.voyage/?' /var/gopher/atom.xml | sed 's?<link>gopher://cosmic.voyage?<link>gemini://cosmic.voyage?' > /var/gemini/atom.xml

  # create ship directories
  find "/var/gopher/" -maxdepth 1 ! -path "/var/gopher/" ! -path "/var/gopher/ships" ! -path "/var/gopher/log" -type d | sed 's|/var/gopher/||' | sort | while read -r ship
  do
    curl -s -N "gopher://localhost/1/ships/${ship}" | /var/cosmic/awk/gophermap2gemini.awk > "/var/gemini/ships/${ship}/index.gmi"
  done
  # Let user's know it's done
  printf "Gemini and gemini feeds rebuilt successfully.\\n"
else
  exec sudo -u gemini "$0" "$@"
fi
