#!/bin/bash

# Function to clean up HTML and format text
clean_value() {
    sed '
        s/<[^>]*>//g;
        s/&nbsp;/ /g;
        s/&amp;/\&/g;
        s/&lt;/</g;
        s/&gt;/>/g;
        s/&quot;/"/g;
        s/&#8217;/'\''/g;
        s/&#8216;/'\''/g;
        s/&#8220;/"/g;
        s/&#8221;/"/g;
        s/&#8211;/-/g;
        s/&#8212;/—/g;
        s/&#8230;/.../g;
        s/^[[:space:]]*\/\*[[:space:]]*//g;
        s/^[[:space:]]*\*[[:space:]]*//g;
        s/^[[:space:]]*[A-Za-z][A-Za-z ]*:[[:space:]]*//g;
        s/[[:space:]]*\*\/[[:space:]]*$//g;
        s/^[[:space:]]*//g;
        s/[[:space:]]*$//g
    '
}

# Process each theme directory
for theme_dir in */; do
    # Skip special directories
    if [[ "$theme_dir" == "demo/" || "$theme_dir" == "dev/" || "$theme_dir" == "extinct/" || "$theme_dir" == "inactive/" || "$theme_dir" == "kerr/" ]]; then
        echo "Processing $theme_dir..."
        continue
    fi

    # Check if style.css exists
    if [ ! -f "${theme_dir}style.css" ]; then
        continue
    fi

    # Skip if readme.txt already exists
    if [ -f "${theme_dir}readme.txt" ]; then
        continue
    fi

    echo "Processing $theme_dir..."

    # Extract metadata from style.css
    theme_name=$(grep -i "Theme Name:" "${theme_dir}style.css" | head -n 1 | clean_value)
    theme_uri=$(grep -i "Theme URI:" "${theme_dir}style.css" | head -n 1 | clean_value)
    description=$(grep -i "Description:" "${theme_dir}style.css" | head -n 1 | clean_value)
    author=$(grep -i "Author:" "${theme_dir}style.css" | head -n 1 | clean_value)
    author_uri=$(grep -i "Author URI:" "${theme_dir}style.css" | head -n 1 | clean_value)
    version=$(grep -i "Version:" "${theme_dir}style.css" | head -n 1 | clean_value)
    license=$(grep -i "License:" "${theme_dir}style.css" | head -n 1 | clean_value)
    license_uri=$(grep -i "License URI:" "${theme_dir}style.css" | head -n 1 | clean_value)
    tags=$(grep -i "Tags:" "${theme_dir}style.css" | head -n 1 | clean_value)
    requires=$(grep -i "Requires at least:" "${theme_dir}style.css" | head -n 1 | clean_value)
    tested_up_to=$(grep -i "Tested up to:" "${theme_dir}style.css" | head -n 1 | clean_value)
    requires_php=$(grep -i "Requires PHP:" "${theme_dir}style.css" | head -n 1 | clean_value)

    # Set default license values if not found
    if [ -z "$license" ]; then
        license="GPLv2 or later"
    fi
    if [ -z "$license_uri" ]; then
        license_uri="http://www.gnu.org/licenses/gpl-2.0.html"
    fi

    # Create readme.txt
    cat > "${theme_dir}readme.txt" << EOF
=== $theme_name ===
Contributors: $author
License: $license
License URI: $license_uri
Tags: $tags

== Description ==

$description

== Changelog ==

= $version =
* Initial release on WordPress.com

== Copyright ==

$theme_name WordPress Theme, (C) 2024 $author
$theme_name is distributed under the terms of the GNU GPL.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
EOF

    echo "Created readme.txt for $theme_name"
done