#!/bin/bash
# Place ce script dans /home/cheewee/public
# Exécute ./generate_index.sh pour créer un index.html à jour

DIR="/home/cheewee/public"
INDEX="$DIR/index.html"

echo "<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Téléchargements Publics</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
table { border-collapse: collapse; width: 100%; max-width: 900px; }
th, td { padding: 8px 10px; border-bottom: 1px solid #ddd; }
th { background: #f2f2f2; text-align: left; }
a { text-decoration: none; color: #1a73e8; }
</style>
</head>
<body>
<h2>Téléchargements Publics</h2>
<table>
<thead><tr><th>Nom</th><th>Taille</th><th>Modifié le</th></tr></thead>
<tbody>" > "$INDEX"

# Parcourt tous les fichiers et dossiers
for f in "$DIR"/*; do
    [ "$f" = "$INDEX" ] && continue
    name=$(basename "$f")
    if [ -d "$f" ]; then
        echo "<tr><td>📁 <a href='$name/' download>$name</a></td><td>-</td><td>$(stat -c '%y' "$f")</td></tr>" >> "$INDEX"
    else
        size=$(stat -c '%s' "$f")
        echo "<tr><td>📄 <a href='$name' download>$name</a></td><td>$size</td><td>$(stat -c '%y' "$f")</td></tr>" >> "$INDEX"
    fi
done

echo "</tbody></table></body></html>" >> "$INDEX"
