@echo off

:--- Pull content submodule ---
rem cd content
rem git checkout .
rem git pull
rem cd ..

:=== Parameters ===

:--- Extract version number ---
python get_version.py PlayStream.py >version.txt
cat version.txt
rem pause
set /p ver=<version.txt
echo Version: %ver%
pause

:--- Set variables ---
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 feed_dir=q:\web\feed\

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

:=== Create 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_locale.py
plugin.png
skin.xml
%prog%.py
%prog%.png
key_menu.png
key_exit.png
VideoDownload.py
enigma2_api.py
content\__init__.py
content\ContentSources.py
content\Downloader.py
content\resolver.py
content\util.py
content\icon.gif
content\run.py
content\playstreamproxy.py
content\offline.mp4
content\demjson.py
content\sources\__init__.py
content\sources\SourceBase.py
content\sources\cinemalive.py
content\sources\config.py
content\sources\euronews.py
content\sources\filmix.py
content\sources\filmon.py
content\sources\iplayer.py
content\sources\kinofilmnet.py
content\sources\movieplace.py
content\sources\ltc.py
content\sources\mtgplay.py
content\sources\play24.py
content\sources\replay.py
content\sources\lmt.py
content\sources\serialguru.py
content\sources\tvdom.py
content\sources\ustvnow.py
content\sources\viaplay.py
content\sources\YouTubeVideoUrl.py
content\sources\jsinterp.py
content\sources\swfinterp.py
content\sources\streams.cfg
content\resolvers\__init__.py
content\resolvers\aadecode.py
content\resolvers\hqqresolver.py
content\resolvers\openload3.py
content\resolvers\hdgo.py
content\resolvers\kapnob.py
content\resolvers\kodik.py
content\resolvers\cloudsany.py
content\resolvers\youtuberesolver.py
locale\*.*
) do echo f | xcopy /y /q %%f %ipk_dir%data\%ext_dir%%prog%\%%f
xcopy /y /q content\picons\* %ipk_dir%data\%ext_dir%%prog%\picons\
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%/sources ]; then
echo    rm -rf %ext_dir2%%prog%/*.pyo ^> /dev/null 2^>^&1
echo    rm -rf %ext_dir2%%prog%/resolvers ^> /dev/null 2^>^&1
echo    mkdir %ext_dir2%%prog%/content
echo    mkdir %ext_dir2%%prog%/content/sources
echo    mv %ext_dir2%%prog%/sources/streams.cfg %ext_dir2%%prog%/content/sources/streams.cfg.old
echo    mv %ext_dir2%%prog%/sources/*.cfg %ext_dir2%%prog%/content/sources/
echo    rm -rf %ext_dir2%%prog%/sources ^> /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   rm -rf %ext_dir2%%prog%/*.pyo ^> /dev/null 2^>^&1
echo   rm -rf %ext_dir2%%prog%/content/sources/*.pyo ^> /dev/null 2^>^&1
echo   rm -rf %ext_dir2%%prog%/content/resolvers/*.pyo ^> /dev/null 2^>^&1

echo exit 0
) >%ipk_dir%CONTROL\postinst
dos2unix %ipk_dir%CONTROL\postinst

:=== postrm file ===
(
echo #!/bin/sh
rem echo if [ -e /etc/rc4.d/S50playstreamproxy ]; then
echo   rm /etc/rc4.d/S50playstreamproxy ^> /dev/null 2^>^&1
rem echo fi
echo #!/bin/sh
rem echo if [ -e /etc/rc3.d/S50playstreamproxy ]; then
echo   rm /etc/rc3.d/S50playstreamproxy ^> /dev/null 2^>^&1
rem echo fi
rem echo if [ -e /usr/bin/playstreamproxyy ]; then
echo  rm /usr/bin/playstreamproxy ^> /dev/null 2^>^&1
rem 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%/content/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

:=== Commit/push and copy to feed if message given ===
if not ()==(%1%) (
    git commit -m "%1%"
    git tag -d "%ver%"
    git tag "%ver%"
    git push origin master

    copy %release_dir%%pack_name%_%ver%.ipk %feed_dir%%pack_name%_%ver%.ipk
    pushd  %feed_dir%
    python c:\Python27\Scripts\opkg-make-index . >Packages
    dos2unix Packages
    gzip -f -k Packages
    popd
)
