#! /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
 -n,--nopatch             Disable patch on docs/conf.py
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
}

patchconfexternalproject(){
	if ! grep -q '^external_projects = ["python"]' docs/conf.py
	then
	cat >> docs/conf.py <<-EOF

# Put here project list with proper inventory
external_projects = ["python"]
external_projects_remote_repository = ""
EOF
	fi
}

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

# Force version in header
html_context = {
    'docs_header_version': '$version'
}
EOF
			fi
		fi
		if ! grep -q "^external_projects_remote_repository" docs/conf.py
		then
				cat >> docs/conf.py <<-EOF

			# This disable interfinx to download external .inv files
			# But get them from projects.yaml
external_projects_remote_repository = ""
EOF
		fi

	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
			# I patch conf on external because the default 
			# would force circular dependancies on rocm-docs-core rocm and hipify
			patchconfexternalproject
			patchconf "Debian Build $latest_version"
		else
			# For other packages make patch except if nopatch in env
			if [ -z "$nopatch" ]
			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
			;;
		'-n'|'--nopatch')
			export nopatch=True
			shift
			continue
			;;
		'--')
			shift
			break
			;;
		*)
			echo 'Internal error!' >&2
			exit 1
			;;
	esac
done

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