#!/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/>.
# *********************************************************************

import requests, fileinput

# 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. 
def main():
    # URL to the raw files on Github where the CSS lives
    baseurl = 'https://raw.githubusercontent.com/PreTeXtBook/CSS_core/main/'
    # List of CSS files to retrieve. Add on as more are added upstream.
    css_file_list = ['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:
        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)

    # Validator does not allow @import in CSS, so let's just remove it
    for line in fileinput.input('pretext_add_on.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");', ''), end='')

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

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