'''
@author <NAME>
@since 12.8.2019
'''

import os.path
from dulwich.repo import Repo
from dulwich import porcelain
from Classes.GitConstants import *

class GitPages(ServiceBase):

    def __init__(self, phone):

        super().__init__()

        self.phoneNumber = self.SMS_MY_PHONE_NUMBER if self.SMS_DEBUG_MODE else phone.getInstanceNumber().strip("+")
        self.sessName = self.phoneNumber[3:] 

        self.repoBase = self.SMS_WORKDIR+"/tkeske.github.io"

        repoNumbers = repoBase + "/numbers/"

        self.numFile = repoNumbers+self.phoneNumber+".html"

        #git protocol for fast read access only
        self.fullGitUrl = "git://github.com/tkeske/tkeske.github.io"

        if self._isFirstRun():

            self.repo = porcelain.clone(self.fullGitUrl, self.SMS_WORKDIR)

        else:

            self.repo = Repo(self.repoBase)
            porcelain.pull(self.repo, self.fullGitUrl)

        if not self._numFileExists():

            self.commitToMaster()


    def _isFirstRun(self):

        if os.path.exists(self.repoBase):
            return False

        return True

    def _numFileExists(self):

        return os.path.exists(self.numFile)

    def _writeNumberPage(self):

        with open(self.repoBase+"/templates/template.txt", "r") as t, open(self.numFile, "w") as o:
            templStr = t.read()

            checkWords = ("##PHONE_NUMBER##","##SESS_NAME##")
            repWords = (self.phoneNumber, self.sessName)

            for check, rep in zip(checkWords, repWords):
                templStr = templStr.replace(check, rep)

            o.write(templStr)

    def commitToMaster(self):

        self._writeNumberPage()

        porcelain.add(self.repo, paths=None)

        #we need bytes prefix for commit method
        author = b"tkeske <<EMAIL>>"

        porcelain.commit(self.repo, b"Adding inbox page for number: "+self.phoneNumber, author, author)
        
        #https protocol for push with authentication
        porcelain.push(self.repo, 
            "https://"+GIT_USERNAME+":"+GIT_PASSWORD+"@github.com/tkeske/tkeske.github.io.git",
            "master",
            outstream=self.SMS_WORKDIR+"/stdout.txt", 
            errstream=self.SMS_WORKDIR+"/stderr.txt")
'''
@author <NAME>
@since 12.8.2019
'''

import os.path
from dulwich.repo import Repo
from dulwich import porcelain
from Classes.GitConstants import *

class GitPages(ServiceBase):

    def __init__(self, phone):

        super().__init__()

        self.phoneNumber = self.SMS_MY_PHONE_NUMBER if self.SMS_DEBUG_MODE else phone.getInstanceNumber().strip("+")
        self.sessName = self.phoneNumber[3:] 

        self.repoBase = self.SMS_WORKDIR+"/tkeske.github.io"

        repoNumbers = repoBase + "/numbers/"

        self.numFile = repoNumbers+self.phoneNumber+".html"

        #git protocol for fast read access only
        self.fullGitUrl = "git://github.com/tkeske/tkeske.github.io"

        if self._isFirstRun():

            self.repo = porcelain.clone(self.fullGitUrl, self.SMS_WORKDIR)

        else:

            self.repo = Repo(self.repoBase)
            porcelain.pull(self.repo, self.fullGitUrl)

        if not self._numFileExists():

            self.commitToMaster()


    def _isFirstRun(self):

        if os.path.exists(self.repoBase):
            return False

        return True

    def _numFileExists(self):

        return os.path.exists(self.numFile)

    def _writeNumberPage(self):

        with open(self.repoBase+"/templates/template.txt", "r") as t, open(self.numFile, "w") as o:
            templStr = t.read()

            checkWords = ("##PHONE_NUMBER##","##SESS_NAME##")
            repWords = (self.phoneNumber, self.sessName)

            for check, rep in zip(checkWords, repWords):
                templStr = templStr.replace(check, rep)

            o.write(templStr)

    def commitToMaster(self):

        self._writeNumberPage()

        porcelain.add(self.repo, paths=None)

        #we need bytes prefix for commit method
        author = b"tkeske <<EMAIL>>"

        porcelain.commit(self.repo, b"Adding inbox page for number: "+self.phoneNumber, author, author)
        
        #https protocol for push with authentication
        porcelain.push(self.repo, 
            "https://"+GIT_USERNAME+":"+GIT_PASSWORD+"@github.com/tkeske/tkeske.github.io.git",
            "master",
            outstream=self.SMS_WORKDIR+"/stdout.txt", 
            errstream=self.SMS_WORKDIR+"/stderr.txt")
@author <NAME> @since 12.8.2019 #git protocol for fast read access only #PHONE_NUMBER##","##SESS_NAME##") #we need bytes prefix for commit method #https protocol for push with authentication