#! /bin/bash -e
# Last edited on 2026-03-12 00:53:38 by stolfi

# Creates directory "${pdir}" and copies into it the default html source script,
# naming it "${pdir}/${pname}_src.py".

USAGE="${0/*\/} {PAGE_DIR} {PAGE_TYPE} {HTML_NAME} (e.g. 'f079v1 page 010_stitches' or 'nymph-W-2 clip clip')"

if [[ $# -ne 3 ]]; then
  echo "** missing or excess arguments" 1>&2; 
  echo "usage:\n${USAGE}" 1>&2
  exit 1;
fi

pdir="$1"; shift
ptype="$1"; shift
pname="$1"; shift

if [[ ( $# -ne 0 ) || ( "/${pname}" == "/" ) || ("/${ptype}" == "/" ) || ("/${pdir}" == "/" ) ]]; then
  echo "** invalid arguments" 1>&2; 
  echo "usage:\n${USAGE}" 1>&2
  exit 1;
fi

if [[ -s ${pdir} ]]; then 
  echo "!! warning: folder ${pdir} already exists" 1>&2
  # ( ls ${pdir}/*_src.py ${pdir}/*.html 1>&2 2> /dev/null ; echo "" )
fi

if [[ ( -s ${pdir}/${pname}_src.py ) || ( -s ${pdir}/${pname}.html ) ]]; then 
  echo "** page ${pdir}/${pname}.html or its source already exists" 1>&2 ;
  ls ${pdir}/*.html 1>&2
  exit 1
fi

WORK="${HOME}/projects/voynich/work"

mkdir -pv ${pdir}

template="${WORK}/report_default_${ptype}_src.py"

if [[ ! ( -s ${template} ) ]]; then
  echo "** unknown page type '${ptype}'" 1>&2;
  echo "available types:" 1>&2
  ls ${WORK}/report_default_*_src.py \
    | sed -e 's:^.*/::g' -e 's:_src.py::g' \
    1>&2
  exit 1
fi

cp -vL ${template} ${pdir}/${pname}_src.py
chmod a+x ${pdir}/${pname}_src.py
( cd ${pdir} && ln -s ../Makefile )
( cd ${pdir} && ln -s ../work )
