#!/bin/sh
cwd=${PWD}
if [[ -z $1 ]]; then
   arch=local
else
   arch=$1
   shift
fi
if [[ -z $1 ]]; then
   version=psmp
else
   version=$1
   shift
fi
cp2kexe=${cwd}/../../exe/${arch}/cp2k.${version}
echo "CP2K executable: ${cp2kexe}"
version_string=$(${cp2kexe} -v | head -1)
if [[ -n $(echo ${version_string} | grep Development) ]]; then
   version_string=trunk
else
   version_string=$(echo "${version_string}" | awk '{print $3}')
fi
echo Update CP2K manual: ${version_string}
if [[ -z $1 ]]; then
   mandir=${arch}/${version}
else
   mandir=$1
   shift
fi
echo "CP2K manual path: ${mandir}"
if [[ -s ${cp2kexe} ]]; then
   [[ -d ${mandir} ]] && rm -rf ${mandir}
   mkdir -p ${mandir}
   cp collapsibleList.js cp2k_input.xsl cp2k_manual_logo.png cp2k_manual_logo.svg favicon.png favicon.svg toggle_folding.js xml2htm ../input_editing/vim/vim.xsl ${mandir}
   cd ${mandir}
   ${cp2kexe} --xml
   ./xml2htm ${version_string}
   echo "CP2K manual update done."
   cd ${cwd} >/dev/null
else
   echo "ERROR in $(basename $0): CP2K executable file ${cp2kexe} not found"
fi
