#! /bin/bash
# Last edited on 2021-04-22 02:13:28 by jstolfi

src="$1"; shift  # Source file name, relative to current directory.
dir="$1"; shift  # Destination directory, rel to ${HOME}.
txt="$*"         # Line for 00-README.

# Moves the file ${src} to the directory ${dir} and appends the 
# line "${src} ${txt}" to the file  "00-README" in that directory.
# The destination directory and the "00-README"  file are created if needed.

dir="${HOME}/${dir}"   # Full destination dir.
rdf="${dir}/00-README" # 00-README in destination dir.

if [[ ! ( -s ${src} ) ]]; then 
  echo "** no file '${src}'" 1>&2 ; exit 1
else
  if [[ ! ( -d ${dir} ) ]]; then
    echo "!! creating directory '${dir}'" 1>&2 
    mkdir -p ${dir} 
  fi
  if [[ ! ( -s ${rdf} ) ]]; then
    echo "!! creating file '${rdf}'" 1>&2 
    echo '# Last edited on DATE TIME by USER' > ${rdf}
    echo ' ' >> ${rdf}
  fi
  mv -vi ${src} ${dir}/.
  sta="${src##*/}"  # Tail of source file name.
  echo "${sta} ${txt}" >> ${rdf}
fi
