   alias Serum.Error
   alias Serum.SiteBuilder
 
-  ## Client
+  #
+  # GenServer Implementation - Client
+  #
 
   @spec start_link(pid, String.t, String.t, pos_integer)
     :: {:ok, pid} | {:error, atom}


  def port(), do: GenServer.call __MODULE__, :port

  ## Server

  def init([builder, dir, site, portnum]) do
    do_rebuild builder

  @spec do_rebuild(pid) :: :ok

  defp do_rebuild(builder) do
    case SiteBuilder.load_info builder do
      {:ok, _info} ->
        case SiteBuilder.build builder, :parallel do
          {:ok, _} -> :ok
          error -> build_failed error
        end
      error -> build_failed error
    end
  end

  defp build_failed(error) do
    Error.show error
    warn "Error occurred while building the website."
    warn "The website may not be displayed correctly."
  end
end
