#! /bin/sh
# A simple script to build rocm documentation
# 
# Christian Bayle <bayle@debian.org>
# License: See debian/copyright
#
#TODO
# add --url parameter 
# add --branch parameter
# add --online parameter
# 

usage(){
	cat <<-EOF
$(basename $0) - Build rocm documentation using ROCm sphinx plugin and theme
Dedicated to debian ROCm maintainer as well as user wanting to rebuild 
the doc online.
A ROCm ./docs directory must be in the current directory
The doc is generated in ./build/html directory
You cas use url and branch env var if required

Usage: $(basename $0) [OPTION]

 -h,--help                Show this help
 -u,--updatedata          Update debian/rocm_data
                          for rocm-docs-core package
                          will only apply if debian/rocm_data is found
 -o,--online              Builds documentation with internet access
                          default is no internet access
 -v,--version             Display rocm version
EOF
}

version(){
	cat <<-EOF
rocm-docs-build 0.0.1

Copyright (C) 2025 Christian Bayle <bayle@debian.org>
Written by Christian Bayle
EOF
}

getonline(){
	latest_version=$(curl -s https://raw.githubusercontent.com/ROCm/rocm-docs-core/data/latest_version.txt)
	release_candidate=$(curl -s https://raw.githubusercontent.com/ROCm/rocm-docs-core/data/release_candidate.txt)
	google_site_verification=$(curl -s https://raw.githubusercontent.com/ROCm/rocm-docs-core/data/google_site_verification.txt)
	echo "Get values from internet"
	echo "latest_version=$latest_version"
	echo "release_candidate=$release_candidate"
	echo "google_site_verification=$google_site_verification"
}

update(){
	if [ -f debian/rocm_data ]
	then
		getonline
		echo "latest_version=$latest_version" > debian/rocm_data
		echo "release_candidate=$release_candidate" >> debian/rocm_data
		echo "google_site_verification=$google_site_verification" >> debian/rocm_data
		cat debian/rocm_data
		exit 0
	else 
		echo "debian/rocm_data directory not found"
		usage
		exit 1
	fi
}

patchconf(){
	local version="$1"
	if [ -f docs/conf.py ]
	then
		cat >> docs/conf.py <<-EOF

	#external_projects = ["hipify", "python", "rocm-docs-core", "rocm"]
	# Put here project list with proper inventory
	external_projects = ["python"]

	# La variable 'docs_header_version' est souvent dérivée de ces options
	# ou directement du dictionnaire 'html_context'
	# Bien que moins courant, vous pourriez aussi le définir directement ici
	html_context = {
	    'docs_header_version': '$version'
	}
EOF
	fi
}
gendoc(){
	# Get version
	if [ -f /usr/share/doc/python3-rocm-docs/rocm_data ]
	then 
		. /usr/share/doc/python3-rocm-docs/rocm_data
	fi
	if [ -f debian/rocm_data ]
	then 
		. debian/rocm_data
	fi
	export latest_version release_candidate google_site_verification
	
	# When you set branch=latest you SHOULD get latest_version value in the header
	# With branch=experimental you SHOULD get no version displayed
	# See ./src/rocm_docs/rocm_docs_theme/flavors/rocm/header.jinja
	#branch=experimental
	# The only way to make this work seems to be to do this
	export branch=${branch:-"Debian-Build-$latest_version"}
	
	if [ -d docs ]
	then
		# For rocm-docs-core bootstrap to use itself
		if [ -d "src/rocm_docs" -a -d "debian" -a -d "build/lib" ]
		then
			export PYTHONPATH=.:build/lib:src
			patchconf "Debian Build $latest_version"
		else
			# For other packages make patch optional
			if [ ! -z "$patchconfigpy" ]
			then
				patchconf "Debian Build $latest_version"
			fi
		fi
		# The url contains the url that will allow to get a remote 
		# version of ./src/rocm_docs/data/projects.yaml of the branch "$branch"
		# in offline mode this file should be loaded locally
		export url=${url:-"https://github.com/ROCm/rocm-docs-core"}
		echo "url=$url"
		echo "branch=$branch"
		echo "latest_version=$latest_version"
		echo "release_candidate=$release_candidate"
		latest_version=$latest_version \
		release_candidate=$release_candidate \
		google_site_verification=$google_site_verification \
		ROCM_DOCS_REMOTE_DETAILS="$url,$branch" \
		ROCM_DOCS_NO_GIT="True" \
		python3 -m sphinx -N -b html \
		-D language=en \
		docs/ build/html 
	fi
}

TEMP=$(getopt -o 'huov' --long 'help,update,online,version' -- "$@")

eval set -- "$TEMP"
unset TEMP

while true; do
	case "$1" in
		'-h'|'--help')
			usage
			exit 0
			;;
		'-v'|'--version')
			version
			exit 0
			;;
		'-u'|'--update')
			update
			;;
		'-o'|'--online')
			getonline
			shift
			continue
			;;
		'--')
			shift
			break
			;;
		*)
			echo 'Internal error!' >&2
			exit 1
			;;
	esac
done

if [ -z "$1" ]; then
	gendoc
fi
	
