#!/bin/bash
##############################################################################
#
# Copyright (C) 2014 - 2016 x-odos GmbH
#
##############################################################################


function echolog {
	echo "`date +'%m/%d/%Y %H:%M:%S'` - $1"
}

/opt/x-stream/getcoverart

if [ -f /etc/x-stream/automp3 ]; then
  echo "" >> /var/log/mirror.log
  echolog "Starting auto mp3 mirror." >> /var/log/mirror.log
  echo "" >> /var/log/mirror.log
  if [ -f /etc/x-stream/mirror-flac-mp3.running ]; then
	echo "" >> /var/log/mirror.log
	echolog "mp3 mirroring allready in progress." >> /var/log/mirror.log
	echo "" >> /var/log/mirror.log
	if [ ! -f /etc/x-stream/mirror-flac-mp3.again ]; then
		touch /etc/x-stream/mirror-flac-mp3.again
		/opt/x-stream/run-again mirror-flac-mp3.again &
	fi
  else
	echo "" >> /var/log/mirror.log
	echolog "Mirroring FLACs to mp3." >> /var/log/mirror.log
	echo "" >> /var/log/mirror.log
	/usr/local/sbin/mirror-flac-mp3 >> /var/log/mirror.log &
  fi
fi

if [ -f /etc/x-stream/autoaac ]; then
  echo "" >> /var/log/mirror.log
  echolog "Starting auto AAC mirror." >> /var/log/mirror.log
  echo "" >> /var/log/mirror.log
  if [ -f /etc/x-stream/mirror-flac-aac.running ]; then
	echo "" >> /var/log/mirror.log
	echolog "AAC mirroring allready in progress." >> /var/log/mirror.log
	echo "" >> /var/log/mirror.log
	if [ ! -f /etc/x-stream/mirror-flac-aac.again ]; then
		touch /etc/x-stream/mirror-flac-aac.again
		/opt/x-stream/run-again mirror-flac-aac.again &
	fi
  else
	echo "" >> /var/log/mirror.log
	echolog "Mirroring FLACs to AAC." >> /var/log/mirror.log
	echo "" >> /var/log/mirror.log
	/usr/local/sbin/flac2m4a.sh /storage/music/Local/flac /storage/music/m4a >> /var/log/mirror.log &
  fi
fi	



