#! /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

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
}

gendoc(){
url=https://github.com/ROCm/ROCm
# 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
branch=latest

# 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
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
	fi
	latest_version=$latest_version \
	release_candidate=$release_candidate \
	google_site_verification=$google_site_verification \
	ROCM_DOCS_REMOTE_DETAILS="$url,$branch" \
	python3 -m sphinx -N -b html \
	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
	
