#!/bin/bash

# Output file name
output_file="listen-all-audios-guruji-5min-pravachan.mp3"

# Temporary file to store the list of MP3 files
temp_list="file_list.txt"

# Clear the temporary file if it already exists
> "$temp_list"

# List of audio files in the desired order
audio_files=(

'01Guruvani_apne_ko_badliye.mp3'
'02Guruvani_devta.mp3'
'03Guruvani_jeevan_kaise.mp3'
'04Guruvani_chintan.mp3'
'05Guruvani_manan.mp3'
'06Guruvani_upasna.mp3'
'07Guruvani_hans_bane.mp3'
'08Guruvani_utthan_patan.mp3'
'09Guruvani_boya_kata.mp3'
'10Guruvani_samay_dan.mp3'
'11Guruvani_shikshya_vidhya.mp3'
'12Guruvani_samaj.mp3'
'13Guruvani_janamdin.mp3'
'14Guruvani_adhyamikta.mp3'
'15Guruvani_akhand_deep.mp3'
'16Guruvani_isa_masih.mp3'
'17Guruvani_puja_upasna_ke_labh.mp3'
'18Guruvani_puja_upasna.mp3'
'19Guruvani_jivan_kaise_jiyen.mp3'
'20Guruvani_sadhak_kya_karen.mp3'
'21Guruvani_samajik_kranti.mp3'
'22Guruvani_vishesh_samay_me_vishesh_kam.mp3'
'23Guruvani_yug_parivaraan_ki_bela.mp3'
'24Guruvani_vidai.mp3'
'25Guruvani_aastikta-sahi-adhyatm-kya-hai.mp3'
)

# Get the absolute path of the current directory
dir_path=$(pwd)

# Loop through the specified MP3 files and write them to the temporary file with absolute paths
for file in "${audio_files[@]}"; do
    # Check if the file exists
    if [ -f "$file" ]; then
        # Write the absolute path of each file to the temporary file
        echo "file '$dir_path/$file'" >> "$temp_list"
    else
        echo "File $file does not exist."
        exit 1
    fi
done

# Debug: Print the contents of the file list
echo "Contents of $temp_list:"
cat "$temp_list"

# Use ffmpeg to concatenate the MP3 files
ffmpeg -f concat -safe 0 -i "$temp_list" -c copy "$output_file"

# Check if ffmpeg succeeded
if [ $? -eq 0 ]; then
    echo "Concatenation complete. Output file: $output_file"
else
    echo "Failed to concatenate MP3 files."
    # Debug: Print ffmpeg output for more details
    ffmpeg -f concat -safe 0 -i "$temp_list" -c copy "$output_file"
fi

# Clean up: remove the temporary file
rm "$temp_list"

