#!/usr/bin/env php
<?php
// Copyright (c) 2016 https://github.com/bunam.
// All rights reserved.
//
// Redistribution and use in source and binary forms are permitted
// provided that the above copyright notice and this paragraph are
// duplicated in all such forms and that any documentation,
// advertising materials, and other materials related to such
// distribution and use acknowledge that the software was developed
// by the <organization>. The name of the
// <organization> may not be used to endorse or promote products derived
// from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// prupose : make homebrew cask recipes for all libreoffice languages in all in one script
// see http://brew.sh
// WIP : https://github.com/caskroom/homebrew-cask/pull/27193#pullrequestreview-10240273
// after run, exemple for testing : brew cask reinstall libreoffice libreoffice-language-pack --language=fr
// keep nowdoc !!
$LIBREOFFICE_LANGUAGE_ONE_TPL=<<<'END_OF_RUBY_TPL'
cask 'libreoffice-language-pack' do
version '___VERSION____'
___ALL_SELECTIONS___
# documentfoundation.org was verified as official when first introduced to the cask
url "http://download.documentfoundation.org/libreoffice/stable/#{version}/mac/x86_64/LibreOffice_#{version}_MacOS_x86-64_langpack_#{language}.dmg"
name 'LibreOffice language pack'
homepage 'https://www.libreoffice.org/'
gpg "#{url}.asc", key_id: 'c2839ecad9408fbe9531c3e9f434a1efafeeaea3'
depends_on cask: 'libreoffice'
preflight do
system '/usr/bin/tar', '-C', "#{appdir}/LibreOffice.app/", '-xjf', "#{staged_path}/LibreOffice Language Pack.app/Contents/tarball.tar.bz2"
system '/usr/bin/touch', "#{appdir}/LibreOffice.app/Contents/Resources/extensions"
end
end
END_OF_RUBY_TPL;
function px_libreoffice_laguages_pack_make_list_and_sha256($LIBREOFFICE_VERSION,$SITE_BASE,$MATCH) {
// get page
$html= file_get_contents($SITE_BASE);
if($html==false) die("Error getting : ".$SITE_BASE.chr(10)) ;
// prepare for exploration
$dom = new DOMDocument();
$dom->loadHTML($html);
$items = $dom->getElementsByTagName('a');
// all languages
$languages=array() ;
foreach($items as $value) {
$attrs = $value->attributes;
//echo $value->nodeValue ."|".$attrs->getNamedItem('href')->nodeValue . chr(10);
$testMATCHStart=substr($value->nodeValue,0,$MATCH['length']) ;
$testMATCHEnd=substr($value->nodeValue,-4) ;
$testKeep=substr($value->nodeValue,$MATCH['length'],-4) ;
if($testMATCHStart==$MATCH['str'] && $testMATCHEnd==".dmg"){
// echo $value->nodeValue ."|".$testKeep.chr(10) ;
// dedouble !
$languages[$testKeep]=true ;
}
}
// get SHA256
foreach($languages as $key => &$sha256 ){
//echo $key.chr(10);
$url = $SITE_BASE.$MATCH['str'].$key.'.dmg.sha256' ;
echo "Getting sha256 ".$url.chr(10);
sleep(1) ;
$html_sha256 = file_get_contents($url);
if($html_sha256==false) die("Error getting : ".$SITE_BASE.chr(10)) ;
$sha256 = substr($html_sha256,0,strpos($html_sha256," "));
}
//print_r($languages) ;
// facility for debug
return file_put_contents(dirname(__FILE__)."/laguages.sha256.ser",serialize($languages)) ;
}
function px_libreoffice_laguages_pack_make_cask_one_recipie($languages, $LIBREOFFICE_LANGUAGE_ONE_TPL, $LIBREOFFICE_VERSION, $PATH){
$all_selections="" ;
foreach($languages as $language => $sha256 ){
if($language!='ca-valencia') {
// mini templating
// defaulting
$default=" ";
if($language=="en-GB") {
$default= ", default: true " ;
}
# <- keep here
$all_selections .= "
language '".$language."'".$default."do
sha256 '".$sha256."'
'".$language."'
end
" ;
}
}
$newTPL=$LIBREOFFICE_LANGUAGE_ONE_TPL ;
$search = array('___VERSION____', '___ALL_SELECTIONS___');
$replace = array($LIBREOFFICE_VERSION , $all_selections);
$filename=$PATH.'/libreoffice-language-pack.rb' ;
echo 'Writing '.$filename.chr(10) ;
file_put_contents($filename, str_replace($search, $replace, $newTPL));
}
# main
if(!isset($argv[1])) {
die("You must provide an argument as a version number ex : 5.2.3") ;
}
$LIBREOFFICE_VERSION=$argv[1] ;
$SITE_BASE='http://download.documentfoundation.org/libreoffice/stable/'.$LIBREOFFICE_VERSION.'/mac/x86_64/' ;
$MATCH['str']="LibreOffice_".$LIBREOFFICE_VERSION."_MacOS_x86-64_langpack_" ;
$MATCH['length']=strlen($MATCH['str']) ;
// where is brew ???
exec('brew config | grep HOMEBREW_REPOSITORY',$output,$ret) ;
if($ret==0) {
$HOMEBREW_REPOSITORY = substr($output[0],strpos($output[0]," ")+1) ;
$HOMEBREW_CASK_REPOSITORY = $HOMEBREW_REPOSITORY."/Library/Taps/caskroom/homebrew-cask/Casks" ;
if(!is_dir($HOMEBREW_CASK_REPOSITORY)) {
die("homebrew : cask not there ? : ".$HOMEBREW_CASK_REPOSITORY) ;
}
}else{
die("homebrew not installed ?") ;
}
px_libreoffice_laguages_pack_make_list_and_sha256($LIBREOFFICE_VERSION,$SITE_BASE,$MATCH) ;
$languages = unserialize(file_get_contents(dirname(__FILE__)."/laguages.sha256.ser")) ;
px_libreoffice_laguages_pack_make_cask_one_recipie($languages, $LIBREOFFICE_LANGUAGE_ONE_TPL, $LIBREOFFICE_VERSION, $HOMEBREW_CASK_REPOSITORY) ;
echo "All finished".chr(10) ;
?>
Comments