#!/bin/bash

# Check if the markdown directory exists
if [ ! -d ~/markdown ]; then
    echo "Error: ~/markdown directory does not exist."
    exit 1
fi

# Check if the public_html directory exists
if [ ! -d ~/public_html ]; then
    echo "Error: ~/public_html directory does not exist."
    exit 1
fi

# Iterate through each markdown file
cd ~/public_html
for file in ~/markdown/*.md; do
    # Get the base filename without the extension
    filename=$(basename "$file" .md)
    
    # Convert markdown to HTML using comrak
    comrak -o ~/public_html/"$filename".html "$file"
    
    # Check if comrak was successful
    if [ $? -ne 0 ]; then
        echo "Error: Failed to convert $file to HTML."
    else
        echo "Converted $file to HTML successfully."
    fi
done

cd ~/markdown
# Copy all files and folders, inlcuding markdown source and data files, to ~/public_html
cp -r * ~/public_html
