#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#author:Wos
#An auxiliary tool for streamlined testing of windows system
#The script only generates batch files, not directly executed
'''
When I want to test whether a file is needed for the system to run,
the easiest way is to rename the file, and then restart the computer
to check the running status, but renaming system files is very cumbersome,
especially if you have multiple files that need to be renamed.
the problem becomes even more troublesome.
'''
#force rename multi system files
#tips: How to get multi files full path in directory?
'''
select file or multi file,then Hold shift, right click and copy as path,then paste to
xrename.txt,format like below

"C:\windows\system32\wups2.dll"
"C:\windows\system32\wusa.exe"
"C:\windows\system32\wuwebv.dll"
"C:\windows\system32\wwancfg.dll"
"C:\windows\system32\wwanconn.dll"
"C:\windows\system32\WWanHC.dll"
"C:\windows\system32\wwaninst.dll"
"C:\windows\system32\wwanmm.dll"
"C:\windows\system32\Wwanpref.dll"
"C:\windows\system32\wwanprotdim.dll"
"C:\windows\system32\wwansvc.dll"
"C:\windows\system32\wwapi.dll"
"C:\windows\system32\wzcdlg.dll"
"C:\windows\system32\wudriver.dll"
"C:\windows\system32\wups.dll"

'''
import os
import sys
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
xrename_txt_path = os.path.join(BASE_DIR, "xrename.txt") #full path file list
xrename_bat_path = os.path.join(BASE_DIR, "xrename.bat") #rename bat file
xrename_restore_bat_path = os.path.join(BASE_DIR, "xrename_restore.bat") #restore bat file

bat_head = '''@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
'''

rlist = []
rlist_ = [] 
cmdlist = [] #rename command
cmdlist_ = [] #restore command
#read rename.txt

if not os.path.exists(xrename_txt_path):
    with open (xrename_txt_path,"w") as f:
        f.write("#select file or multi file,Hold shift, right click and copy as path,then paste to this file")
        sys.exit()
else:
    with open (xrename_txt_path,"r") as f:
        line = f.readline().strip().replace("\"","")
        while line:
            if str(line).startswith("#"):
                line = f.readline().strip().replace("\"","")
                continue
            else:
                rlist.append(line)
                line = f.readline().strip().replace("\"","")
                continue

if len(rlist) != 0:                 
    for i in rlist:
        d = "\\".join(str(i).split("\\")[:-1])
        l = str(i).split("\\")[-1]
        l1 = ("cd %s"%(d))
        l2 = ("takeown /f %s"%(l))
        l2_ = ("takeown /f %s_bak"%(l))
        l3 = ("icacls %s /grant administrators:F"%(l))
        l3_ = ("icacls %s_bak /grant administrators:F"%(l))
        l4 = ("rename %s %s_bak"%(l,l))
        l4_ = ("rename %s_bak %s"%(l,l)) 
        cmdlist.append(l1)
        cmdlist.append(l2)
        cmdlist.append(l3)
        cmdlist.append(l4)
        cmdlist_.append(l1)
        cmdlist_.append(l2_)
        cmdlist_.append(l3_)
        cmdlist_.append(l4_)



    with open(xrename_bat_path,"w") as f:
        f.write(bat_head)
        for j in cmdlist:
            f.writelines(j+"\n")
        f.writelines("pause")

    with open(xrename_restore_bat_path,"w") as f:
        f.write(bat_head)
        for j in cmdlist_:
            f.writelines(j+"\n")
        f.writelines("pause")