#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""荧光素酶资料：产品名改 Luciferase、去掉 020714 后缀与 BR 前缀，并重命名文件。"""
import os, shutil
ROOT='/Users/liuxinyuan/Desktop/Hermes输出-工作类'
MAP={
 'BR-Luciferase_产品介绍_A4_20261027':'Luciferase_产品介绍_A4_20261027',
 'BR-Luciferase_A4_优化版A_优势卡片_20261027':'Luciferase_A4_优化版A_优势卡片_20261027',
 'BR-Luciferase_A4_优化版B_数据看板_20261027':'Luciferase_A4_优化版B_数据看板_20261027',
 'BR-Luciferase_A4_设计版C_科研光感_20261027':'Luciferase_A4_设计版C_科研光感_20261027',
 'BR-Luciferase_A4_设计版D_生物荧光_20261027':'Luciferase_A4_设计版D_生物荧光_20261027',
 'BR-Luciferase_A4_B版_生物荧光规格强化_20261027':'Luciferase_A4_B版_生物荧光规格强化_20261027',
}
bak=os.path.join(ROOT,'归档/_版本备份/Luciferase去BR重命名_20261028')
os.makedirs(bak,exist_ok=True)

def rep(s):
    s=s.replace('BR-Luciferase','Luciferase')
    s=s.replace('BR vs Supplier N','Luciferase vs Supplier N')
    s=s.replace('>BR</text>','>本品</text>')
    s=s.replace('>BR<','>本品<')
    s=s.replace('Luciferase (020714)','Luciferase')
    s=s.replace('Luciferase（020714）','Luciferase')
    return s

for old,new in MAP.items():
    for ext in ['.html','.pdf','_预览.png']:
        p=os.path.join(ROOT,'对外物料',old+ext)
        if not os.path.exists(p): print('MISS',old+ext); continue
        shutil.copy2(p,os.path.join(bak,old+ext))
        q=os.path.join(ROOT,'对外物料',new+ext)
        os.replace(p,q)
        print('renamed',old+ext,'->',new+ext)

# edit renamed HTML
for old,new in MAP.items():
    p=os.path.join(ROOT,'对外物料',new+'.html')
    s=open(p,encoding='utf-8').read(); n0=s.count('BR-Luciferase')
    s=rep(s)
    open(p,'w',encoding='utf-8').write(s)
    print('edited',new+'.html','BR-Luc',n0)

# edit scripts
scripts=['build_luciferase_flyer.py','build_luciferase_flyer_v2.py','build_luciferase_flyer_v3.py','build_luciferase_flyer_v4.py','build_luciferase_design_v6.py','build_luciferase_B_v5.py','build_luciferase_B_bio_v7.py','replace_e800_series.py']
for sn in scripts:
    p=os.path.join(ROOT,'脚本',sn)
    s=open(p,encoding='utf-8').read(); s2=rep(s)
    # file path/name replacements in scripts
    for old,new in MAP.items():
        s2=s2.replace(old,new)
    open(p,'w',encoding='utf-8').write(s2)
    print('edited',sn)
print('backup',bak)
