#!/p/kd/fdc/bin/wermit + # # Updates the family history index chapter list with # current page counts and dates for each chapter. # Does nothing if index is newer than all chapters. # Putting anything (e.g. 'x') as command-line argument # forces the script to update the index anyway. # # fdc 29 March 2022 # # Chapter list (order doesn't matter). # This illustrates how to initialize an array with a lot of # members without having to use line-continuation syntax. # .chapters = { vivian.html dadchapter.html lenore.html portugal.html unclepete.html dennis.html chesterbrook.html arlington.html frankfurt.html cia.html uva.html army.html afterarmy.html newyorkcity.html columbia.html thescotts.html pam.html } # Populate the array - break characters are comma and space # void \fsplit(\m(chapters),&a,\44\32) def error exit 1 "Oops - unexpected table format, line \m(n): \m(line)" cd ~/public_html/family/ if fail exit .n := \fdim(&a) # Number of chapters .idate := \fdate(index.html) # index.html date echo echo Array size: \&a[0] / \m(n); Index date: \m(idate) echo if def \%1 forward START # If any args skip the following check .max = -1 # Check if any chapters have changed for i 1 n 1 { .tag = .pname := \&a[i] .pdate := \fdate(\m(pname)) .\%9 := \fcmpdates(\m(pdate),\m(idate)) if > \%9 \m(max) .max := \%9 if > \%9 0 .tag = " - NEWER" echo "\flpad(\m(i),2). \frpad(\m(pname),20) \m(pdate) \m(tag)" } show mac max if < \m(max) 1 exit 0 No updates since \m(pdate). :START fopen /read \%c index.html # Source file if fail exit fopen /write \%o newindex.html # Destination file if fail exit copy /preserve index.html index-backup.html # Backup file if fail exit 1 "FATAL: Backup failed" .datedone = 0 # index.html "last update" line .state = 0 # For state machine .n = 0 # HTML file line number while true { # Loop through all the file lines if not \m(datedone) .prev := \m(line) fread /line /trim \%c line if fail break incr n # Line counter if \findex(BEGIN:LIST,\m(line)) { # Look for chapter table markers echo ENTERING CHAPTERS TABLE .state = 1 .row = 0 fwrite /line \%o \m(line) continue } else if \findex(END:LIST,\m(line)) { echo EXITING CHAPTERS TABLE .state = 0 fwrite /line \%o \m(line) continue } switch \m(state) { # Handle file line according to state :0 if not \m(datedone) { # Update the "Last update" date if equ "\m(prev)" "Most recent update:" { .prev = .line := \fcvtdate(,1) .datedone = 1 } } fwrite /line \%o \m(line) continue :1 # In chapters table if not def line { # Blank line fwrite /line \%o \m(line) # Just copy it continue } switch \m(row) { # Table entry :0 # Row 0 = or blank line fwrite /line \%o \m(line) if equ "\m(line)" incr row continue :1 # Row 1 = first = Name and link if not equ "\fleft(\m(line),4)" "" error void \fsplit(\m(line),&t,\34,ALL) # Get elements .pagename := \&t[2] # Extract page's filename if not exist \m(pagename) exit 1 BAD PAGE: \m(pagename) fwrite /line \%o \m(line) incr row continue :2 # Row 2 = second = page count if not equ "\fleft(\m(line),4)" "" error .tmp := \fcommand(countpages.ksc \m(pagename) | grep -i "pages") .pages := \fword(\m(tmp),3,\32) # Extract page count grep line fwrite /line \%o \m(pages) # Add to table incr row continue :3 # Row 3 = third = last update if not equ "\fleft(\m(line),4)" "" error .p := \fdate(\m(pagename)) # Get page file's creation date .pagedate := \s(p[1_4])\32/\32\s(p[5_6])\32/\32\s(p[7_8]) fwrite /line \%o \m(pagedate) # Add formatted date to table incr row continue :default # DEFAULT = or blank line .row = 0 # Start a new item fwrite /line \%o \m(line) continue } } } fclose \%c # Close input and output files fclose \%o echo DONE # Check results echo File lines: \m(n) ! diff index.html newindex.html > index.diff # Diff original and new files .difflines := \fcommand(wc -l index.diff) # How many diffs .n := \fword(\m(difflines),1) echo Diff lines: \m(n) # Shouldn't be more than about 100 if > \m(n) 100 { beep echo "TOO MANY DIFFS... check result" beep exit 1 } chmod 644 newindex.html # Fix file permissions copy /preserve /list newindex.html index.html chmod 644 index*.html dir index*.html exit ; Local Variables: ; comment-column:40 ; comment-start:"# " ; End: