#! /bin/bash

#
## DESCRIPTION:
#    JPEG to MP4 converter.
#    This is only a local "proof of conept" for testing and debugging.
#

#
## DEPENDENCIES: sudo apt|yum install -y ffmpeg
#

#
## FUNCTIONS
#

addtologs() {
	echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a $logs
}
execpause() {
	read -p "Press [ENTER] to continue... "
}
execquite() {
	addtologs "execution time is $(($(date +%s)-$time)) seconds, exit"
	if [ "${show}" != "qn" ];then
		execpause
	fi
	exit
}
execerror() {
	addtologs "error: $1"
	curl -s -X POST $telegramapiurl/sendMessage -d chat_id=$telegramchatid -d text="error: $1" >> /dev/null 2>&1
	execquite
}
getconfig() {
	logs=$(cat $conf | grep "logs=" | cut -d= -f2)
	list=$(cat $conf | grep "list=" | cut -d= -f2)
	imgroot=$(cat $conf | grep "imgroot=" | cut -d= -f2)
	imgnames=($(cat $conf | grep "imgnames=" | cut -d= -f2))
	xscale=$(cat $conf | grep "xscale=" | cut -d= -f2)
	yscale=$(cat $conf | grep "yscale=" | cut -d= -f2)
	mp4fps=$(cat $conf | grep "mp4fps=" | cut -d= -f2)
	telegramapiurl=$(cat $conf | grep "telegramapiurl=" | cut -d= -f2)
	telegramchatid=$(cat $conf | grep "telegramchatid=" | cut -d= -f2)
}

#
## PARAMETERS
#

show=${1}
conf=${2}
if [ -z "${conf}" ] || [ "${conf}" == "-" ];then
	conf="$(dirname $(realpath "$0"))/$(basename -s .sh $0).conf"
fi
when=${3}
if [ -z "$when" ]; then
	d=$(date +"%d")
	w=$(date +"%V")
	m=$(date +"%m")
	y=$(date +"%Y")
	duration=1
	imgpath=$y/$m/$w/$d
	imgname=$y.$m.$d
fi
if [ "$when" == "-d" ]; then
	d=$(date -d "-1 day" +"%d")
	m=$(date +"%m")
	if [ "$(date -d '-1 day' +'%m')" != "$(date +'%m')" ]; then
		m=$(date -d '-1 day' +'%m')
	fi
	y=$(date +"%Y")
	if [ "$(date -d '-1 day' +'%Y')" != "$(date +'%Y')" ]; then
		y=$(date -d '-1 day' +'%Y')
	fi
	w=$(date +"%V")
	if [ "$(date +'%w')" == "1" ]; then
		w=$(date -d "-1 week" +"%V")
	fi
	duration=1
	imgpath=$y/$m/$w/$d
	imgname=$y.$m.$d
fi
if [ "$when" == "-w" ]; then
	w=$(date -d "-1 week" +"%V")
	m=$(date +"%m")
	if [ "$(date -d '-1 week' +'%m')" != "$(date +'%m')" ]; then
		m=$(date -d '-1 week' +'%m')
	fi
	y=$(date +"%Y")
	if [ "$(date -d '-1 week' +'%Y')" != "$(date +'%Y')" ]; then
		y=$(date -d '-1 week' +'%Y')
	fi
	duration=7
	imgpath=$y/*/$w
	imgname=$y-w$w
fi
if [ "$when" == "-m" ]; then
	m=$(date -d "-1 month" +"%m")
	y=$(date +"%Y")
	if [ "$(date -d '-1 month' +'%Y')" != "$(date +'%Y')" ]; then
		y=$(date -d '-1 month' +'%Y')
	fi
	duration=30
	imgpath=$y/$m
	imgname=$y.$m
fi
if [ "$when" == "-y" ]; then
	y=$(date -d "-1 year" +"%Y")
	duration=360
	imgpath=$y
	imgname=$y
fi

#
## VARIABLES
#

time=$(date +%s)
cd "$(dirname "$(realpath "$0")")"
if [ ! -e ${conf} ];then
	execerror "Not found config file: ${conf}"
else
	getconfig
fi
if [ -z "${logs}" ];then
	logs=/dev/null
elif [ ! -e ${logs} ];then
	touch ${logs}
fi
if [ -z "${list}" ];then
	list="$(dirname $(realpath "$0"))/$(basename -s .sh $0).list"
fi
if [ ! -e ${list} ];then
	touch ${list}
fi
if ! command -v ffmpeg &> /dev/null; then
    execerror "Not found dependencies"
fi

#
## RUN
#

for name in ${imgnames[*]}; do
	imgmatch="*$name*.jpeg"
	imgarray=()
	while read FILE; do
		imgarray+=("${FILE}")
	done < <(find $imgroot/$imgpath -name "$imgmatch" | sort)
	imgcount=${#imgarray[*]}

	echo '' > $list
	for item in ${imgarray[*]}; do
		echo file \'$item\' >> $list
	done

	imgdest=$imgroot/$name\_$imgname.mp4
	echo $imgdest
	#ffmpeg -r $imgcount/$duration -f concat -safe 0 -i $list -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -t $duration -c:v libx264 -vf "scale=$xscale:$yscale,fps=$mp4fps,format=yuv420p" -c:a aac $imgdest -y
	ffmpeg -r $imgcount/$duration -f concat -safe 0 -i $list -c:v libx264 -vf "scale=$xscale:$yscale,fps=$mp4fps,format=yuv420p" $imgdest -y \
	&& addtologs "converted $imgcount images to $imgdest with duration $duration" \
	|| execerror "converted $imgcount images to $imgdest with duration $duration"
done
execquite
