#!/bin/bash

INDIR="${HOME}/code/chriscowley-me-uk/content"
OUTDIR="${HOME}/code/cowley-tech/content/blog"

for FILE in $(ls ${INDIR}/*.md ); do
  DATE="$(echo ${FILE} | cut -b 46-55)"
  SLUG="$(echo ${FILE} | cut -b 57- | sed 's/.md//')"
  mkdir -pv "${OUTDIR}/$SLUG"
  OUTFILE="${OUTDIR}/$SLUG/index.md"
  #echo "---\ndate: $DATE\n" > "${OUTDIR}/$SLUG/index.md"
  echo "---" | tee "${OUTFILE}"
  printf "date: %s\n" ${DATE} | tee -a "${OUTFILE}"
  sed -e 's/Title: /title: /g' ${FILE} \
    -e 's/Thumbnail: /featured_image: /g' \
    -e 's/Category: /category: /g' \
    -e '/Slug: /d' \
    -e '/Email: /d' \
    -e '/Tags: /d' \
    -e '/tags: /d' \
    -e '0,/^$/ s/^$/---\n/' \
    | tee -a "${OUTFILE}"
done

