from pathlib import Path
import struct
out=Path(__file__).resolve().parent
# Minimal original Publisher 97-style Contents stream. Demonstrates one text
# frame through the production libmspub parser, not broad Publisher fidelity.
b=bytearray(4096); b[:4]=bytes.fromhex('e8ac2200')
struct.pack_into('<II',b,0x12,0x40,0x80)
lines=['PUBHARBOR ARCHIVE DEMO','Original synthetic publication.','No customer or third-party content.','1. Keep your original PUB.','2. Export a reading copy.','3. Check every page.','Simple example, not a fidelity benchmark.']
text=('\x0c'.join(lines)+'\x0c').encode('ascii')
struct.pack_into('<IIHHHH',b,0x40+14,0x900,0x900+len(text),0,0,0,0)
b[0x900:0x900+len(text)]=text
chunks=[(1,0,0x100,0x15),(2,1,0x180,0x14)]+[(3+i,2,0x200+0x80*i,0) for i in range(len(lines))]
struct.pack_into('<H',b,0x80,len(chunks))
for i,(id,parent,offset,kind) in enumerate(chunks):
 struct.pack_into('<HHHI',b,0x82+i*10,0,id,parent,offset)
 struct.pack_into('<H',b,offset,kind)
struct.pack_into('<HII',b,0x112,0,6*914400,5*914400)
for i in range(len(lines)):
 off=0x200+0x80*i;y=-1.8+i*0.5
 struct.pack_into('<iiii',b,off+6,*[int(v*914400) for v in (22.4,25+y-0.3,27.6,25+y)])
 struct.pack_into('<H',b,off+0x46,i)
# CFB v3: 8 regular sectors, one directory, one FAT. No mini stream needed.
FREE,END,FAT=0xffffffff,0xfffffffe,0xfffffffd
h=bytearray(512);h[:8]=bytes.fromhex('d0cf11e0a1b11ae1')
struct.pack_into('<HHHHH',h,24,0x3e,3,0xfffe,9,6)
struct.pack_into('<IIIIIIIII',h,40,0,1,8,0,4096,END,0,END,0)
struct.pack_into('<109I',h,76,9,*([FREE]*108))
directory=bytearray(512)
for i,(name,kind,child,start,size) in enumerate([('Root Entry',5,1,END,0),('Contents',2,FREE,0,4096)]):
 n=(name+'\0').encode('utf-16le');off=i*128;directory[off:off+len(n)]=n
 struct.pack_into('<HBBIII',directory,off+64,len(n),kind,1,FREE,FREE,child)
 struct.pack_into('<IQ',directory,off+116,start,size)
fat=[1,2,3,4,5,6,7,END,END,FAT]+[FREE]*118
(out/'demo-archive-publication.pub').write_bytes(h+b+directory+struct.pack('<128I',*fat))
print('Created original unsigned ASiC container and synthetic PUB parser example.')
