#! /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 

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 22050 -ac 1 -b:a 80k "$filename".mp3
done < disfile


# convert the previous file to an even lower quality and speed it up by 20
ffmpeg -i "$(cat disfile)"  -filter:a "atempo=20.0" -vn -ar 8000 -ac 1 -b:a 8k "$(cat disfile | cut -c19- | rev | cut -c5- | rev)"_pooped.mp3

mv *_pooped.mp3 ../noisebot/

rm disartist distitle disalbum disdate discountry

cat rndtrack | cut -c42- | sed 's/[(].*//' >> disartist
cat rndtrack | sed 's/^[^(]*(//'| rev | sed 's/^[^(]*(//' | cut -c3- | rev >> discountry

#ffprobe -v quiet -of json -show_entries format_tags "$(cat rndtrack)" | jq -r .format.tags.artist >> disartist
ffprobe -v quiet -of json -show_entries format_tags "$(cat rndtrack)" | jq -r .format.tags.title >> distitle
ffprobe -v quiet -of json -show_entries format_tags "$(cat rndtrack)" | jq -r .format.tags.album >> disalbum
ffprobe -v quiet -of json -show_entries format_tags "$(cat rndtrack)" | jq -r .format.tags.date >> disdate


poopyTitle="$(cat distitle)"
poopyArtist="$(cat disartist)"
poopyAlbum="$(cat disalbum)"
poopyDate="$(cat disdate)"
poopyCountry="$(cat discountry)"
poopyLink="$(cat dislink)"

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

rm distoot

cat >> distoot <<EOF
Hi, this is $poopyArtist from $poopyCountry with the tune $(cat distitle), from the $(cat disdate) album $(cat disalbum). 
Link: $(cat dislink)
Enjoy,
DISBOT
P.S. Fuck tha police! ACAB 4 LYFE! 1312! 1312!
EOF


toot delete -u amok@ni.hil.ist $myToot
rm oldtoot && toot post -u amok@ni.hil.ist "$(cat distoot)" | cut -c39- >> oldtoot 
#toot pin "$(cat oldtoot)"

./htmlmaker.sh

echo "$(cat dislink)"

