#!/usr/bin/env python3

# ********************************************************************
# Copyright 2021 Mitchel T. Keller
#
# This file is part of PreTeXt.
#
# PreTeXt 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 or version 3 of the
# License (at your option).
#
# PreTeXt 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 PreTeXt.  If not, see <http://www.gnu.org/licenses/>.
# *********************************************************************

# NOTE: when you use this script to update the CSS, even if
# there is no actual change, note the date and CSS version
# in the README.md file in this directory as part of a commit

# Simple Python 3 script to retrieve the PreTeXt book CSS from Github
# This CSS is used for EPUB and Kindle output. It is not required to
# exist or be updated for HTML builds.
#
# History:
#
# 2022-12-30:  Added a print statement for progress indication
# 2023-01-03:  Parameterize with branch name
# 2023-01-03:  Update, extend filtering external resources

import requests, fileinput

# some situations require getting this from another branch
branch = 'main'

def main():
    # URL to the raw files on Github where the CSS lives
    baseurl = 'https://raw.githubusercontent.com/PreTeXtBook/CSS_core/{}/'.format(branch)
    # List of CSS files to retrieve. Add on as more are added upstream.
    css_file_list = ['pretext.css',
                         'pretext_add_on.css',
                         'setcolors.css', 
                         'style_default.css', 
                         'style_oscarlevin.css', 
                         'style_soundwriting.css', 
                         'colors_blue_green.css', 
                         'colors_blue_grey.css', 
                         'colors_blue_red.css', 
                         'colors_brown_gold.css', 
                         'colors_darkmartiansands.css', 
                         'colors_default.css', 
                         'colors_green_blue.css', 
                         'colors_green_plum.css', 
                         'colors_maroon_grey.css', 
                         'colors_martiansands.css', 
                         'colors_orange_navy.css', 
                         'colors_pastel_blue_orange.css', 
                         'colors_red_blue.css', 
                         'colors_ruby_amethyst.css', 
                         'colors_ruby_emerald.css', 
                         'colors_ruby_turquoise.css', 
                         'kindle.css',
                         'epub.css']
    for filename in css_file_list:
        print("Downloading {}...".format(filename))
        url = baseurl + filename
        r = requests.get(url, allow_redirects=True)
        # requests gets the file as bytes and not text, so need 'wb' here
        open(filename, 'wb').write(r.content)

    # EPUB validator does not allow @import in CSS, so let's just remove it
    for line in fileinput.input('pretext.css', inplace=1):
        print(line.replace('@import url("https://fonts.googleapis.com/css?family=PT+Serif:400,700,400italic,700italic|Open+Sans:400italic,700italic,400,700");', '/* Google font download removed */'), end='')
    # Likewise, the EPUB validator does not like an image coming from an external site
    for line in fileinput.input('pretext_add_on.css', inplace=1):
        print(line.replace("background-image: url('https://raw.githubusercontent.com/openwebwork/webwork2/main/htdocs/images/favicon.ico');", '/* WW Activate button backgroound removed */'), end='')

    print("CSS files have been updated in your local copy of the repository")

# Do it - lone top-level command
main()
