#!/usr/bin/python3

import os
import subprocess
import json
from threading import Thread
import concurrent.futures

import time
import psutil
import shutil

MAX_CHECK_FOLDER_INTERVAL=5
MAX_CHECK_STATUS_INTERVAL=150
MAX_CHECK_FREE_SPACE=150
FREE_SPACE_LIMIT=5368709120


class checkSpaceLocalFolder():

	def __init__(self,*args):

		pass

	#def __init__

	def checkFolder(self,spaceInfo):

		spaceLocalFolder,spaceConfigPath,spaceService,spaceType,self.userSystemdAutoStartPath=spaceInfo
		localFolderEmptyToken=os.path.join(spaceConfigPath,".run/localFolderEmptyToken");
		localFolderRemovedToken=os.path.join(spaceConfigPath,".run/localFolderRemovedToken");
		emptyToken=os.path.join(spaceConfigPath,".run/emptyToken")
		updateRequiredToken=os.path.join(spaceConfigPath,".run/updateRequiredToken")
		updatedToken=os.path.join(spaceConfigPath,".run/updatedToken")
		onedriveVersionToken="/var/lib/lliurex-onedrive/version"
		backupFolder="LLIUREX_ONEDRIVE_BACKUP"

		isRunning=self._isOnedriveRunning(spaceConfigPath)
		if os.path.exists(spaceLocalFolder):
			if spaceType!="onedriveBackup":
				content=os.listdir(spaceLocalFolder)
				if (len(content)>1) or (len(content)>0 and ".directory" not in content):
					self._manageFolderToken(False,False,localFolderRemovedToken,localFolderEmptyToken)
					if os.path.exists(emptyToken):
						os.remove(emptyToken)
				else:
					if not os.path.exists(emptyToken):
						if not os.path.exists(localFolderEmptyToken):
							self._manageFolderToken(False,True,localFolderRemovedToken,localFolderEmptyToken)
						
						if isRunning:
							self._stopClient(spaceConfigPath,spaceService)
					else:
						self._manageFolderToken(False,False,localFolderRemovedToken,localFolderEmptyToken)
			else:
				folderToCheck=os.path.join(spaceLocalFolder,backupFolder)
				if os.path.exists(folderToCheck):
					self._manageFolderToken(False,False,localFolderRemovedToken,localFolderEmptyToken)
					if os.path.exists(emptyToken):
						os.remove(emptyToken)
				else:
					os.mkdir(folderToCheck)
					self._checkBackupDirectory(spaceLocalFolder,backupFolder)
		else:
			if not os.path.exists(emptyToken):
				if not os.path.exists(localFolderRemovedToken):
					self._manageFolderToken(True,False,localFolderRemovedToken,localFolderEmptyToken)
					if isRunning:
						self._stopClient(spaceConfigPath,spaceService)
				else:
					pass
		
		
		self._manageUpdateRequiredToken(updateRequiredToken,onedriveVersionToken,updatedToken)
		self._manageAutoStart(spaceConfigPath,spaceService,localFolderRemovedToken,localFolderEmptyToken,updateRequiredToken)

		return True
	
	#def checkFolder

	def _manageFolderToken(self,remove,empty,localFolderRemovedToken,localFolderEmptyToken):

		if remove:
			if not os.path.exists(localFolderRemovedToken):
				with open(localFolderRemovedToken,'w') as fd:
					pass
		else:
			if os.path.exists(localFolderRemovedToken):
				os.remove(localFolderRemovedToken)

		if empty:
			if not os.path.exists(localFolderEmptyToken):
				with open(localFolderEmptyToken,'w') as fd:
					pass
		else:
			if os.path.exists(localFolderEmptyToken):
				os.remove(localFolderEmptyToken)

	#def _manageFolderToken

	def _stopClient(self,spaceConfigPath,spaceService):

		cmd="systemctl --user stop %s"%spaceService
		try:
			p=subprocess.run(cmd,shell=True,check=True)
		except subprocess.CalledProcessError as e:
			pass

	#def _stopClient

	def _manageUpdateRequiredToken(self,updateRequiredToken,onedriveVersionToken,updatedToken):

		if os.path.exists(onedriveVersionToken):
			with open(onedriveVersionToken,'r') as fd:
				currentVersion=fd.readline().strip()

			if "2.15." in currentVersion or "1.5." in currentVersion:
				if not os.path.exists(updatedToken):
					if not os.path.exists(updateRequiredToken):
						with open(updateRequiredToken,'w') as fd:
							pass
				else:
					if os.path.exists(updateRequiredToken):
						os.remove(updateRequiredToken)

	#def _manageUpdateRequiredToken

	def _manageAutoStart(self,spaceConfigPath,spaceService,localFolderRemovedToken,localFolderEmptyToken,updateRequiredToken):

		lockAutoStartToken=os.path.join(spaceConfigPath,".run/lockAutoStartToken")

		if os.path.exists(os.path.join(self.userSystemdAutoStartPath,spaceService)):
			if os.path.exists(localFolderEmptyToken) or os.path.exists(localFolderRemovedToken) or os.path.exists(updateRequiredToken):
				if not os.path.exists(lockAutoStartToken):
					with open(lockAutoStartToken,'w') as fd:
						pass
					cmd="systemctl --user disable %s"%spaceService
					try:
						p=subprocess.run(cmd,shell=True,check=True)
					except subprocess.CalledProcessError as e:
						pass
			else:
				if os.path.exists(lockAutoStartToken):
					os.remove(lockAutoStartToken)
		else:
		    if not os.path.exists(localFolderEmptyToken) and not os.path.exists(localFolderRemovedToken) and not os.path.exists(updateRequiredToken):
		    	if os.path.exists(lockAutoStartToken):
		    		os.remove(lockAutoStartToken)
		    		cmd="systemctl --user enable %s"%spaceService
		    		try:
		    			p=subprocess.run(cmd,shell=True,check=True)
		    		except subprocess.CalledProcessError as e:
		    			pass

	#def _manageAutoStart

	def _isOnedriveRunning(self,spaceConfigPath=None):

		runToken=os.path.join(spaceConfigPath,".run/runToken")

		if spaceConfigPath!=None:
			onedriveCommand='onedrive --monitor --confdir="%s"'%spaceConfigPath

			if os.system('ps -ef | grep "%s" | grep -v "grep" 1>/dev/null'%onedriveCommand)==0:
				if not os.path.exists(runToken):
					with open(runToken,'w') as fd:
						pass
				return True
			else:
				if os.path.exists(runToken):
					os.remove(runToken)
				return False

		return False

	#def _isOnedriveRunning

	def _checkBackupDirectory(self,spaceFolder,backupFolder):

		directoryFile="/usr/share/lliurex-onedrive/llx-data/directoryOneDriveSync"
		tmpFolder=os.path.join(spaceFolder,backupFolder)
		tmpDirectory=os.path.join(tmpFolder,".directory")

		if not os.path.exists(tmpDirectory):
			shutil.copyfile(directoryFile,tmpDirectory)

		content=os.listdir(spaceFolder)
		for item in content:
			tmpPath=os.path.join(spaceFolder,item)
			if os.path.isdir(tmpPath) and tmpPath!=tmpFolder:
				if os.path.exists(os.path.join(tmpPath,".directory")):
					os.remove(os.path.join(tmpPath,".directory"))
					
	#def _checkBackupDirectory

#class checkSpaceLocalFolder

class getSpaceStatusWorker():

	def __init__(self,*args):

		pass

	#def __init__

	def checkStatus(self,spaceInfo):

		spaceConfigPath,spaceType,spaceLocalFolder=spaceInfo
		localFolderEmptyToken=os.path.join(spaceConfigPath,'.run/localFolderEmptyToken')
		localFolderRemovedToken=os.path.join(spaceConfigPath,".run/localFolderRemovedToken");
		updateRequiredToken=os.path.join(spaceConfigPath,".run/updateRequiredToken")

		MICROSOFT_API_ERROR=-1
		UNABLE_CONNECT_MICROSOFT_ERROR=-2
		LOCAL_FILE_SYSTEM_ERROR=-3
		ZERO_SPACE_AVAILABLE_ERROR=-4
		QUOTA_RESTRICTED_ERROR=-5
		DATABASE_ERROR=-6
		UNAUTHORIZED_ERROR=-7
		UPLOADING_CANCEL_ERROR=-8
		SERVICE_UNAVAILABLE=-9
		FORBIDDEN_USER=-14

		ALL_SYNCHRONIZE_MSG=0
		OUT_OF_SYNC_MSG=2
		WITH_OUT_CONFIG=1
		INFORMATION_NOT_AVAILABLE=3
		UPLOADING_PENDING_CHANGES=4
		UPLOADING_PENDING_CHANGES_BACKUP=5

		error=False
		code=INFORMATION_NOT_AVAILABLE
		freespace=""
		pendingChanges="0 KB"
		lastPendingChanges="0 KB"
		filesPendingUpload=0

		if self._isOnedriveRunning(spaceConfigPath):
			if not os.path.exists(localFolderEmptyToken) and not os.path.exists(localFolderRemovedToken) and not os.path.exists(updateRequiredToken):
				if os.path.join(spaceConfigPath,'refresh_token'):
					lastPendingChanges=self._getLastPendingChanges(spaceConfigPath)
					if spaceType!="onedriveBackup": 
						cmd='/usr/bin/onedrive --display-sync-status --dry-run --verbose --confdir="%s"'%spaceConfigPath
					else:
						cmd='/usr/bin/onedrive --display-sync-status --dry-run --verbose --confdir="%s" --single-directory "LLIUREX_ONEDRIVE_BACKUP"'%spaceConfigPath
	
					p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
					try:
						poutput,perror=p.communicate(timeout=90)

						if type(poutput) is bytes:
							poutput=poutput.decode()

						if type(perror) is bytes:
							perror=perror.decode()

						if len(perror)>0:
							perror=perror.split('\n')
							error=True
							for item in perror:
								if 'OneDrive API returned an error' in item:
									code=MICROSOFT_API_ERROR
								elif 'Cannot connect to' in item:
									code=UNABLE_CONNECT_MICROSOFT_ERROR
									break
								elif 'local file system' in item:
									code=LOCAL_FILE_SYSTEM_ERROR
								elif 'zero space available' in item:
									code=ZERO_SPACE_AVAILABLE_ERROR
									break
								elif 'quota information' in item:
									if spaceType!="sharepoint":
										code=QUOTA_RESTRICTED_ERROR
									else:
										error=False
								elif 'database' in item:
									code=DATABASE_ERROR
								elif 'Unauthorized' in item:
									code=UNAUTHORIZED_ERROR
								elif '416' in item and 'included by' not in item:
									code=UPLOADING_PENDING_CHANGES
									break
								elif 'Unable to query OneDrive' in item:
									code=UNAUTHORIZED_ERROR
									error=True
									break
								elif '503' in item and 'included by' not in item:
									code=SERVICE_UNAVAILABLE
								elif 'Free Space' in item:
									tmp_freespace=item.split(':')[1].strip()
									if not 'Not Available' in tmp_freespace:
										tmp_freespace=tmp_freespace.split(" ")
										freespace="%s %s"%(tmp_freespace[0],tmp_freespace[1])
						if not error:
							if len(poutput)>0:
								poutput=poutput.split('\n')
								for item in poutput:
									if 'OneDrive API returned an error' in item:
										code=MICROSOFT_API_ERROR
										error=True
									elif 'Cannot connect to' in item:
										code=UNABLE_CONNECT_MICROSOFT_ERROR
										error=True
										break
									elif 'local file system' in item:
										code=LOCAL_FILE_SYSTEM_ERROR
										error=True
									elif 'zero space available' in item:
										code=ZERO_SPACE_AVAILABLE_ERROR
										error=True
										break
									elif 'quota information' in item:
										if spaceType!="sharepoint":
											code=QUOTA_RESTRICTED_ERROR
											error=True
									elif 'database access error' in item or 'database cannot be opened' in item:
										code=DATABASE_ERROR
										error=True
									elif 'Unauthorized' in item:
										code=UNAUTHORIZED_ERROR
										error=True
									elif '416' in item and 'included by' not in item:
										code=UPLOADING_PENDING_CHANGES
										error=True
										break
									elif 'Unable to query OneDrive' in item:
										code=UNAUTHORIZED_ERROR
										error=True
										break
									elif '503' in item and 'included by' not in item:
										code=SERVICE_UNAVAILABLE
										error=True
									if not error:
										if 'Uploading' in item and '...done' not in item:
											code=UPLOADING_PENDING_CHANGES
											break;
										if 'no pending' in item:
											code=ALL_SYNCHRONIZE_MSG
										elif 'out of sync' in item:
											code=OUT_OF_SYNC_MSG
										elif 'Authentication scope needs' in item:
											code=FORBIDDEN_USER
											error=True
											break
										elif 'download from Microsoft OneDrive:' in item:
											pendingChanges=item.split(':')[1].strip()
										elif 'Free Space' in item:
											tmp_freespace=item.split(':')[1].strip()
											if not 'Not Available' in tmp_freespace:
												tmp_freespace=tmp_freespace.split(" ")
												freespace="%s %s"%(tmp_freespace[0],tmp_freespace[1])
					except:
						p.kil()
						error=True
						code=UNABLE_CONNECT_MICROSOFT_ERROR
				else:
					error=True
					code=WITH_OUT_CONFIG
		
				if spaceType=="onedriveBackup":
					filesPendingUpload=self._getFilesPendigUpload(spaceLocalFolder)
					if not error:
						if filesPendingUpload>0:
							code=UPLOADING_PENDING_CHANGES_BACKUP
						else:
							#self._removeEmptyFolders(spaceLocalFolder)
							if code==OUT_OF_SYNC_MSG:
								code=ALL_SYNCHRONIZE_MSG
				else:
					if code==OUT_OF_SYNC_MSG:
						if pendingChanges==lastPendingChanges:
							code=ALL_SYNCHRONIZE_MSG
				
				try:
					with open(os.path.join(spaceConfigPath,".run/statusToken"),'w') as fd:
						tmpLine=str(error)+("\n")
						fd.write(tmpLine)
						tmpLine=str(code)+("\n")
						fd.write(tmpLine)
						tmpLine=str(freespace)+("\n")
						fd.write(tmpLine)
						tmpLine=str(pendingChanges)+("\n")
						fd.write(tmpLine)
						tmpLine=str(filesPendingUpload)
						fd.write(tmpLine)
				except:
					pass
		else:
			if os.path.exists(os.path.join(spaceConfigPath,".run/statusToken")):
				os.remove(os.path.join(spaceConfigPath,".run/statusToken"))
		
		return True

	#def checkStatus

	def _isOnedriveRunning(self,spaceConfigPath=None):

		if spaceConfigPath!=None:
			onedriveCommand='onedrive --monitor --confdir="%s"'%spaceConfigPath
			if os.system('ps -ef | grep "%s" | grep -v "grep" 1>/dev/null'%onedriveCommand)==0:
				return True
			else:
				return False

		return False

	#def _isOnedriveRunning

	def _getLastPendingChanges(self,spaceConfigPath=None):

		lastPendingChanges=""
		if spaceConfigPath!=None:
			if os.path.exists(os.path.join(spaceConfigPath,".run/statusToken")):
				with open(os.path.join(spaceConfigPath,".run/statusToken"),'r') as fd:
					lastPendingChanges=fd.readlines()[-1].strip()
		
		return lastPendingChanges

	#def _getLastPendingChanges

	def _getFilesPendigUpload(self,spaceLocalFolder):

		folderToCheck=os.path.join(spaceLocalFolder,"LLIUREX_ONEDRIVE_BACKUP")
		pendingFiles=0

		try:
			if os.path.exists(folderToCheck):
				for base,_,files in os.walk(folderToCheck):
					for item in files:
						if item != ".directory":
							pendingFiles+=1
		except:
			pass

		return pendingFiles

	#def _getFilesPendigUpload

	def _removeEmptyFolders(self,spaceLocalFolder):

		folderToCheck=os.path.join(spaceLocalFolder,"LLIUREX_ONEDRIVE_BACKUP")

		if os.path.exists(folderToCheck):
			for base,dirs,files in os.walk(folderToCheck,topdown=False):
				for directory in dirs:
					dirPath=os.path.join(base,directory)
					if not os.listdir(dirPath):
						try:
							os.rmdir(dirPath)
						except:
							pass

	#def _removeEmptyFolders

#class getSpaceStatusWorker

class LliurexOneDriveAC:

	def __init__(self):

		self.user=os.environ["USER"]
		self.llxOnedriveConfigDir="/home/%s/.config/lliurex-onedrive-config/"%(self.user)
		self.onedriveConfigFile=os.path.join(self.llxOnedriveConfigDir,"onedriveConfig.json")
		self.userSystemdPath="/home/%s/.config/systemd/user"%self.user
		self.userSystemdAutoStartPath=os.path.join(self.userSystemdPath,"default.target.wants")
		self.folderWorker=False
		self.statusWorker=False
		self.freeSpaceWorker=False
		self.lastFolderCheck=MAX_CHECK_FOLDER_INTERVAL
		self.lastStatusCheck=MAX_CHECK_STATUS_INTERVAL
		self.lastFreeSpaceCheck=MAX_CHECK_FREE_SPACE
		self.freeSpaceWarningToken=os.path.join(self.llxOnedriveConfigDir,".run/hddWarningToken")
		self.freeSpaceErrorToken=os.path.join(self.llxOnedriveConfigDir,".run/hddErrorToken")
		self.envConfFiles=[".config.backup",".config.hash"]
		self.fixSpacesConfigFile()
		self.initWorker()

	#def __init__

	def fixSpacesConfigFile(self):

		oneDriveSpaces=self.readOneDriveConfig()
		paramsToCheck=["space_reservation","force_http_11","ip_protocol_version","skip_file"]

		for space in oneDriveSpaces:
			tmpConfigPath=space["configPath"]
			tmpConfigFile=os.path.join(tmpConfigPath,"config")
	
			if os.path.exists(tmpConfigFile):
				changeParam=False
				try:
					with open(tmpConfigFile,'r') as fd:
						lines=fd.readlines()
					fd.close()
					
					for line in lines:
						for param in paramsToCheck:
							if param in line:
								if 'skip_file' in line:
									if '*.partial' not in line or '*.swp' not in line:
										changeParam=True
								else:
									if line.startswith("#"):
										changeParam=True
										break

					if changeParam:
						backupConfigFile=os.path.join(tmpConfigPath,"config_back")
						shutil.copyfile(tmpConfigFile,backupConfigFile)

						with open(tmpConfigFile,'w') as fd:
							for line in lines:
								for param in paramsToCheck:
									if param in line:
										if line.startswith("#"):
											if param=="space_reservation":
												newLine='%s = "5120"\n'%param
											elif param=="force_http_11":
												newLine='%s = "true"\n'%param
											elif param=="ip_protocol_version":
												newLine='%s = "1"\n'%param
											line=newLine
										else:
											if 'skip_file' in line:
												newLine=line.split("=")[1]
												newLine=newLine.strip().replace('"',"")
												if "*.partial" not in line:
													newLine='%s|*.partial'%newLine
												if "*.swp" not in line:
													newLine='%s|*.swp'%newLine
												
												line='%s = "%s"\n'%(param,newLine)
								fd.write(line)

						fd.close()

						if os.path.exists(backupConfigFile):
						 	os.remove(backupConfigFile)

						self._cleanEnvFiles(tmpConfigPath)
					
				except Exception as e:
					if os.path.exists(backupConfigFile):
						shutil.copyfile(backupConfigFile,tmpConfigFile)
						os.remove(backupConfigFile)
					pass

	#def fixSpacesConfigFile

	def _cleanEnvFiles(self,configPath):

		for item in self.envConfFiles:
			tmpFile=os.path.join(configPath,item)
			if os.path.exists(tmpFile):
				os.remove(tmpFile)

	#def _cleanEnvFiles

	def initWorker(self):

		self.folderWorkerThread=Thread(target=self.checkSpaceFolder)
		self.folderWorkerThread.daemon=True
		self.folderWorkerThread.start()
		
		self.statusWorkerThread=Thread(target=self.checkSpaceStatus)
		self.statusWorkerThread.daemon=True
		self.statusWorkerThread.start()

		self.freeSpaceWorkerThread=Thread(target=self.checkFreeSpace)
		self.freeSpaceWorkerThread.daemon=True
		self.freeSpaceWorkerThread.start()
		
	#def initWorker

	def checkSpaceFolder(self):

		while True:
			if not self.folderWorker:
				time.sleep(1)
				self.lastFolderCheck+=1
				if self.lastFolderCheck > MAX_CHECK_FOLDER_INTERVAL:
					#folderQ=Queue()
					self.folderWorker=True
					oneDriveSpaces=self.readOneDriveConfig()
					checkSpaceFolder=checkSpaceLocalFolder()

					with concurrent.futures.ThreadPoolExecutor() as executor:
						futures=[]
						for space in oneDriveSpaces:
							tmp=[]
							tmpInfo=[space["localFolder"],space["configPath"],space["systemd"],space["spaceType"],self.userSystemdAutoStartPath] 
							futures.append(executor.submit(checkSpaceFolder.checkFolder,tmpInfo))

						for future in concurrent.futures.as_completed(futures):
							pass	

						self.folderWorker=False
						self.lastFolderCheck=0
				
				else:
					self.lastFolderCheck+=1

	#def checkSpaceFolder
	
	def checkSpaceStatus(self):

		while True:
			if not self.statusWorker:
				time.sleep(1)
				self.lastStatusCheck+=1
				if self.lastStatusCheck > MAX_CHECK_STATUS_INTERVAL:
					self.statusWorker=True
					oneDriveSpaces=self.readOneDriveConfig()
					checkSpaceStatus=getSpaceStatusWorker()
					
					with concurrent.futures.ThreadPoolExecutor() as executor:
						futures=[]
						for space in oneDriveSpaces:
							tmp=[]
							tmpInfo=[space['configPath'],space['spaceType'],space["localFolder"]]
							futures.append(executor.submit(checkSpaceStatus.checkStatus,tmpInfo))
						
						for future in concurrent.futures.as_completed(futures):
							pass	

					self.statusWorker=False
					self.lastStatusCheck=0
				else:
					self.lastStatusCheck+=1
	
	#def checkSpaceStatus

	def checkFreeSpace(self):

		while True:
			if not self.freeSpaceWorker:
				time.sleep(1)
				self.lastFreeSpaceCheck+=1
				if self.lastFreeSpaceCheck>MAX_CHECK_FREE_SPACE:
					self.freeSpaceWorker=True
					hdd=psutil.disk_usage('/home')
					if hdd.free<=(2*FREE_SPACE_LIMIT):
						if hdd.free<=(FREE_SPACE_LIMIT):
							if os.path.exists(self.freeSpaceErrorToken):
								os.remove(self.freeSpaceErrorToken)
							with open(self.freeSpaceErrorToken,'w') as fd:
								tmpLine=str(hdd.free)+("\n")
								fd.write(tmpLine)
							if os.path.exists(self.freeSpaceWarningToken):
								os.remove(self.freeSpaceWarningToken)
						else:
							if os.path.exists(self.freeSpaceWarningToken):
								os.remove(self.freeSpaceWarningToken)
							with open(self.freeSpaceWarningToken,'w') as fd:
								tmpLine=str(hdd.free)+("\n")
								fd.write(tmpLine)
							if os.path.exists(self.freeSpaceErrorToken):
								os.remove(self.freeSpaceErrorToken)
					else:
						if os.path.exists(self.freeSpaceWarningToken):
							os.remove(self.freeSpaceWarningToken)
						if os.path.exists(self.freeSpaceErrorToken):
							os.remove(self.freeSpaceErrorToken)


					self.freeSpaceWorker=False
					self.lastFreeSpaceCheck=0
				else:
					self.lastFreeSpaceCheck+=1

	#def checkFreeSpace
	
	def readOneDriveConfig(self):

		tmpSpacesList=[]

		if os.path.exists(self.onedriveConfigFile):
			with open(self.onedriveConfigFile,'r') as fd:
				tmpOneDriveConfig=json.load(fd)
				try:
					tmpSpacesList=tmpOneDriveConfig['spacesList']
				except:
					pass

		return tmpSpacesList
	
	#def readOneDriveConfig

#class LliurexOneDriveAC

if __name__=="__main__":
	
	LliurexOneDriveAC()
	while True:
		time.sleep(1)
