#!/usr/bin/python
# -*- coding:utf-8 -*-

import os
import sys
from subprocess import call
import shutil
import glob
import ziputil

basepath=os.path.dirname(os.path.abspath(__file__))
if not os.path.exists(os.path.join(basepath,"release")):
    os.mkdir(os.path.join(basepath,"release"))

if len(sys.argv)>1:
    version=sys.argv[1]
else:
    print("バージョン番号を入力してください")
    version=sys.stdin.readline()
version=version.rstrip()
if version=="":
    print("バージョン番号がありません\n")
    exit()
#バージョンに合わせたリリースフォルダ作成
vbasepath=os.path.join(basepath,"release",version)
if os.path.exists(vbasepath):
    os.system('rmdir /S /Q "'+vbasepath +'"')
os.mkdir(vbasepath)
#ソースコードをエクスポート
os.system('svn export ../MMDX '+os.path.join('release',version,"MMDX_src"))
#ソースコードを圧縮
ziputil.zipdir(os.path.join('release',version,"MMDX_src"), os.path.join(vbasepath,"MMDX_"+version+"_src.zip"))

#MSBuildパス取得
msbuild=os.environ.get("windir")
msbuild=os.path.join(msbuild,"Microsoft.NET","Framework","v4.0.30319","MSBuild.exe")
#sandcastleプロジェクトパス
projdir=os.path.join(os.path.dirname(basepath),"MMDX")
helpdir=os.path.join(projdir,"HelpFileProject")
#ビルド
call([msbuild,"/p:Configuration=Release",os.path.join(helpdir,"MikuMikuDanceXNA.shfbproj")])
call([msbuild,"/p:Configuration=Release",os.path.join(helpdir,"SlimMMDX.shfbproj")])

#binフォルダ作成
xnawinpath=os.path.join(vbasepath,"MikuMikuDanceXNA")
os.mkdir(xnawinpath)
xnawinbinpath=os.path.join(xnawinpath,"x86")
os.mkdir(xnawinbinpath)
xnawinpipepath=os.path.join(xnawinpath,"pipeline")
os.mkdir(xnawinpipepath)
xnaxboxpath=os.path.join(xnawinpath,"XBox360")
os.mkdir(xnaxboxpath)

slimbinpath=os.path.join(vbasepath,"SlimMMDX_bin")
os.mkdir(slimbinpath)

#ファイルコピー
files=glob.glob(os.path.join(projdir,"MikuMikuDanceXNA","bin","x86","Release","*.dll"))
for dllfile in files:
    shutil.copyfile(dllfile,os.path.join(xnawinbinpath,os.path.basename(dllfile)))
files=glob.glob(os.path.join(projdir,"MikuMikuDanceXNA","bin","Xbox 360","Release","*.dll"))
for dllfile in files:
    shutil.copyfile(dllfile,os.path.join(xnaxboxpath,os.path.basename(dllfile)))
files=glob.glob(os.path.join(projdir,"MMDPipeline","bin","x86","Debug","*.dll"))
for dllfile in files:
    shutil.copyfile(dllfile,os.path.join(xnawinpipepath,os.path.basename(dllfile)))
shutil.copyfile(os.path.join(helpdir,"Help","MikuMikuDanceXNAReference.chm"),os.path.join(xnawinpath,"MikuMikuDanceXNAReference.chm"))
shutil.copyfile(os.path.join(projdir,"readme.txt"),os.path.join(xnawinpath,"readme.txt"))
shutil.copyfile(os.path.join(projdir,"BulletLicense.txt"),os.path.join(xnawinpath,"BulletLicense.txt"))

files=glob.glob(os.path.join(projdir,"SlimMMDX","bin","Release","*.dll"))
for dllfile in files:
    shutil.copyfile(dllfile,os.path.join(slimbinpath,os.path.basename(dllfile)))
shutil.copyfile(os.path.join(helpdir,"Help","SlimMMDXReference.chm"),os.path.join(slimbinpath,"SlimMMDXReference.chm"))
shutil.copyfile(os.path.join(projdir,"readme.txt"),os.path.join(slimbinpath,"readme.txt"))
shutil.copyfile(os.path.join(projdir,"BulletLicense.txt"),os.path.join(xnawinpath,"BulletLicense.txt"))
#サンプルコピー
for i in range(1,7):
    shutil.copytree(os.path.join(projdir,"MikuMikuDanceXNADemo"+str(i),"MikuMikuDanceXNADemo"+str(i),"bin","x86","Release"),os.path.join(xnawinpath,"Demo"+str(i)))
    shutil.copytree(os.path.join(projdir,"SlimMMDXDemo"+str(i),"bin","Release"),os.path.join(slimbinpath,"Demo"+str(i)))

#バイナリ圧縮
ziputil.zipdir(xnawinpath,os.path.join(vbasepath,"MikuMikuDanceXNA_"+version+"_bin.zip"))
ziputil.zipdir(slimbinpath,os.path.join(vbasepath,"SlimMMDX_"+version+"_bin.zip"))

