#!/bin/bash

# Support script to copy the extracted training videos files into the right place.
# The folder structur has to be like the following:
# 
# product (12C or 12R or 16)
#  L language (en or de...)
#      L   licence (cm or ca (or later pa, todo...) ) 
#

. /home/plusoptix/plusoptix/program/functions.sh
SUPPORTDIR=/home/plusoptix/plusoptix/usb
lic=
prod=
lang=
licS=
FINMSG=

PoDialog --no-button --infobox "Training video copy script started\nplease wait..." 0 0 10000
DIAG=$!

# Last function to call on exit
function finish {
    if [ -z "$FINMSG" ]
    then
	FINMSG="Successfully copied training videos for\n${licS}${prod} in language ${lang}."
    fi
    kill $DIAG
    PoDialog --msgbox "${FINMSG}" 0 0
    # restart gui to make the new videos available
    touch ${prgdir}/need_restart
}

trap finish EXIT

# set the lic variable
function setLicence () {
    if isScreener
    then
	lic=ca
	licS=S
    elif isAutoref
    then
	lic=cm
	licS=A
    else
	FINMSG="License still not set - aborting"
	false
    fi
}
# set the prod variable
function setProduct () {
    if isX16
    then
	prod=16
    else
	if [ -f /tmp/poserialnumber ]
	then
	    PRODUCT=$(grep "Product:" /tmp/poserialnumber | cut -d ":" -f 2 | cut -d " " -f2)
	    if [ x$PRODUCT == x"1201" ]
	    then
		prod=12
	    else
		prod=12
	    fi
	else
	    FINMSG="Unable to find /tmp/poserialnumber - aborting"
	    false
	fi
    fi
}
# set the lang variable
function setLang () {
    if [ -f ${prgdir}/PlusoptixQtDesktopGui.cfg ]
    then
	langi=$(grep language ${prgdir}/PlusoptixQtDesktopGui.cfg | cut -f 2 -d "=")
	case $langi in
	    2)
		lang=de
		;;
	    4)
		lang=en
		;;
	    6)
		lang=fr
		;;
	    16)
		lang=ru
		;;
	    # to be continued...
	    *)
		lang=en
		;;
	esac
    else
	FINMSG="Unable to find ${prgdir}/PlusoptixQtDesktopGui.cfg - aborting"
	false
    fi
}

# Stop gui to get config
killall -1 qtopia_gui

# init:
setLicence
setLang
setProduct

# check if either raw file is there or the folder structure (default = ok) else give error message
if ls ${SUPPORTDIR}/*.7z
then
    FINMSG="Training videos are still compressed.\nDownload 7zip application from http://www.7zip.org\n, install it and extract this file with it:\n`ls *.7z`"
    false
elif ls ${SUPPORTDIR}/${prod}/${lang}/${lic}/*.mp4
then
    rm -rf ${prgdir}/videos/*
    mkdir -p ${prgdir}/videos/${prod}/${lang}/${lic}
    rsync -rtv ${SUPPORTDIR}/${prod}/${lang}/${lic}/ ${prgdir}/videos/${prod}/${lang}/${lic}/
elif ls ${SUPPORTDIR}/${prod}/en/${lic}/*.mp4
then
    # original current set language not available.
    rm -rf ${prgdir}/videos/*
    # copy extra audio tracks if available
    if ls ${SUPPORTDIR}/${prod}/${lang}/${lic}/*.aac
    then
	mkdir -p ${prgdir}/videos/${prod}/${lang}/${lic}
	rsync -rtv ${SUPPORTDIR}/${prod}/${lang}/${lic}/ ${prgdir}/videos/${prod}/${lang}/${lic}/
	# no msg for this case. The user gets the right audio track - only the texts at the video are wrong.
    else
	FINMSG="Successfully copied training videos for\n${licS}${prod} in English\n(wanted language not available: $lang)."	
    fi
    lang=en
    # copy just english videos
    mkdir -p ${prgdir}/videos/${prod}/${lang}/${lic}
    rsync -rtv ${SUPPORTDIR}/${prod}/${lang}/${lic}/ ${prgdir}/videos/${prod}/${lang}/${lic}/
else
    FINMSG="No training videos found.\nDownload and extract the following file\n${lang}-${licS}${prod}-[YYYMMDD].7z\nwhich can be found under www.plusoptix.com -> support -> training videos.\nIf you don't know, how to do it, contact plusoptix service."
    false
fi
