#!/bin/bash
# Default values
main_folder="poni"
main="red"
second="orange"
third="black"
background="white"
n_colors_dither="3"
footer=""
removeHtml=false
removeAll=false
# Parse command line options
while getopts ":f:1:2:3:d:b:F:rR" opt; do
case ${opt} in
f )
main_folder="$OPTARG"
;;
1 )
main="$OPTARG"
;;
2 )
second="$OPTARG"
;;
3 )
third="$OPTARG"
;;
d )
n_colors_dither="$OPTARG"
;;
b )
background="$OPTARG"
;;
F )
footer="$OPTARG"
;;
r )
removeHtml=true
;;
R )
removeAll=true
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit 1
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
css='
body {
width: 100%;
height: 100%;
margin: 0;
font-family: 'arial';
background-color: var(--bck)
}
h1{
font-weight: 100;
text-align: center;
color: var(--main);
font-style: italic;
font-size: 3rem;
margin: 1rem;
}
h2{
color: var(--second);
}
a{
text-align: center;
color: var(--second);
}
li a{
color: var(--third);
}
header,footer p{
align-items: center;
display: flex;
justify-content: space-between;
width: 80%;
margin-left: 10%;
color: var(--second);
font-size: 2rem
}
footer{
border-top: solid var(--main);
bottom: 0;
position: fixed;
margin-top: 2rem;
font-size: 1rem;
padding: 1rem;
background-color: var(--bck);
width: 100%
}
footer p{
font-size: 1rem;
color: var(--third);
display: block;
}
.footer a{
display: inline;
}
.exception{
color: var(--main)
}
hr{
color: var(--main);
width: 100% !important;
}
.go{
max-width: 75%;
gap: 2rem;
display: flex;
flex-wrap: wrap;
line-height: .5;
margin-top: 1rem;
}
.go a{
margin-right: 1.5rem
}
.contents{
margin-bottom: 10%;
width: 80%;
margin-left: 10%;
display: grid;
grid-template-columns: auto auto auto;
}
.contents div{
padding: 2rem;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
img, video {
width: 100%;
}
@media only screen and (max-width: 600px) {
.contents{
margin-bottom: 50%;
display: flex;
flex-direction: column;
gap: 2rem;
}
.contents div{
gap: .5rem;
}
.go{
font-size: 1rem;
line-height: 1.5;
width: 70%;
display: block;
}
}"
'
# Function to compress and generate HTML links
compress_and_generate_links() {
local folder="$1"
local compressed_folder="compressed"
mkdir -p "$folder"/"$compressed_folder"
declare -a text_files
declare -a picture_files
declare -a video_files
declare -a audio_files
declare -a other_files
for file in "$folder"/*; do
if [ -f "$file" ]; then
case "$file" in
*.txt|*.md|*.html|*.sh|*.py|*.js|*.php|*.TXT|*.MD)
text_files+=("$file")
;;
*.jpg|*.png|*.heic|*.svg|*.JPG|*.PNG|*.HEIC|*.SVG)
picture_files+=("$file")
;;
# |*.gif|*.GIF)
# gif_files+=("$file")
# ;;
*.mp4|*.mov|*.mkv|*.avi|*.MP4)
video_files+=("$file")
;;
*.mp3|*.wav|*.WAV|*.aiff|*.AIFF|*.FLAC|*.MP3|*.flac)
audio_files+=("$file")
;;
*index.html) # Exclude index.html files
continue
;;
*)
other_files+=("$file")
;;
esac
fi
done
# Create index.html file
index_file="$folder/index.html"
echo "
" >> "$index_file"
# Display text files in the subfolder
if [ ${#text_files[@]} -gt 0 ]; then
echo "
" >> "$index_file"
for text_file in "${text_files[@]}"; do
echo "
" >> "$index_file"
done
echo "
" >> "$index_file"
fi
# Compress and display picture files in the subfolder
if [ ${#picture_files[@]} -gt 0 ]; then
# echo "
" >> "$index_file"
for picture_file in "${picture_files[@]}"; do
compressed_file="$folder"/"$compressed_folder/$(basename "$picture_file")_compressed.jpg"
if [ ! -f "$compressed_file" ]; then
if $dither; then
convert "$picture_file" -dither Riemersma -colors $n_colors_dither -quality 85 "$compressed_file"
else
convert "$picture_file" -quality 85 "$compressed_file"
fi
fi
echo "
" >> "$index_file"
done
fi
# Compress and display video files in the subfolder
if [ ${#video_files[@]} -gt 0 ]; then
# echo "
" >> "$index_file"
for video_file in "${video_files[@]}"; do
compressed_file="$folder"/"$compressed_folder/$(basename "$video_file")_compressed.mp4"
if [ ! -f "$compressed_file" ]; then
ffmpeg -i "$video_file" -vf "scale=640:480" -c:v libx264 -crf 23 -c:a aac -strict experimental "$compressed_file" -y
fi
echo "
" >> "$index_file"
done
fi
# Compress and display audio files in the subfolder
if [ ${#audio_files[@]} -gt 0 ]; then
for audio_file in "${audio_files[@]}"; do
compressed_file="$folder"/"$compressed_folder/$(basename "$audio_file")_compressed.mp3"
if [ ! -f "$compressed_file" ]; then
ffmpeg -i "$audio_file" -b:a 96k "$compressed_file" -y
fi
echo "
" >> "$index_file"
done
fi
# Display other files in the subfolder
if [ ${#other_files[@]} -gt 0 ]; then
echo "
Other Files:
" >> "$index_file"
for other_file in "${other_files[@]}"; do
echo "- $other_file
" >> "$index_file"
done
echo "
" >> "$index_file"
fi
# Close HTML tags
echo "
" >> "$index_file"
}
# function to iterate through all subfolders
function iterate_subfolders() {
local folder="$1"
if $removeHtml; then
# Loop through each item in the folder
for item in "$folder"/*; do
if [ -d "$item" ] && [ ! "$(basename "$item")" == "*" ]; then
iterate_subfolders "$item";
echo "$item";
elif [ "$(basename "$item")" == "index.html" ]; then
rm "$item";
fi
done
elif $removeAll; then
# Loop through each item in the folder
for item in "$folder"/*; do
# Check if the item is a directory
if [ -d "$item" ] && [ ! "$(basename "$item")" == "*" ]; then
if [ "$(basename "$item")" == "compressed" ]; then
echo $item;
rm -r "$item"
else
iterate_subfolders "$item"
echo "$item"
fi
elif [ "$(basename "$item")" == "index.html" ]; then
rm "$item"
fi
done
else
# Loop through each item in the folder
for item in "$folder"/*; do
# Check if the item is a directory
if [ -d "$item" ] && [ ! "$(basename "$item")" == "compressed" ] && [ ! "$(basename "$item")" == "*" ]; then
compress_and_generate_links "$item"
iterate_subfolders "$item"
echo "$item"
fi
done
fi
}
# check if the command is supposed to delete stuff or generate stuff
if $removeHtml; then
rm 'index.html'
# Start iterating from the main folder
iterate_subfolders $main_folder
echo "HTML files deleted for each subfolder."
elif $removeAll; then
rm $main_folder/'index.html'
rm -r $main_folder/compressed
# Start iterating from the main folder
iterate_subfolders $main_folder
echo "HTML and compressed files deleted for each subfolder."
else
compress_and_generate_links "$main_folder"
# Start iterating from the main folder
iterate_subfolders $main_folder
echo "HTML files created for each subfolder."
fi