#!/bin/sh

PROGRAM_NAME=$1
RECORD_SECS=$2 
DEST_DIR=$3/$1`date +_%y%m`

RADIO_ADDR="rtsp://new_iradio.ebs.co.kr:554/iradio/iradiolive_m4a"
CHROOTTARGET=/usr/local/debian-chroot/var/chroottarget
TITLE=$PROGRAM_NAME`date "+ %Y.%_m.%_d."`
TEMP_AAC=/tmp/`date +%H%M%S%N`.m4a
FINAL_AAC=$PROGRAM_NAME`date +_%y%m%d`.m4a

chroot $CHROOTTARGET avconv -rtsp_transport tcp -i $RADIO_ADDR -t $RECORD_SECS -codec:a copy -vn -metadata title="$TITLE" -metadata date=`date +%F` $TEMP_AAC

if [ ! -d "$DEST_DIR" ] ; then
  mkdir -p "$DEST_DIR"
  synoindex -A "$3"
fi
mv $CHROOTTARGET$TEMP_AAC "$DEST_DIR/$FINAL_AAC"
synoindex -a "$DEST_DIR/$FINAL_AAC"
