#!/usr/bin/env bash

GO_EXE_ROOT=./apps/cli/executables/go
GO_EXES=$(find ${GO_EXE_ROOT} -type d -depth 1 -print)

# Retrieve generated godocs for all Go executables
for exe in $GO_EXES; do
    pushd "$exe"
    godoc -http=:6060 &
    sleep 2
    wget -r -N -np -nH -E -p -k --cut-dirs=1 http://localhost:6060/pkg/ssa/"$(basename "$exe")"
    kill "$(pgrep godoc)"
    sleep 2
    popd
done

# move generated godoc files to docs area
for exe in $GO_EXES; do
  doc_dir=./docs/godocs/$(basename "$exe")
    if [[ ! -d $doc_dir ]]; then
      mkdir "$doc_dir"
    fi

    # copy, overwrite if exists
    rsync -a "$exe"/godoc "$doc_dir"
    rsync -a "$exe"/ssa "$doc_dir"

    # remove docs from executable directory
    rm -rf "$exe"/godoc
    rm -rf "$exe"/ssa
done
