#!/usr/bin/python
# coding: UTF-8
#import sys # モジュール属性 argv を取得するため
import os
import shutil
import commands
import configreader
#import re
#import glob
import time
import hashlib
import status
#japanesenkf.overrideEncodings()

mypath=str(os.path.dirname(os.path.abspath(__file__)))+"/"
def tv2tsmix(pout,ch,time):
    if len(ch)>2:#BS/CSは100とかCS??とかなので3文字以上
        status.setBSCSRecording(status.getBSCSRecording()+1)
    else:
        status.setTERecording(status.getTERecording()+1)
    tv2b25ts(pout+".b25",ch,time)
    if len(ch)>2:#BS/CSは100とかCS??とかなので3文字以上
        status.setBSCSRecording(status.getBSCSRecording()-1)
    else:
        status.setTERecording(status.getTERecording()-1)
    
    b252tsmix(pout+".b25",pout)
    #os.remove(pout+".b25")
    if os.access(pout, os.F_OK):
        try:
            os.remove(pout+".b25")
        except:
            ""
def tv2ts(pout,ch,csch,time):
    ext=os.path.splitext(pout)[1]#ext=".なんたら"
    t=hashlib.md5(pout).hexdigest()
    tempt=os.path.split(pout)
    path=tempt[0]
    if len(ch)>2:#BS/CSは100とかCS??とかなので3文字以上
        status.setBSCSRecording(status.getBSCSRecording()+1)
    else:
        status.setTERecording(status.getTERecording()+1)
    tv2b25ts(pout+".b25",ch,time)
    if len(ch)>2:#BS/CSは100とかCS??とかなので3文字以上
        status.setBSCSRecording(status.getBSCSRecording()-1)
    else:
        status.setTERecording(status.getTERecording()-1)
    b252tsmix(pout+".b25",path+"/"+t+ext)
    tsmix2ts(path+"/"+t+ext,pout,csch)
    #os.remove(path+"/"+t+"."+temp[1])
    if os.access(pout, os.F_OK):
        try:
            os.remove(path+"/"+t+ext)
        except:
            ""

def b252ts(pout,ch,csch):
    ext=os.path.splitext(pout)[1]#ext=".なんたら"
    t=hashlib.md5(pout).hexdigest()
    tempt=os.path.split(pout)
    path=tempt[0]
    b252tsmix(pout+".b25",path+"/"+t+ext)
    tsmix2ts(path+"/"+t+ext,pout,csch)
    if os.access(pout, os.F_OK):
        try:
            os.remove(path+"/"+t+ext)
        except:
            ""

def tv2b25ts(pout,ch,time):
    exe=configreader.getpath('recpt1')
    doexe=exe+' '+ch+' '+time+' \''+pout+'\''
    os.system(doexe)
def tv2b25ts_b25(pout,ch,time):
    exe=configreader.getpath('recpt1')
    doexe=exe+' --b25 '+ch+' '+time+' \''+pout+'\''
    os.system(doexe)
def b252tsmix(pin,pout):
    exe=configreader.getpath('b25')
    doexe=exe.encode('utf-8')+u' \''+pin.encode('utf-8')+u'\' \''+pout.encode('utf-8')+u'\''
    doexe="/usr/bin/nice -n 17 "+doexe
    txt=commands.getoutput(doexe)
def tsmix2ts(pin,pout,csch):#csch=0ならcsの処理をしない
    wine=configreader.getpath('wine')
    exe=configreader.getpath('splitt')
    if csch == "0":
        time.sleep(1)
        shutil.move(pin.encode('utf-8'),pout.encode('utf-8'))
    else:
        csopt='-CS'+str(csch)+" "
        dirname=os.path.dirname(pin)
        filename=os.path.basename(pin)
        fn=os.path.splitext(filename)[0]		
        fileext=os.path.splitext(filename)[1]
        outpath=dirname+"/"+fn+'_CS1('+str(csch)+')'+fileext
        #print dirname+" : "+filename+" : "+fn+" : "+fileext
        path=dirname
        outpath=os.path.join(dirname,fn+'_CS1('+str(csch)+')'+fileext)
        time.sleep(1)
        #doexe=wine+u' '+exe+u' -SD -1SEG -EIT '+csopt+u'-SEPA3 -OUT '+path+'/ '+pin
        doexe=wine+u' '+exe+u' -SD -1SEG -EIT '+csopt+' -OUT '+path.encode('utf-8')+'/ '+pin.encode('utf-8')
        doexe='xvfb-run -a -e xvfberror.txt '+doexe
        doexe="/usr/bin/nice -n 18 "+doexe
        os.system(doexe)
        time.sleep(1)
        for i in xrange(10):
            try:
                shutil.move(outpath.encode('utf-8'),pout.encode('utf-8'))
                break
            except:
                time.sleep(1)
        if os.access(pout, os.F_OK):
            os.remove(pin.encode('utf-8'))
        
