perl

My Perl Scripts Collection
git clone https://fab.ddns.me.uk/stagit/perl
Log | Files | Refs | README

update-stagit.pl (3367B)


      1 #!/usr/bin/perl
      2 use strict;
      3 use warnings;
      4 
      5 # -----------------------------------------------------------------------------
      6 # Name:        update-stagit.pl
      7 # Description: Universal static site generator for stagit with automated
      8 #              navigation link insertion for OpenBSD.
      9 # Version:     1.0.7 (2026-01-01)
     10 # Author:      masayoshi
     11 # License:     ISC License
     12 # -----------------------------------------------------------------------------
     13 # Copyright (c) 2026 masayoshi
     14 #
     15 # Permission to use, copy, modify, and distribute this software for any
     16 # purpose with or without fee is hereby granted, provided that the above
     17 # copyright notice and this permission notice appear in all copies.
     18 # -----------------------------------------------------------------------------
     19 
     20 # --- Configuration ---
     21 my $base_wwwDir = "/var/www/stagit";
     22 my $stagit_bin  = "/usr/local/bin/stagit";
     23 my $index_bin   = "/usr/local/bin/stagit-index";
     24 
     25 # --- 1. Identify Repository from Arguments ---
     26 my $repo_path = $ARGV[0];
     27 if ( !$repo_path ) {
     28     print "Error: No repository path provided.\n";
     29     exit 1;
     30 }
     31 
     32 # Resolve repository name (e.g., /var/www/stagit/diary.git -> diary)
     33 my $repo_git_name  = ( $repo_path =~ m|/([^/]+)/?$| )[0] || $repo_path;
     34 my $repo_bare_name = $repo_git_name;
     35 $repo_bare_name =~ s/\.git$//;
     36 
     37 my $target_dir = "$base_wwwDir/$repo_bare_name";
     38 
     39 print "--- update-stagit v1.0.7 ---\n";
     40 print "Processing repository: $repo_bare_name\n";
     41 
     42 # --- 2. Update Repository Pages ---
     43 if ( !-d $target_dir ) {
     44     print "Initialising new directory: $target_dir\n";
     45     mkdir $target_dir, 0755 or die "Cannot create directory: $!";
     46 }
     47 
     48 if ( chdir $target_dir ) {
     49     print "Generating HTML via stagit...\n";
     50     system( $stagit_bin, $repo_path );
     51 
     52     # Insert Navigation Link to all generated HTML files
     53     my @html_files = glob( "*.html" );
     54     foreach my $file ( @html_files ) {
     55         if ( open my $fh, '<', $file ) {
     56             local $/;
     57             my $content = <$fh>;
     58             close $fh;
     59 
     60             # Check if link already exists to prevent duplicate insertion
     61             if ( $content !~ m|stagit-index-link| ) {
     62                 my $link_html =
     63                       qq|<hr/><div class="stagit-index-link" style="margin: 10px 0; font-family: sans-serif;">|
     64                     . qq|<b><a href="/stagit/index.html">← Back to Portal (All Repositories)</a></b></div>|;
     65 
     66                 # Insert link after the first horizontal rule
     67                 if ( $content =~ s|<hr\s*/?>|$link_html| ) {
     68                     if ( open my $out, '>', $file ) {
     69                         print $out $content;
     70                         close $out;
     71                     }
     72                 }
     73             }
     74         }
     75     }
     76 
     77     # Ensure log.html is the default index for the repository
     78     if ( -e "log.html" ) {
     79         unlink( "index.html" );
     80         symlink( "log.html", "index.html" );
     81     }
     82     system( "chmod 644 *.html" );
     83 }
     84 
     85 # --- 3. Update Global Portal (stagit-index) ---
     86 if ( chdir $base_wwwDir ) {
     87     my @all_repos = glob( "*.git" );
     88     if ( @all_repos ) {
     89         print "Updating global portal index...\n";
     90         system( "$index_bin @all_repos > index.html.tmp" );
     91         rename( "index.html.tmp", "index.html" );
     92         chmod 0644, "index.html";
     93     }
     94 }
     95 
     96 print "Success: $repo_bare_name is updated and linked to portal.\n";
     97 print "----------------------------\n";