#!/bin/bash

# Dialogs separated by pipes
dialogs="I can't do that.|up|down|left|right|long press|You are in bed looking at the ceiling.|You are in bed looking at wall.|You are in bed looking at your alarm clock.|You got out of bed.  Door is to the left.  Alarm clock in front|The Alarm Clock is on.  Swipe Down to Turn Off, Up to turn on"

# Directory to store .wav files
dir="/home/ok/Desktop/mobile_audio/audio_rpg_demo"

# Convert each dialog into a .wav file
OLDIFS=$IFS
IFS='|' 
for dialog in $dialogs; do
    # Replace spaces and special characters with underscores
    filename=$(echo "$dialog" | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]' | tr ' ' '_')
    
    # Use espeak-ng to convert the dialog into a .wav file
    espeak-ng -w "$dir/$filename.wav" -s 150 -v en-us "$dialog"
done
IFS=$OLDIFS

# Initialize an empty string
files=""

# Loop over all .wav files in the directory
for filename in "$dir"/*.wav; do
    # Get the base name of the file
    base=$(basename "$filename" .wav)
    
    # Append the base name to the string
    if [ -z "$files" ]; then
        files="$base"
    else
        files="$files,$base"
    fi
    
    # Convert the .wav file to base64
    base64string=$(base64 -w0 "$filename")
    
    # Create a .js file with the base64 string as a global variable
    echo "var $base=\"$base64string\";" > "$dir/$base.js"
done

# Print the string
echo "$files"
