soapdish/scripts/playlist/playlist_create

67 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# MAIN: CREATE/UPDATE PLAYLIST
# Creates and/or updates a playlist to reflect what's in main-audio_queued folder
# Includes and Declarations (global variables, formatting, and functions)
source "${APPLICATION_ROOT}/scripts/system/system-functions"
source "${APPLICATION_ROOT}/scripts/system/system-declarations"
# If playlist exists, delete it.
if test -f "$PLAYLIST_PATH"; then
sudo rm "$PLAYLIST_PATH";
fi
sudo touch "$PLAYLIST_PATH";
# Create array of directory files
declare -a FILES_ARRAY=()
for FILE in "$AUDIO_QUEUED_DIRECTORY"/*.mp3 ; do
FILES_ARRAY+=("$FILE")
done
# Check if array is empty, then construct accordingly
if (( ${#FILES_ARRAY[@]} )); then
# If files: Append header
CURRENT_DATE=$(date +"%D %T")
echo "# Playlist created date:" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
echo "# $CURRENT_DATE" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
echo "#" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
# Then append tracks
echo "# Playlist tracks:" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
for TRACK in "${FILES_ARRAY[@]}" ; do
echo "$TRACK" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
done
else
# If no files: Append header
CURRENT_DATE=$(date +"%D %T")
echo "# Playlist date created:" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
echo "# $CURRENT_DATE" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
echo "#" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
# Then append empty tracklist
echo "# Playlist tracks:" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
echo "# (((--- PLAYLIST EMPTY ---)))" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
echo "#" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
echo "#" | sudo tee -a "$PLAYLIST_PATH" >/dev/null
error_message
fi
# Set permissions after file creation
sudo chmod 755 "$PLAYLIST_PATH";
sudo chown "$APPLICATION_FILE_OWNER" "$PLAYLIST_PATH";
# Echo result
confirmation_message "[C] Playlist create: Playlist created."
echo ""