import shutil
import subprocess
import os
import sys


def run_by_root():
    if os.geteuid() == 0:
        return True

    try:
        subprocess.check_call(['sudo', 'python', sys.argv[0] + sys.argv[1:]])
        return False
    except subprocess.CalledProcessError:
        print('failed')
        return False


def search_yozosoft():
    if not run_by_root():
        print('Failed to elevate permissions, retrieving with low privileges')

    yozow = shutil.which('yozow')
    if yozow:
        base_path = os.path.dirname(yozow)
        yozoi = os.path.join(base_path, 'yozoi')
        yozoc = os.path.join(base_path, 'yozoc')
        return yozoc, yozoi, yozow


    res = subprocess.run(['sudo', 'find', '/', '-name', 'yozow'], capture_output=True, text=True)
    if res.stdout:
        yozow = res.stdout.strip()
        base_path = os.path.dirname(yozow)
        yozoi = os.path.join(base_path, 'yozoi')
        yozoc = os.path.join(base_path, 'yozoc')
        return yozoc, yozoi, yozow  # excel,ppt,word

    else:
        return None, None, None

# yozoc, yozoi, yozow = search_yozosoft()
# subprocess.run(['/opt/apps/com.yozosoft.yozo-office/files/bin/yozow', '/home/jxkj/桌面/test.pptx'])
