@echo off
:=== Parameters ===

if ()==(%1%) (
    set ver=0.5n
	rem echo Please provide version
	rem pause
	rem GOTO:EOF
) else (
    set ver=%1
)

set prog=PlayStream
set pack_name=enigma2-plugin-extensions-playstream
set desc=Play online streams from various sources, mostly Latvian

set ext_dir=usr\lib\enigma2\python\Plugins\Extensions\
set ext_dir2=/usr/lib/enigma2/python/Plugins/Extensions/
set script_dir=usr\script\
set ipk_dir=ipkg\
set release_dir=release\

set AR=\MinGW\bin\ar.exe
set TAR=\MinGW\msys\1.0\bin\tar.exe

:=== data files ===
if exist %ipk_dir% rm -r -f %ipk_dir% 
mkdir %ipk_dir%
for %%f in (
readme.md
changelog.md
__init__.py
plugin.py
plugin.png
%prog%.py
%prog%.png
ContentSources.py
VideoDownload.py
enigma2_api.py
resolver.py
util.py
streamproxy.py
offline.mp4
demjson.py
locale\*.*
sources\__init__.py          
sources\SourceBase.py        
sources\cinemalive.py        
sources\config.py            
sources\euronews.py          
sources\filmix.py            
sources\filmon.py            
sources\iplayer.py           
sources\kinofilmnet.py       
sources\ltc.py               
sources\mtgplay.py           
sources\play24.py            
sources\replay.py            
sources\serialguru.py        
sources\tvdom.py             
sources\ustvnow.py           
sources\viaplay.py           
sources\YouTubeVideoUrl.py   
sources\jsinterp.py          
sources\swfinterp.py
sources\streams.cfg
resolvers\__init__.py
resolvers\aadecode.py
resolvers\hqqresolver.py
resolvers\openload3.py
resolvers\youtuberesolver.py
picons\*.*
) do echo f | xcopy /y /q %%f %ipk_dir%data\%ext_dir%%prog%\%%f

xcopy /y /q playstreamproxy %ipk_dir%data\etc\init.d\

:=== control file ===
mkdir %ipk_dir%CONTROL\
rem xcopy  /v /d /y control %ipk_dir%CONTROL\
echo 2.0 >%ipk_dir%debian-binary
(
echo Version: %ver%
echo Package: %pack_name%
echo Description: %desc%
echo Architecture: all
echo Section:
echo Priority: optional
echo Maintainer: ivars777@gmail.com
echo Homepage:
echo Depends: python-json,python-twisted-web,python-simplejson,python-html,python-zlib,python-requests 
echo Source:
) >%ipk_dir%CONTROL\control
dos2unix %ipk_dir%CONTROL\control

:=== preinst file ===
(
echo #!/bin/sh
:echo if [ -d %ext_dir2%%prog% ]; then
:echo   rm -rf %ext_dir2%%prog%/* ^> /dev/null 2^>^&1
:echo   rm -rf %ext_dir2%%prog%/sources/*.py* ^> /dev/null 2^>^&1
:echo   rm -rf %ext_dir2%%prog%/resolvers/*.py* ^> /dev/null 2^>^&1
:echo fi
echo if [ -e /etc/init.d/playstreamproxy ]; then
echo   rm /etc/init.d/playstreamproxy ^> /dev/null 2^>^&1
echo fi
echo exit 0
) >%ipk_dir%CONTROL\preinst
dos2unix %ipk_dir%CONTROL\preinst

:=== postinst file ===
(
echo #!/bin/sh
echo chmod +x /etc/init.d/playstreamproxy
echo ln -s /etc/init.d/playstreamproxy /etc/rc4.d/S50playstreamproxy
echo ln -s /etc/init.d/playstreamproxy /etc/rc3.d/S50playstreamproxy
echo ln -s /etc/init.d/playstreamproxy /usr/bin/playstreamproxy
echo exit 0
) >%ipk_dir%CONTROL\postinst
dos2unix %ipk_dir%CONTROL\postinst

:=== postrm file ===
(
echo #!/bin/sh
:echo if [ -e /etc/rc4.d/S50playstreamproxy ]; then
echo   rm /etc/rc4.d/S50playstreamproxy ^> /dev/null 2^>^&1
:echo fi
echo #!/bin/sh
:echo if [ -e /etc/rc3.d/S50playstreamproxy ]; then
echo   rm /etc/rc3.d/S50playstreamproxy ^> /dev/null 2^>^&1
:echo fi
:echo if [ -e /usr/bin/playstreamproxyy ]; then
echo  rm /usr/bin/playstreamproxy ^> /dev/null 2^>^&1
:echo fi
echo if [ -e /etc/init.d/playstreamproxy ]; then
echo   rm /etc/init.d/playstreamproxy ^> /dev/null 2^>^&1
echo fi
echo exit 0
) >%ipk_dir%CONTROL\postrm
dos2unix %ipk_dir%CONTROL\postrm

:=== conffiles file ===
(
echo %ext_dir2%%prog%/sources/streams.cfg
) >%ipk_dir%CONTROL\conffiles
dos2unix %ipk_dir%CONTROL\conffiles

:=== create ipk file ===
if not exist %release_dir% mkdir %release_dir%
%TAR% -C ipkg\data --mode=777 -czf ipkg\data.tar.gz .
%TAR% -C ipkg\CONTROL --mode=777 -czf ipkg\control.tar.gz .
if exist %release_dir%%pack_name%_%ver%.ipk del %release_dir%%pack_name%_%ver%.ipk
%AR% -r %release_dir%%pack_name%_%ver% ipkg\debian-binary ipkg\data.tar.gz ipkg\control.tar.gz
@echo on
mv %release_dir%%pack_name%_%ver% %release_dir%%pack_name%_%ver%.ipk
git add %release_dir%%pack_name%_%ver%.ipk

