#!/usr/bin/perl
use strict ;
use warnings ;
use Term::ANSIColor ;
use Cwd qw ( getcwd ) ;

# ******************************************** #
# Create PerlSub distribution archive.         #
# ******************************************** #
# Updated: 21-Jun-2026
# Author : Mahlon R. Smith
#          Copyright (c) 2026-2027 Mahlon R. Smith, The Software Samurai
#              GNU GPL copyright notice located in NcDialog.hpp
# Developed under: Perl 5.40.4
# Call:  cd PerlSub/install            # navigate to install directory
#        ./create_archive.pl           # execute the Perl script
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


   # Define the filenames included in the package.#
   my @SrcList = 
   (
    "heic.pl",
    "subtest.pl",
    "Image_01.heic",
    "Image_02.heic",
    "Image_03.heic",
    "README",
   ) ;

   my @AuxList = 
   (
      "create_archive.pl",
   ) ;

   # number of source files expected
   my $SrcCount = scalar @SrcList + scalar @AuxList ;
   my $SrcOK    = 0 ;                  # number of source files found
   my $TrgOK    = 0 ;                  # number of files copied to target
   my $SrcSpec  = "" ;                 # source filespec
   my $TrgSpec  = "" ;                 # target filespec

   # Define source and target directories.#
   use Cwd qw ( getcwd ) ;
   my $BWD=getcwd() ;
   chdir ( ".." ) ;
   my $SrcBase = getcwd () ;
   my $AuxBase = "$BWD" ;
   my $TrgBase = "$BWD/PerlSub" ;
   my $TrgAux  = "$TrgBase/install" ;

   system ( "clear" ) ;
   print colored ( "** Copy source to install directory. **", 'blue' ) ;
   print colored ( "\nsource base: '$SrcBase'", 'blue' ) ;
   print colored ( "\ntarget base: '$TrgBase'", 'blue' ) ;

   #********************************************************#
   # If source directory does not exist, we cannot continue.#
   #********************************************************#
   if ( ! -d $SrcBase )
   {
      print colored ( "\n** Source Directory Does Not Exist! **", 'bright_red' ) ;
      print "\n   ($SrcBase)\n\n" ;
      exit ;
   }

   #************************************************#
   # If target directory does not exist, create it. #
   #************************************************#
   if ( ! -d $TrgBase )
   { mkdir "$TrgBase" ; }
   if ( ! -d $TrgBase )
   {
      print colored ( "\n** Unable to create target directory! **", 'bright_red' ) ;
      print "\n   ($TrgBase)\n\n" ;
      exit ;
   }

   #****************************************************#
   # If target aux directory does not exist, create it. #
   #****************************************************#
   if ( ! -d $TrgAux )
   { mkdir "$TrgAux" ; }
   if ( ! -d $TrgAux )
   {
      print colored ( "\n** Unable to create target directory! **", 'bright_red' ) ;
      print "\n   ($TrgAux)\n\n" ;
      exit ;
   }

   #****************************#
   # Copy the Auxilliary files  #
   #****************************#
   foreach my $i ( 0 .. $#AuxList )
   {
      $SrcSpec = sprintf ( "%s/%s", $AuxBase, $AuxList[$i] ) ;
      print colored ( "\n SRC: ", 'blue' ) ;
      print "$SrcSpec" ;
      SetSpacing ( $SrcSpec ) ;
      if ( -e $SrcSpec )
      {
         print colored ( "OK", 'bright_green' ) ;
         $SrcOK += 1 ;
         $TrgSpec = sprintf ( "%s/%s", $TrgAux, $AuxList[$i] ) ;
         print "\n TRG: $TrgSpec" ;
         SetSpacing ( $TrgSpec ) ;
         system ( "cp --preserve=all '$SrcSpec' '$TrgSpec'" ) ;
         if ( -e $TrgSpec )
         {
            print colored ( "OK", 'bright_green' ) ;
            $TrgOK += 1 ;
         }
         else
         { print colored ( "  [Access Error!]",  'bright_red' ) ; }
      }
   }

   #***************************************#
   # Copy the source code files to target. #
   #***************************************#
   foreach my $i ( 0 .. $#SrcList )
   {
      $SrcSpec = sprintf ( "%s/%s", $SrcBase, $SrcList[$i] ) ;
      print colored ( "\n SRC: ", 'blue' ) ;
      print "$SrcSpec" ;
      SetSpacing ( $SrcSpec ) ;

      if ( -e $SrcSpec )
      {
         print colored ( "OK", 'bright_green' ) ;
         $SrcOK += 1 ;

         $TrgSpec = sprintf ( "%s/%s", $TrgBase, $SrcList[$i] ) ;
         print colored ( "\n TRG: ", 'blue' ) ;
         print "$TrgSpec" ;
         SetSpacing ( $TrgSpec ) ;

         system ( "cp --preserve=all '$SrcSpec' '$TrgSpec'" ) ;
         if ( -e $TrgSpec )
         {
            print colored ( "OK", 'bright_green' ) ;
            $TrgOK += 1 ;
         }
         else
         {
            print colored ( "Copy Error!\n\n", 'bright_red' ) ;
            exit ;
         }
      }
      else
      {
         print colored ( "  [Not Found!]\n\n",  'bright_red' ) ;
         exit ;
      }
   }

   # Report results of copy operation.#
   print colored ( "\n\nSrcCount: $SrcCount  SrcOK: $SrcOK  TrgOK: $TrgOK\n", 'blue' ) ;

   #********************************************#
   # If copy was successful, create the archive.#
   #********************************************#
   if ( $TrgOK == $SrcCount )
   {
     # Return to "install" directory. Archive created here. #
      chdir ( "$BWD" ) ;
      my $WD=getcwd() ;
      print colored ( "\nCreate Archive:\n---------------", 'blue' ) ;
      print "\n $WD" ;     # DEBUG ONLY

      if ( $WD eq $BWD )
      {
         # IMPORTANT NOTE: Verify that version number here matches the   #
         # version number defined in heic.pl. This assumes that          #
         # filename at $SrcList[0] contains the version-number string.   #
         # It is also assumed that gremlins have not modified the        #
         # layout of the source line containing the version string.      #
         my $SearchString = "my \$HEIC_VERSION = \"" ;
         $TrgSpec = sprintf ( "%s/%s", $TrgBase, $SrcList[0] ) ;
         my $VerString = qx ( grep -hm 1 '$SearchString' $TrgSpec 2>&1 ) ;
         chomp $VerString ;
         #print "\n$SearchString\n$TrgSpec\n$VerString" ; # debugging
         if ( (index ( "$VerString", "HEIC_VERSION" )) > 0 )
         {
            my $indx = index ( "$VerString", "\"" ) + 1 ;
            my $AppVersion = substr ( $VerString, $indx, 6 ) ;
            print "\nDistro Version: '$AppVersion'" ;

            my $PACK_TARGET = "perlsub-$AppVersion.tar.bz2" ;
            print "\nTarget Archive: '$PACK_TARGET'" ;

            unlink $PACK_TARGET ;      # delete existing target (if any)

            # Create the archive #
            system ( "tar --numeric-owner -cjf '$PACK_TARGET' 'PerlSub'" ) ;

            # DEBUG ONLY: visually verify the results #
            #print "\n" ; system ( "tar -tvf $PACK_TARGET" ) ;  # DEBUG ONLY

            # Verify archive creation #
            if ( -e $PACK_TARGET )
            {
               print "\t" ;
               print colored ( " OK ", 'bright_yellow on_green' ) ;

               # Delete the temporary files. #
               print colored ( "\nDelete temporary files:\t\t", 'blue' ) ;
               system ( "rm $TrgAux/*" ) ;
               system ( "rmdir $TrgAux" ) ;
               system ( "rm $TrgBase/*" ) ;
               system ( "rmdir $TrgBase" ) ;
               if ( ! -e $TrgBase )
               { print colored ( "OK", 'green' ) ; }
               else
               { print colored ( "ERROR!", 'red' ) ; }
            }
            else
            { print "\nError creating archive!" ; }
         }
         else
         { print "\nError! Version string not found in $SrcList[0]." ; }
      }
      else     # (unlikely)
      { print "\nError! Unable to reach archive directory." ; }
   }
   else        # (should never arrive here)
   { print "\nError(s) copying source to target!" ; }

   print "\n\n" ;

   # Calculate spacing for consistent output formatting.#
   # (assumes an 8-column tab)                          #
   sub SetSpacing
   {
      my $slen = length $_[0] ;
      $slen += 6 ;                        # width of header
      if    ( $slen < 64 )  { print "\t\t\t\t\t\t" ; }
      elsif ( $slen < 72 )  { print "\t\t\t\t\t" ; }
      elsif ( $slen < 80 )  { print "\t\t\t\t" ; }
      elsif ( $slen < 88 )  { print "\t\t\t" ; }
      elsif ( $slen < 96 )  { print "\t\t" ; }
      else                  { print "\t" ; }
   }

