<?php

## ManageCSS.php

/*
    local.css manager for PMWiki

    This script assembles all of the css files in the pub/css/ directory
    and creates a new local.css file 
    (WARNING: It will delete the previous local.css file!)

    In other words you can place recipe specifi css files in the
    pmwiki/pub/css folder and never edit the local.css file!

    Copyright 2021 Kirk Siqveland DBA: CyberTamer Services
   

    This recipe is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with your copy of th PMWiki code; if not, write to the 
    Free Software Foundation, Inc., 
    59 Temple Place, Suite 330, 
    Boston, MA  02111-1307  USA
*/

if (!defined('PmWiki')) exit();

SDV($RecipeInfo['ManageCSS']['Version'], '2021-12-01');
$FmtPV['$ManageCSSVersion'] = "'{$RecipeInfo["ManageCSS"]["Version"]}'";

## ----------------------------------------------------------------------------------
##    Populate a new local.css file with all the CSS files in the pub/css/ folder
## ----------------------------------------------------------------------------------    

## Get the current working directory (for the pmwiki.php file)
## then add the sub-directories pub/css
   $myDir = getcwd()."/pub/css";

    if ($myDir !== false){

    ## - Scan the css directory for all files, then extract css filenames.
    ##   Open directory | Read Line-BY-Line into $dirArray | Close directory
            $myDirectory = opendir($myDir) ;

            if($myDirectory == false){
                $outputCSS  .= "\nFailed to Open $myDir \n" ;
            }else{
                while($entryName = readdir($myDirectory))
                { 
                    if(!is_dir( $entryName )){  
                    // Find CSS files and add them to the list (ignore directories)
                        $extn=pathinfo($entryName, PATHINFO_EXTENSION) ; 
                        if($extn == "css"){
                            if($entryName != "local.css"){ $dirArray[]  = $entryName ;   }
                        } 
                    }
                }
                closedir($myDirectory) ;  

                // Count elements in array
                    $tally=count($dirArray) ;

                // Sort directory file list array
                  sort($dirArray) ;             

                // Compile a string of the contents of each CSS file
                    for($tick=0 ; $tick < $tally ; $tick++) 
                    {
                        $srcFile = $dirArray[$tick] ;     
                        $outputCSS .= "\n\n".'/* CSS code from '. $srcFile .'*/'."\n";
                        $outputCSS .=  file_get_contents("$myDir/$srcFile") ;
                    }
            }                
    }else{
        $outputCSS = "Can't read the given directory: " . $myDir ;
    }

## Empty the file if it exists 
    if (file_exists("$myDir/local.css")) {
        $outfile = fopen( "$myDir/local.css", 'w' );
        fclose($outfile) ;
    }
## Make a new local.css file using the compiled information
    $outfile =  fopen("$myDir/local.css", "w") ; // or die("Unable to open file!") ;
        fwrite($outfile, $outputCSS)     ;
        fclose($outfile)                 ;