#! /bin/bash

# set the folder
myRandom=/lol/hahadata0/NOISE/DIS-BAND_COLLECTION/


# set the current toot ID
myToot="$(cat oldtoot)"


# make temp directory
rm -R tmp && mkdir tmp
touch lol.wav && rm lol.wav
rm rndtrack && rm TEMP_FILE_STORAGE/*.*
rm disfile && rm *.mp3
rm disartist && rm distitle && rm disalbum && rm disdate
rm ../../disbot/*.*
rm dislink
#rm currenttoot

find $myRandom -type f \( -name "*.wav" -or  -name "*.mp4"  -name "*.m4a" -or -name "*.mp3" -or -name "*.flac"-or -name "*.ogg" \) -printf "%p\n" | sed 's/^.\///' | shuf -n 1 >> rndtrack 

toilet rndtrack
cat rndtrack

cp "$(cat rndtrack)" TEMP_FILE_STORAGE/

find TEMP_FILE_STORAGE/ -type f \( -name "*.wav" -or  -name "*.mp4"  -name "*.m4a" -or -name "*.mp3" -or -name "*.flac"-or -name "*.ogg" \) >> disfile

while read filename; do
    ffmpeg -i "$filename" -vn -ar 44100 -ac 2 -b:a 112k "$filename".mp3
done < disfile

ffprobe -v quiet -of json -show_entries format_tags dis.mp3 | jq -r .format.tags.artist >> disartist
ffprobe -v quiet -of json -show_entries format_tags dis.mp3 | jq -r .format.tags.title >> distitle
ffprobe -v quiet -of json -show_entries format_tags dis.mp3 | jq -r .format.tags.album >> disalbum
ffprobe -v quiet -of json -show_entries format_tags dis.mp3 | jq -r .format.tags.date >> disdate

mv "$(cat disfile)" ../../disbot/

#mv dis.mp3 ../../disbot/"$(cat disartist)"_-_"$(cat distitle)".mp3
#filename=disfile
#echo "${filename%.*}"

echo "https://altformeget.online/disbot/""$(ls ../../disbot/)" >> dislink
sed -i "s/ /\%20/g" dislink

toot delete $myToot
rm oldtoot && toot post "$(cat dislink)" | cut -c39- >> oldtoot 
toot pin "$(cat oldtoot)"

echo "$(cat dislink)"

