#!/bin/sh
########
########$1 - name of service -"family" - "premium"
########$2 - version "12" "15"
########$3 - date_time 140405_1305
if [ "$3" != "" ]; then
echo "Start assemble plugins for "$1 "service  Version " $2 "Date is " $3

plugin_code_path='plugin_code'
core_code_path='core_code'
source_code_path='source_code'
#echo $plugin_code_path
#`cd "$plugin_code_path/$1/services"`
services_name=`ls "./$plugin_code_path/$1/services"`
echo "Service _name $services_name"
for i in $services_name
do
index=`xmlstarlet sel -t -v "/dune_plugin_update_info/plugin_version_descriptor/version_index" "../update/plugins/$i/update_info.xml" | tail -n 1`
if [ $index -eq $2 ];then
echo "You already have Version $2"
exit
fi
echo "Assemble $i plugin"
###############################
####----XML_PARSE---###############
`xmlstarlet ed -L -u "/dune_plugin/version" -v "$3" "./$plugin_code_path/$1/services/$i/dune_plugin.xml"`
`xmlstarlet ed -L -u "/dune_plugin/version_index" -v "$2" "./$plugin_code_path/$1/services/$i/dune_plugin.xml"`

`sed -i "s/^version.*$/version = "$2"/" "./$plugin_code_path/$1/services/$i/"$i"_plugin_cookies.properties"`;
tempdir="temp_$i"
`mkdir "./$plugin_code_path/$1/services/$tempdir"`
`cp ./$plugin_code_path/$core_code_path/* ./$plugin_code_path/$1/services/$tempdir/ -a`
`cp ./$plugin_code_path/$1/services/$i/* ./$plugin_code_path/$1/services/$tempdir/ -a`
`cp ./$plugin_code_path/$1/$source_code_path/* ./$plugin_code_path/$1/services/$tempdir/ -a`
`tar -czvf "$2_$i.tgz" -C ./$plugin_code_path/$1/services/$tempdir/ .`
`cp  "$2_$i.tgz" ../update/plugins/"$i"/`
echo "Plugin copy to ../update/plugins/"$i"/"
plugin_version_descriptor="/dune_plugin_update_info/plugin_version_descriptor[last()]"
md5sum=`md5sum "../update/plugins/$i/$2_$i.tgz" | cut -d " " -f1`
size=`du -b "../update/plugins/$i/$2_$i.tgz" | cut -f1`
name=`xmlstarlet sel -t -v "/dune_plugin_update_info/plugin_version_descriptor/caption" "../update/plugins/$i/update_info.xml" | tail -n 1`
##################################################################
xmlstarlet ed -L --subnode "/dune_plugin_update_info" --type elem -n plugin_version_descriptor -v "" --subnode "$plugin_version_descriptor" --type elem -n version_index -v "$2" --subnode "$plugin_version_descriptor" --type elem -n version -v "$3"  --subnode "$plugin_version_descriptor" --type elem -n beta -v "no" --subnode "$plugin_version_descriptor" --type elem -n critical -v "no" --subnode "$plugin_version_descriptor" --type elem -n url -v "http://dune.iptvsys.com/system/update/plugins/$i/"$2"_"$i".tgz" --subnode "$plugin_version_descriptor" --type elem -n md5 -v "$md5sum" --subnode "$plugin_version_descriptor" --type elem -n size -v "$size" --subnode "$plugin_version_descriptor" --type elem -n caption -v "$name" --subnode "$plugin_version_descriptor" --type elem -n require -v "" --subnode "$plugin_version_descriptor/require[last()]" --type elem -n api_version -v "1.1.0" "../update/plugins/$i/update_info.xml"

`rm "./$plugin_code_path/$1/services/$tempdir" -r`
`rm "$2"_"$i".tgz`
echo "$i plugin sucsessfuly assamble!!!"
done
echo "All plugins was assemble!!"
else
echo "You have to add Name of service then version then date ./assamble_plugins.sh family 10 140412_1500"
fi
#ru_plugin_prop_path="$russian_path/$russian_path_plugin_cookies.properties"
#pl_plugin_prop_path="$polish_path/$polish_path_plugin_cookies.properties"
#ru_xml_plugin_prop_path="$russian_path/dune_plugin.xml"
#pl_xml_plugin_prop_path="$polish_path/dune_plugin.xml"

#version="$1"
#assamble_day="$2"
#`sed -i 's/^version.*$/version = '$1'/' "$ru_plugin_prop_path"`;
#`sed -i 's/^version.*$/version = '$1'/' "$pl_plugin_prop_path"`;


#`cat $polish_path/$polish_path_plugin_cookies.properties |tail -n 1 | sed 's/.*= //'`
