#!/usr/bin/env bash

#############################################################################
##
##   Documentation build script
##   Last updated on July 14, 2026
##
##   This file is part of Logtalk <https://logtalk.org/>
##   SPDX-FileCopyrightText: 1998-2026 Paulo Moura <pmoura@logtalk.org>
##   SPDX-License-Identifier: Apache-2.0
##
##   Licensed under the Apache License, Version 2.0 (the "License");
##   you may not use this file except in compliance with the License.
##   You may obtain a copy of the License at
##
##       http://www.apache.org/licenses/LICENSE-2.0
##
##   Unless required by applicable law or agreed to in writing, software
##   distributed under the License is distributed on an "AS IS" BASIS,
##   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##   See the License for the specific language governing permissions and
##   limitations under the License.
##
#############################################################################


# allow using this script from any directory
cd "$(dirname "$0")" || exit 1

rm -f ../TheLogtalkHandbook*.pdf
rm -f ../TheLogtalkHandbook*.epub
rm -f ../TheLogtalkHandbook*.info
rm -f ../TheLogtalkHandbook*.md
rm -rf ../_sources
rm -rf ../_static
rm -rf ../faq
rm -rf ../refman
rm -rf ../tutorial
rm -rf ../userman
rm -rf ../devtools
rm -rf ../libraries
rm -rf ../ports
rm -rf ../contributions
make clean

sed '1,19d' ../../../tools/NOTES.md | pandoc -f gfm -t rst -o devtools/overview.rst
sed '1,19d' ../../../tools/asdf/NOTES.md | pandoc -f gfm -t rst -o devtools/asdf.rst
sed '1,19d' ../../../tools/assertions/NOTES.md | pandoc -f gfm -t rst -o devtools/assertions.rst
sed '1,19d' ../../../tools/code_metrics/NOTES.md | pandoc -f gfm -t rst -o devtools/code_metrics.rst
sed '1,19d' ../../../tools/dead_code_scanner/NOTES.md | pandoc -f gfm -t rst -o devtools/dead_code_scanner.rst
sed '1,19d' ../../../tools/debug_messages/NOTES.md | pandoc -f gfm -t rst -o devtools/debug_messages.rst
sed '1,19d' ../../../tools/debugger/NOTES.md | pandoc -f gfm -t rst -o devtools/debugger.rst
sed '1,19d' ../../../tools/diagrams/NOTES.md | pandoc -f gfm -t rst -o devtools/diagrams.rst
sed '1,19d' ../../../tools/doclet/NOTES.md | pandoc -f gfm -t rst -o devtools/doclet.rst
sed '1,19d' ../../../tools/help/NOTES.md | pandoc -f gfm -t rst -o devtools/help.rst
sed '1,19d' ../../../tools/issue_creator/NOTES.md | pandoc -f gfm -t rst -o devtools/issue_creator.rst
sed '1,19d' ../../../tools/lgtdoc/NOTES.md | pandoc -f gfm -t rst -o devtools/lgtdoc.rst
sed '1,19d' ../../../tools/lgtunit/NOTES.md | pandoc -f gfm -t rst -o devtools/lgtunit.rst
sed '1,19d' ../../../tools/linter/NOTES.md | pandoc -f gfm -t rst -o devtools/linter.rst
sed '1,19d' ../../../tools/linter_reporter/NOTES.md | pandoc -f gfm -t rst -o devtools/linter_reporter.rst
sed '1,19d' ../../../tools/make/NOTES.md | pandoc -f gfm -t rst -o devtools/make.rst
sed '1,19d' ../../../tools/mutation_testing/NOTES.md | pandoc -f gfm -t rst -o devtools/mutation_testing.rst
sed '1,19d' ../../../tools/packs/NOTES.md | pandoc -f gfm -t rst -o devtools/packs.rst
sed '1,19d' ../../../tools/ports_profiler/NOTES.md | pandoc -f gfm -t rst -o devtools/ports_profiler.rst
sed '1,19d' ../../../tools/profiler/NOTES.md | pandoc -f gfm -t rst -o devtools/profiler.rst
sed '1,19d' ../../../tools/sarif/NOTES.md | pandoc -f gfm -t rst -o devtools/sarif.rst
sed '1,19d' ../../../tools/sbom/NOTES.md | pandoc -f gfm -t rst -o devtools/sbom.rst
sed '1,19d' ../../../tools/tool_diagnostics/NOTES.md | pandoc -f gfm -t rst -o devtools/tool_diagnostics.rst
sed '1,19d' ../../../tools/tutor/NOTES.md | pandoc -f gfm -t rst -o devtools/tutor.rst
sed '1,19d' ../../../tools/wrapper/NOTES.md | pandoc -f gfm -t rst -o devtools/wrapper.rst

for file in devtools/*.rst; do
	base="${file##*/}"
	if [ "$base" != "index.rst" ] && [ "$base" != "overview.rst" ] ; then
		name="${base%.*}"
		echo ".. _library_$name:" > temp0 && echo >> temp0 && cat temp0 "$file" > temp1 && mv temp1 "$file"
	fi
done
rm -f temp0

sed '1,19d' ../../../library/NOTES.md | pandoc -f gfm -t rst -o libraries/overview.rst
sed '1,19d' ../../../library/adaptive_boosting_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/adaptive_boosting_classifier.rst
sed '1,19d' ../../../library/agglomerative_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/agglomerative_clusterer.rst
sed '1,19d' ../../../library/amqp/NOTES.md | pandoc -f gfm -t rst -o libraries/amqp.rst
sed '1,19d' ../../../library/anomaly_detection_protocols/NOTES.md | pandoc -f gfm -t rst -o libraries/anomaly_detection_protocols.rst
sed '1,19d' ../../../library/application/NOTES.md | pandoc -f gfm -t rst -o libraries/application.rst
sed '1,19d' ../../../library/apriori_pattern_miner/NOTES.md | pandoc -f gfm -t rst -o libraries/apriori_pattern_miner.rst
sed '1,19d' ../../../library/arbitrary/NOTES.md | pandoc -f gfm -t rst -o libraries/arbitrary.rst
sed '1,19d' ../../../library/arrangements/NOTES.md | pandoc -f gfm -t rst -o libraries/arrangements.rst
sed '1,19d' ../../../library/assignvars/NOTES.md | pandoc -f gfm -t rst -o libraries/assignvars.rst
sed '1,19d' ../../../library/avro/NOTES.md | pandoc -f gfm -t rst -o libraries/avro.rst
sed '1,19d' ../../../library/base32/NOTES.md | pandoc -f gfm -t rst -o libraries/base32.rst
sed '1,19d' ../../../library/base58/NOTES.md | pandoc -f gfm -t rst -o libraries/base58.rst
sed '1,19d' ../../../library/base64/NOTES.md | pandoc -f gfm -t rst -o libraries/base64.rst
sed '1,19d' ../../../library/base85/NOTES.md | pandoc -f gfm -t rst -o libraries/base85.rst
sed '1,19d' ../../../library/basic_types/NOTES.md | pandoc -f gfm -t rst -o libraries/basic_types.rst
sed '1,19d' ../../../library/bayesian_ridge_regression/NOTES.md | pandoc -f gfm -t rst -o libraries/bayesian_ridge_regression.rst
sed '1,19d' ../../../library/borda_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/borda_ranker.rst
sed '1,19d' ../../../library/bradley_terry_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/bradley_terry_ranker.rst
sed '1,19d' ../../../library/c45_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/c45_classifier.rst
sed '1,19d' ../../../library/cartesian_products/NOTES.md | pandoc -f gfm -t rst -o libraries/cartesian_products.rst
sed '1,19d' ../../../library/copeland_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/copeland_ranker.rst
sed '1,19d' ../../../library/coroutining/NOTES.md | pandoc -f gfm -t rst -o libraries/coroutining.rst
sed '1,19d' ../../../library/crypto/NOTES.md | pandoc -f gfm -t rst -o libraries/crypto.rst
sed '1,19d' ../../../library/crs_projections/NOTES.md | pandoc -f gfm -t rst -o libraries/crs_projections.rst
sed '1,19d' ../../../library/cbor/NOTES.md | pandoc -f gfm -t rst -o libraries/cbor.rst
sed '1,19d' ../../../library/ccsds_frames/NOTES.md | pandoc -f gfm -t rst -o libraries/ccsds_frames.rst
sed '1,19d' ../../../library/ccsds_link_profiles/NOTES.md | pandoc -f gfm -t rst -o libraries/ccsds_link_profiles.rst
sed '1,19d' ../../../library/ccsds_packetization/NOTES.md | pandoc -f gfm -t rst -o libraries/ccsds_packetization.rst
sed '1,19d' ../../../library/ccsds_packets/NOTES.md | pandoc -f gfm -t rst -o libraries/ccsds_packets.rst
sed '1,19d' ../../../library/ccsds_packet_services/NOTES.md | pandoc -f gfm -t rst -o libraries/ccsds_packet_services.rst
sed '1,19d' ../../../library/ccsds_tc_services/NOTES.md | pandoc -f gfm -t rst -o libraries/ccsds_tc_services.rst
sed '1,19d' ../../../library/ccsds_time_codes/NOTES.md | pandoc -f gfm -t rst -o libraries/ccsds_time_codes.rst
sed '1,19d' ../../../library/ccsds_time_fields/NOTES.md | pandoc -f gfm -t rst -o libraries/ccsds_time_fields.rst
sed '1,19d' ../../../library/character_sets/NOTES.md | pandoc -f gfm -t rst -o libraries/character_sets.rst
sed '1,19d' ../../../library/classification_protocols/NOTES.md | pandoc -f gfm -t rst -o libraries/classification_protocols.rst
sed '1,19d' ../../../library/clo_span_pattern_miner/NOTES.md | pandoc -f gfm -t rst -o libraries/clo_span_pattern_miner.rst
sed '1,19d' ../../../library/clustering_protocols/NOTES.md | pandoc -f gfm -t rst -o libraries/clustering_protocols.rst
sed '1,19d' ../../../library/colley_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/colley_ranker.rst
sed '1,19d' ../../../library/combinations/NOTES.md | pandoc -f gfm -t rst -o libraries/combinations.rst
sed '1,32d' ../../../library/command_line_options/NOTES.md | pandoc -f gfm -t rst -o libraries/command_line_options.rst
sed '1,19d' ../../../library/csv/NOTES.md | pandoc -f gfm -t rst -o libraries/csv.rst
sed '1,19d' ../../../library/cuid2/NOTES.md | pandoc -f gfm -t rst -o libraries/cuid2.rst
sed '1,19d' ../../../library/cusum_anomaly_detector/NOTES.md | pandoc -f gfm -t rst -o libraries/cusum_anomaly_detector.rst
sed '1,19d' ../../../library/datalog/NOTES.md | pandoc -f gfm -t rst -o libraries/datalog.rst
sed '1,19d' ../../../library/dates/NOTES.md | pandoc -f gfm -t rst -o libraries/dates.rst
sed '1,19d' ../../../library/dates_tz/NOTES.md | pandoc -f gfm -t rst -o libraries/dates_tz.rst
sed '1,19d' ../../../library/dbscan_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/dbscan_clusterer.rst
sed '1,19d' ../../../library/dependents/NOTES.md | pandoc -f gfm -t rst -o libraries/dependents.rst
sed '1,19d' ../../../library/deques/NOTES.md | pandoc -f gfm -t rst -o libraries/deques.rst
sed '1,19d' ../../../library/derangements/NOTES.md | pandoc -f gfm -t rst -o libraries/derangements.rst
sed '1,19d' ../../../library/dictionaries/NOTES.md | pandoc -f gfm -t rst -o libraries/dictionaries.rst
sed '1,19d' ../../../library/dif/NOTES.md | pandoc -f gfm -t rst -o libraries/dif.rst
sed '1,19d' ../../../library/dimension_reduction_protocols/NOTES.md | pandoc -f gfm -t rst -o libraries/dimension_reduction_protocols.rst
sed '1,19d' ../../../library/eclat_pattern_miner/NOTES.md | pandoc -f gfm -t rst -o libraries/eclat_pattern_miner.rst
sed '1,19d' ../../../library/edcg/NOTES.md | pandoc -f gfm -t rst -o libraries/edcg.rst
sed '1,19d' ../../../library/elastic_net_regression/NOTES.md | pandoc -f gfm -t rst -o libraries/elastic_net_regression.rst
sed '1,19d' ../../../library/elo_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/elo_ranker.rst
sed '1,19d' ../../../library/events/NOTES.md | pandoc -f gfm -t rst -o libraries/events.rst
sed '1,19d' ../../../library/ewma_anomaly_detector/NOTES.md | pandoc -f gfm -t rst -o libraries/ewma_anomaly_detector.rst
sed '1,19d' ../../../library/expand_library_alias_paths/NOTES.md | pandoc -f gfm -t rst -o libraries/expand_library_alias_paths.rst
sed '1,19d' ../../../library/expecteds/NOTES.md | pandoc -f gfm -t rst -o libraries/expecteds.rst
sed '1,19d' ../../../library/format/NOTES.md | pandoc -f gfm -t rst -o libraries/format.rst
sed '1,19d' ../../../library/fp_growth_pattern_miner/NOTES.md | pandoc -f gfm -t rst -o libraries/fp_growth_pattern_miner.rst
sed '1,19d' ../../../library/frequent_pattern_mining_protocols/NOTES.md | pandoc -f gfm -t rst -o libraries/frequent_pattern_mining_protocols.rst
sed '1,19d' ../../../library/gaussian_mixture_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/gaussian_mixture_clusterer.rst
sed '1,19d' ../../../library/gaussian_process_regression/NOTES.md | pandoc -f gfm -t rst -o libraries/gaussian_process_regression.rst
sed '1,19d' ../../../library/genint/NOTES.md | pandoc -f gfm -t rst -o libraries/genint.rst
sed '1,19d' ../../../library/gensym/NOTES.md | pandoc -f gfm -t rst -o libraries/gensym.rst
sed '1,19d' ../../../library/geojson/NOTES.md | pandoc -f gfm -t rst -o libraries/geojson.rst
sed '1,19d' ../../../library/geohash/NOTES.md | pandoc -f gfm -t rst -o libraries/geohash.rst
sed '1,19d' ../../../library/geospatial/NOTES.md | pandoc -f gfm -t rst -o libraries/geospatial.rst
sed '1,19d' ../../../library/git/NOTES.md | pandoc -f gfm -t rst -o libraries/git.rst
sed '1,19d' ../../../library/glicko2_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/glicko2_ranker.rst
sed '1,19d' ../../../library/glicko2_periodic_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/glicko2_periodic_ranker.rst
sed '1,19d' ../../../library/gradient_boosting_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/gradient_boosting_classifier.rst
sed '1,19d' ../../../library/gradient_boosting_regression/NOTES.md | pandoc -f gfm -t rst -o libraries/gradient_boosting_regression.rst
sed '1,19d' ../../../library/grammars/NOTES.md | pandoc -f gfm -t rst -o libraries/grammars.rst
sed '1,19d' ../../../library/graphs/NOTES.md | pandoc -f gfm -t rst -o libraries/graphs.rst
sed '1,19d' ../../../library/gravatar/NOTES.md | pandoc -f gfm -t rst -o libraries/gravatar.rst
sed '1,19d' ../../../library/gsp_pattern_miner/NOTES.md | pandoc -f gfm -t rst -o libraries/gsp_pattern_miner.rst
sed '1,19d' ../../../library/hashes/NOTES.md | pandoc -f gfm -t rst -o libraries/hashes.rst
sed '1,19d' ../../../library/hdbscan_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/hdbscan_clusterer.rst
sed '1,19d' ../../../library/heaps/NOTES.md | pandoc -f gfm -t rst -o libraries/heaps.rst
sed '1,19d' ../../../library/hierarchical_clustering/NOTES.md | pandoc -f gfm -t rst -o libraries/hierarchical_clustering.rst
sed '1,19d' ../../../library/hierarchies/NOTES.md | pandoc -f gfm -t rst -o libraries/hierarchies.rst
sed '1,19d' ../../../library/hmac/NOTES.md | pandoc -f gfm -t rst -o libraries/hmac.rst
sed '1,19d' ../../../library/hodge_rank/NOTES.md | pandoc -f gfm -t rst -o libraries/hodge_rank.rst
sed '1,19d' ../../../library/hook_flows/NOTES.md | pandoc -f gfm -t rst -o libraries/hook_flows.rst
sed '1,19d' ../../../library/hook_objects/NOTES.md | pandoc -f gfm -t rst -o libraries/hook_objects.rst
sed '1,19d' ../../../library/html/NOTES.md | pandoc -f gfm -t rst -o libraries/html.rst
sed '1,19d' ../../../library/http_authenticate/NOTES.md | pandoc -f gfm -t rst -o libraries/http_authenticate.rst
sed '1,19d' ../../../library/http_client/NOTES.md | pandoc -f gfm -t rst -o libraries/http_client.rst
sed '1,19d' ../../../library/http_client_core/NOTES.md | pandoc -f gfm -t rst -o libraries/http_client_core.rst
sed '1,19d' ../../../library/http_cookies/NOTES.md | pandoc -f gfm -t rst -o libraries/http_cookies.rst
sed '1,19d' ../../../library/http_core/NOTES.md | pandoc -f gfm -t rst -o libraries/http_core.rst
sed '1,19d' ../../../library/http_cors/NOTES.md | pandoc -f gfm -t rst -o libraries/http_cors.rst
sed '1,19d' ../../../library/http_digest/NOTES.md | pandoc -f gfm -t rst -o libraries/http_digest.rst
sed '1,19d' ../../../library/http_directory_listing/NOTES.md | pandoc -f gfm -t rst -o libraries/http_directory_listing.rst
sed '1,19d' ../../../library/http_htmx/NOTES.md | pandoc -f gfm -t rst -o libraries/http_htmx.rst
sed '1,19d' ../../../library/http_multipart/NOTES.md | pandoc -f gfm -t rst -o libraries/http_multipart.rst
sed '1,19d' ../../../library/http_parameters/NOTES.md | pandoc -f gfm -t rst -o libraries/http_parameters.rst
sed '1,19d' ../../../library/http_router/NOTES.md | pandoc -f gfm -t rst -o libraries/http_router.rst
sed '1,19d' ../../../library/http_server/NOTES.md | pandoc -f gfm -t rst -o libraries/http_server.rst
sed '1,19d' ../../../library/http_server_core/NOTES.md | pandoc -f gfm -t rst -o libraries/http_server_core.rst
sed '1,19d' ../../../library/http_session/NOTES.md | pandoc -f gfm -t rst -o libraries/http_session.rst
sed '1,19d' ../../../library/http_socket_transport/NOTES.md | pandoc -f gfm -t rst -o libraries/http_socket_transport.rst
sed '1,19d' ../../../library/http_process_transport/NOTES.md | pandoc -f gfm -t rst -o libraries/http_process_transport.rst
sed '1,19d' ../../../library/http_static_files/NOTES.md | pandoc -f gfm -t rst -o libraries/http_static_files.rst
sed '1,19d' ../../../library/http_websocket/NOTES.md | pandoc -f gfm -t rst -o libraries/http_websocket.rst
sed '1,19d' ../../../library/http_websocket_frames/NOTES.md | pandoc -f gfm -t rst -o libraries/http_websocket_frames.rst
sed '1,19d' ../../../library/http_websocket_handshake/NOTES.md | pandoc -f gfm -t rst -o libraries/http_websocket_handshake.rst
sed '1,19d' ../../../library/http_websocket_messages/NOTES.md | pandoc -f gfm -t rst -o libraries/http_websocket_messages.rst
sed '1,19d' ../../../library/http_websocket_service/NOTES.md | pandoc -f gfm -t rst -o libraries/http_websocket_service.rst
sed '1,19d' ../../../library/http_websocket_session/NOTES.md | pandoc -f gfm -t rst -o libraries/http_websocket_session.rst
sed '1,19d' ../../../library/ica_projection/NOTES.md | pandoc -f gfm -t rst -o libraries/ica_projection.rst
sed '1,19d' ../../../library/ids/NOTES.md | pandoc -f gfm -t rst -o libraries/ids.rst
sed '1,19d' ../../../library/ieee_754/NOTES.md | pandoc -f gfm -t rst -o libraries/ieee_754.rst
sed '1,19d' ../../../library/intervals/NOTES.md | pandoc -f gfm -t rst -o libraries/intervals.rst
sed '1,19d' ../../../library/iqr_anomaly_detector/NOTES.md | pandoc -f gfm -t rst -o libraries/iqr_anomaly_detector.rst
sed '1,19d' ../../../library/iso_639/NOTES.md | pandoc -f gfm -t rst -o libraries/iso_639.rst
sed '1,19d' ../../../library/iso_3166/NOTES.md | pandoc -f gfm -t rst -o libraries/iso_3166.rst
sed '1,19d' ../../../library/iso_4217/NOTES.md | pandoc -f gfm -t rst -o libraries/iso_4217.rst
sed '1,19d' ../../../library/iso_9362/NOTES.md | pandoc -f gfm -t rst -o libraries/iso_9362.rst
sed '1,19d' ../../../library/iso_13616/NOTES.md | pandoc -f gfm -t rst -o libraries/iso_13616.rst
sed '1,19d' ../../../library/isolation_forest_anomaly_detector/NOTES.md | pandoc -f gfm -t rst -o libraries/isolation_forest_anomaly_detector.rst
sed '1,19d' ../../../library/java/NOTES.md | pandoc -f gfm -t rst -o libraries/java.rst
sed '1,19d' ../../../library/json/NOTES.md | pandoc -f gfm -t rst -o libraries/json.rst
sed '1,19d' ../../../library/json_graph/NOTES.md | pandoc -f gfm -t rst -o libraries/json_graph.rst
sed '1,19d' ../../../library/json_ld/NOTES.md | pandoc -f gfm -t rst -o libraries/json_ld.rst
sed '1,19d' ../../../library/json_lines/NOTES.md | pandoc -f gfm -t rst -o libraries/json_lines.rst
sed '1,19d' ../../../library/json_patch/NOTES.md | pandoc -f gfm -t rst -o libraries/json_patch.rst
sed '1,19d' ../../../library/json_path/NOTES.md | pandoc -f gfm -t rst -o libraries/json_path.rst
sed '1,19d' ../../../library/json_pointer/NOTES.md | pandoc -f gfm -t rst -o libraries/json_pointer.rst
sed '1,19d' ../../../library/json_rpc/NOTES.md | pandoc -f gfm -t rst -o libraries/json_rpc.rst
sed '1,19d' ../../../library/json_schema/NOTES.md | pandoc -f gfm -t rst -o libraries/json_schema.rst
sed '1,19d' ../../../library/jwt/NOTES.md | pandoc -f gfm -t rst -o libraries/jwt.rst
sed '1,19d' ../../../library/kcenters_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/kcenters_clusterer.rst
sed '1,19d' ../../../library/kemeny_young_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/kemeny_young_ranker.rst
sed '1,19d' ../../../library/kernel_svm_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/kernel_svm_classifier.rst
sed '1,19d' ../../../library/kernel_pca_projection/NOTES.md | pandoc -f gfm -t rst -o libraries/kernel_pca_projection.rst
sed '1,19d' ../../../library/kmeans_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/kmeans_clusterer.rst
sed '1,19d' ../../../library/kmedians_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/kmedians_clusterer.rst
sed '1,19d' ../../../library/kmedoids_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/kmedoids_clusterer.rst
sed '1,19d' ../../../library/kmodes_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/kmodes_clusterer.rst
sed '1,19d' ../../../library/knn_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/knn_classifier.rst
sed '1,19d' ../../../library/knn_distance_anomaly_detector/NOTES.md | pandoc -f gfm -t rst -o libraries/knn_distance_anomaly_detector.rst
sed '1,19d' ../../../library/knn_regression/NOTES.md | pandoc -f gfm -t rst -o libraries/knn_regression.rst
sed '1,19d' ../../../library/kprototypes_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/kprototypes_clusterer.rst
sed '1,19d' ../../../library/ksuid/NOTES.md | pandoc -f gfm -t rst -o libraries/ksuid.rst
sed '1,19d' ../../../library/lasso_regression/NOTES.md | pandoc -f gfm -t rst -o libraries/lasso_regression.rst
sed '1,19d' ../../../library/lda_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/lda_classifier.rst
sed '1,19d' ../../../library/lda_projection/NOTES.md | pandoc -f gfm -t rst -o libraries/lda_projection.rst
sed '1,19d' ../../../library/linda/NOTES.md | pandoc -f gfm -t rst -o libraries/linda.rst
sed '1,19d' ../../../library/linear_algebra/NOTES.md | pandoc -f gfm -t rst -o libraries/linear_algebra.rst
sed '1,19d' ../../../library/linear_regression/NOTES.md | pandoc -f gfm -t rst -o libraries/linear_regression.rst
sed '1,19d' ../../../library/linear_svm_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/linear_svm_classifier.rst
sed '1,19d' ../../../library/listing/NOTES.md | pandoc -f gfm -t rst -o libraries/listing.rst
sed '1,19d' ../../../library/lof_anomaly_detector/NOTES.md | pandoc -f gfm -t rst -o libraries/lof_anomaly_detector.rst
sed '1,19d' ../../../library/logging/NOTES.md | pandoc -f gfm -t rst -o libraries/logging.rst
sed '1,19d' ../../../library/logistic_regression_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/logistic_regression_classifier.rst
sed '1,19d' ../../../library/loops/NOTES.md | pandoc -f gfm -t rst -o libraries/loops.rst
sed '1,19d' ../../../library/massey_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/massey_ranker.rst
sed '1,19d' ../../../library/mcp_server/NOTES.md | pandoc -f gfm -t rst -o libraries/mcp_server.rst
sed '1,19d' ../../../library/memcached/NOTES.md | pandoc -f gfm -t rst -o libraries/memcached.rst
sed '1,19d' ../../../library/message_pack/NOTES.md | pandoc -f gfm -t rst -o libraries/message_pack.rst
sed '1,19d' ../../../library/meta/NOTES.md | pandoc -f gfm -t rst -o libraries/meta.rst
sed '1,19d' ../../../library/meta_compiler/NOTES.md | pandoc -f gfm -t rst -o libraries/meta_compiler.rst
sed '1,19d' ../../../library/mime_types/NOTES.md | pandoc -f gfm -t rst -o libraries/mime_types.rst
sed '1,19d' ../../../library/modified_z_score_anomaly_detector/NOTES.md | pandoc -f gfm -t rst -o libraries/modified_z_score_anomaly_detector.rst
sed '1,19d' ../../../library/mutations/NOTES.md | pandoc -f gfm -t rst -o libraries/mutations.rst
sed '1,19d' ../../../library/multisets/NOTES.md | pandoc -f gfm -t rst -o libraries/multisets.rst
sed '1,19d' ../../../library/nanoid/NOTES.md | pandoc -f gfm -t rst -o libraries/nanoid.rst
sed '1,19d' ../../../library/naive_bayes_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/naive_bayes_classifier.rst
sed '1,19d' ../../../library/nearest_centroid_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/nearest_centroid_classifier.rst
sed '1,19d' ../../../library/nested_dictionaries/NOTES.md | pandoc -f gfm -t rst -o libraries/nested_dictionaries.rst
sed '1,19d' ../../../library/nmea/NOTES.md | pandoc -f gfm -t rst -o libraries/nmea.rst
sed '1,19d' ../../../library/nmf_projection/NOTES.md | pandoc -f gfm -t rst -o libraries/nmf_projection.rst
sed '1,19d' ../../../library/open_api/NOTES.md | pandoc -f gfm -t rst -o libraries/open_api.rst
sed '1,19d' ../../../library/open_ai/NOTES.md | pandoc -f gfm -t rst -o libraries/open_ai.rst
sed '1,19d' ../../../library/open_id/NOTES.md | pandoc -f gfm -t rst -o libraries/open_id.rst
sed '1,19d' ../../../library/optics_clusterer/NOTES.md | pandoc -f gfm -t rst -o libraries/optics_clusterer.rst
sed '1,19d' ../../../library/optionals/NOTES.md | pandoc -f gfm -t rst -o libraries/optionals.rst
sed '1,19d' ../../../library/options/NOTES.md | pandoc -f gfm -t rst -o libraries/options.rst
sed '1,19d' ../../../library/os/NOTES.md | pandoc -f gfm -t rst -o libraries/os.rst
sed '1,19d' ../../../library/otp/NOTES.md | pandoc -f gfm -t rst -o libraries/otp.rst
sed '1,19d' ../../../library/partitions/NOTES.md | pandoc -f gfm -t rst -o libraries/partitions.rst
sed '1,19d' ../../../library/pattern_mining_protocols/NOTES.md | pandoc -f gfm -t rst -o libraries/pattern_mining_protocols.rst
sed '1,19d' ../../../library/pca_projection/NOTES.md | pandoc -f gfm -t rst -o libraries/pca_projection.rst
sed '1,19d' ../../../library/permutations/NOTES.md | pandoc -f gfm -t rst -o libraries/permutations.rst
sed '1,19d' ../../../library/plackett_luce_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/plackett_luce_ranker.rst
sed '1,19d' ../../../library/plackett_luce_last_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/plackett_luce_last_ranker.rst
sed '1,19d' ../../../library/pls_projection/NOTES.md | pandoc -f gfm -t rst -o libraries/pls_projection.rst
sed '1,19d' ../../../library/prefix_span_pattern_miner/NOTES.md | pandoc -f gfm -t rst -o libraries/prefix_span_pattern_miner.rst
sed '1,19d' ../../../library/probabilistic_pca_projection/NOTES.md | pandoc -f gfm -t rst -o libraries/probabilistic_pca_projection.rst
sed '1,19d' ../../../library/process/NOTES.md | pandoc -f gfm -t rst -o libraries/process.rst
sed '1,19d' ../../../library/protobuf/NOTES.md | pandoc -f gfm -t rst -o libraries/protobuf.rst
sed '1,19d' ../../../library/qda_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/qda_classifier.rst
sed '1,19d' ../../../library/queues/NOTES.md | pandoc -f gfm -t rst -o libraries/queues.rst
sed '1,19d' ../../../library/random/NOTES.md | pandoc -f gfm -t rst -o libraries/random.rst
sed '1,19d' ../../../library/random_forest_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/random_forest_classifier.rst
sed '1,19d' ../../../library/random_forest_regression/NOTES.md | pandoc -f gfm -t rst -o libraries/random_forest_regression.rst
sed '1,19d' ../../../library/random_projection/NOTES.md | pandoc -f gfm -t rst -o libraries/random_projection.rst
sed '1,19d' ../../../library/rank_centrality/NOTES.md | pandoc -f gfm -t rst -o libraries/rank_centrality.rst
sed '1,19d' ../../../library/ranked_pairs/NOTES.md | pandoc -f gfm -t rst -o libraries/ranked_pairs.rst
sed '1,19d' ../../../library/ranking_protocols/NOTES.md | pandoc -f gfm -t rst -o libraries/ranking_protocols.rst
sed '1,19d' ../../../library/reader/NOTES.md | pandoc -f gfm -t rst -o libraries/reader.rst
sed '1,19d' ../../../library/recorded_database/NOTES.md | pandoc -f gfm -t rst -o libraries/recorded_database.rst
sed '1,19d' ../../../library/redis/NOTES.md | pandoc -f gfm -t rst -o libraries/redis.rst
sed '1,19d' ../../../library/regression_protocols/NOTES.md | pandoc -f gfm -t rst -o libraries/regression_protocols.rst
sed '1,19d' ../../../library/regression_tree/NOTES.md | pandoc -f gfm -t rst -o libraries/regression_tree.rst
sed '1,19d' ../../../library/regularized_bradley_terry_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/regularized_bradley_terry_ranker.rst
sed '1,19d' ../../../library/rest/NOTES.md | pandoc -f gfm -t rst -o libraries/rest.rst
sed '1,19d' ../../../library/ridge_regression/NOTES.md | pandoc -f gfm -t rst -o libraries/ridge_regression.rst
sed '1,19d' ../../../library/s3/NOTES.md | pandoc -f gfm -t rst -o libraries/s3.rst
sed '1,19d' ../../../library/schulze_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/schulze_ranker.rst
sed '1,19d' ../../../library/sequential_pattern_mining_protocols/NOTES.md | pandoc -f gfm -t rst -o libraries/sequential_pattern_mining_protocols.rst
sed '1,19d' ../../../library/sets/NOTES.md | pandoc -f gfm -t rst -o libraries/sets.rst
sed '1,19d' ../../../library/sgd_classifier/NOTES.md | pandoc -f gfm -t rst -o libraries/sgd_classifier.rst
sed '1,19d' ../../../library/simulated_annealing/NOTES.md | pandoc -f gfm -t rst -o libraries/simulated_annealing.rst
sed '1,19d' ../../../library/snowflakeid/NOTES.md | pandoc -f gfm -t rst -o libraries/snowflakeid.rst
sed '1,19d' ../../../library/sockets/NOTES.md | pandoc -f gfm -t rst -o libraries/sockets.rst
sed '1,19d' ../../../library/spade_pattern_miner/NOTES.md | pandoc -f gfm -t rst -o libraries/spade_pattern_miner.rst
sed '1,19d' ../../../library/statistics/NOTES.md | pandoc -f gfm -t rst -o libraries/statistics.rst
sed '1,19d' ../../../library/stemming/NOTES.md | pandoc -f gfm -t rst -o libraries/stemming.rst
sed '1,19d' ../../../library/stomp/NOTES.md | pandoc -f gfm -t rst -o libraries/stomp.rst
sed '1,19d' ../../../library/string_distance/NOTES.md | pandoc -f gfm -t rst -o libraries/string_distance.rst
sed '1,19d' ../../../library/strings/NOTES.md | pandoc -f gfm -t rst -o libraries/strings.rst
sed '1,19d' ../../../library/subsequences/NOTES.md | pandoc -f gfm -t rst -o libraries/subsequences.rst
sed '1,19d' ../../../library/term_io/NOTES.md | pandoc -f gfm -t rst -o libraries/term_io.rst
sed '1,19d' ../../../library/thurstone_mosteller_ranker/NOTES.md | pandoc -f gfm -t rst -o libraries/thurstone_mosteller_ranker.rst
sed '1,19d' ../../../library/time_scales/NOTES.md | pandoc -f gfm -t rst -o libraries/time_scales.rst
sed '1,19d' ../../../library/timeout/NOTES.md | pandoc -f gfm -t rst -o libraries/timeout.rst
sed '1,19d' ../../../library/tle_orbits/NOTES.md | pandoc -f gfm -t rst -o libraries/tle_orbits.rst
sed '1,19d' ../../../library/toml/NOTES.md | pandoc -f gfm -t rst -o libraries/toml.rst
sed '1,19d' ../../../library/toon/NOTES.md | pandoc -f gfm -t rst -o libraries/toon.rst
sed '1,19d' ../../../library/truncated_svd_projection/NOTES.md | pandoc -f gfm -t rst -o libraries/truncated_svd_projection.rst
sed '1,19d' ../../../library/tsv/NOTES.md | pandoc -f gfm -t rst -o libraries/tsv.rst
sed '1,19d' ../../../library/types/NOTES.md | pandoc -f gfm -t rst -o libraries/types.rst
sed '1,19d' ../../../library/tzif/NOTES.md | pandoc -f gfm -t rst -o libraries/tzif.rst
sed '1,19d' ../../../library/ulid/NOTES.md | pandoc -f gfm -t rst -o libraries/ulid.rst
cat ../../../library/unicode_data/README.md | pandoc -f gfm -t rst -o libraries/unicode_data.rst
sed '1,19d' ../../../library/union_find/NOTES.md | pandoc -f gfm -t rst -o libraries/union_find.rst
sed '1,19d' ../../../library/url/NOTES.md | pandoc -f gfm -t rst -o libraries/url.rst
sed '1,19d' ../../../library/uuid/NOTES.md | pandoc -f gfm -t rst -o libraries/uuid.rst
sed '1,19d' ../../../library/validations/NOTES.md | pandoc -f gfm -t rst -o libraries/validations.rst
sed '1,19d' ../../../library/wkt_wkb/NOTES.md | pandoc -f gfm -t rst -o libraries/wkt_wkb.rst
sed '1,19d' ../../../library/yaml/NOTES.md | pandoc -f gfm -t rst -o libraries/yaml.rst
sed '1,19d' ../../../library/z_score_anomaly_detector/NOTES.md | pandoc -f gfm -t rst -o libraries/z_score_anomaly_detector.rst
sed '1,19d' ../../../library/zippers/NOTES.md | pandoc -f gfm -t rst -o libraries/zippers.rst

for file in libraries/*.rst; do
	base="${file##*/}"
	if [ "$base" != "index.rst" ] && [ "$base" != "overview.rst" ] && [ "$base" != "core.rst" ] ; then
		name="${base%.*}"
		echo ".. _library_$name:" > temp0 && echo >> temp0 && cat temp0 "$file" > temp1 && mv temp1 "$file"
	fi
done
rm -f temp0

sed '1,24d' ../../../ports/fcube/NOTES.md | pandoc -f gfm -t rst -o ports/fcube.rst
sed '1,35d' ../../../ports/metagol/NOTES.md | pandoc -f gfm -t rst -o ports/metagol.rst
sed '1,22d' ../../../ports/toychr/NOTES.md | pandoc -f gfm -t rst -o ports/toychr.rst

for file in ports/*.rst; do
	base="${file##*/}"
	if [ "$base" != "index.rst" ] && [ "$base" != "overview.rst" ] ; then
		name="${base%.*}"
		echo ".. _library_$name:" > temp0 && echo >> temp0 && cat temp0 "$file" > temp1 && mv temp1 "$file"
	fi
done
rm -f temp0

cat ../../../contributions/flags/NOTES.md | pandoc -f gfm -t rst -o contributions/flags.rst
sed '1,20d' ../../../contributions/iso8601/NOTES.md | pandoc -f gfm -t rst -o contributions/iso8601.rst
cat ../../../contributions/pddl_parser/README.txt | pandoc -f gfm -t rst -o contributions/pddl_parser.rst
cat ../../../contributions/verdi_neruda/README.txt | pandoc -f gfm -t rst -o contributions/verdi_neruda.rst
sed '1,19d' ../../../contributions/xml_parser/NOTES.md | pandoc -f gfm -t rst -o contributions/xml_parser.rst

for file in contributions/*.rst; do
	base="${file##*/}"
	if [ "$base" != "index.rst" ] && [ "$base" != "overview.rst" ] ; then
		name="${base%.*}"
		echo ".. _library_$name:" > temp0 && echo >> temp0 && cat temp0 "$file" > temp1 && mv temp1 "$file"
	fi
done
rm -f temp0

make html
make epub
make info
make latexpdf
make singlehtml
#make linkcheck

# Handle sed differences between GNU and BSD
case $(sed --help 2>&1) in
	*GNU*) sed_i () { sed -i "$@"; };;
	*) sed_i () { sed -i '' "$@"; };;
esac

version_base=$(cat ../../../VERSION.txt | cut -f1 -d"-")
pandoc _build/singlehtml/index.html --wrap=none -t gfm-raw_html -o _build/singlehtml/TheLogtalkHandbook-$version_base.md

# Remove heading link references from the Markdown file
sed_i -E 's/\[.\]\(#[-a-z0-9]+ "Link to this heading"\)//g' _build/singlehtml/TheLogtalkHandbook-$version_base.md
# Remove other links leaving only the text
sed_i -E 's/\[([^]]+)\]\([^)]+\)/\1/g' _build/singlehtml/TheLogtalkHandbook-$version_base.md

rm -f _build/html/index_latexpdf.html
mv -f _build/html/* ../
rm -f ../_sources/index_latexpdf.rst.txt
mv -f _build/latex/TheLogtalkHandbook*.pdf ../
mv -f _build/epub/TheLogtalkHandbook*.epub ../
mv -f _build/texinfo/TheLogtalkHandbook*.info ../
mv -f _build/singlehtml/TheLogtalkHandbook*.md ../

make clean
