#!/usr/bin/env bash
set -euo pipefail

### Обновите index.html для снежинки, если он не обновлен.

APP_DIR="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")"
APP_DATA_DIR="${APP_DIR}/data"

INDEX_HTML_FILE="${APP_DATA_DIR}/index.html"

# Мы используем маркер в верхней части файла, чтобы определить канонический index.html для снежинки.
MARKER_REGEX='^<!-- DFWebOS Snowflake canonical index.html 24-Jan-2026 -->'

# Проверяем, не существует ли файла index.html или он существует и маркер не актуален
if [[ ! -f "${INDEX_HTML_FILE}" ]] || ! grep -qx "${MARKER_REGEX}" "${INDEX_HTML_FILE}"; then
  echo "Updating index.html file for snowflake."
  # Перезапишите файл index.html.
  cat >"${INDEX_HTML_FILE}" <<'EOF'
<!-- DFWebOS Snowflake canonical index.html 24-Jan-2026 -->
<!doctype html>
<html>
  <head>
    <title>Tor Snowflake Proxy</title>
    <link rel="stylesheet" href="./css/index.css" />
    <link rel="stylesheet" href="./css/xterm.css" />
    <link rel="stylesheet" href="./css/xterm_customize.css" />
    <style>/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}main{display:block}h1{font-size:2em;margin:.67em 0}hr{box-sizing:content-box;height:0;overflow:visible}pre{font-family:monospace,monospace;font-size:1em}a{background-color:transparent}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}img{border-style:none}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:1.15;margin:0}button,input{overflow:visible}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:none;padding:0}[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring{outline:1px dotted ButtonText}fieldset{padding:.35em .75em .625em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}details{display:block}summary{display:list-item}template{display:none}[hidden]{display:none}</style>
    <style>
        body {
            background-color: #1D1B1B !important;
            font-family: system-ui,-apple-system,BlinkMacSystemFont,Roboto,Helvetica Neue,Segoe UI,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;
            color: #fff;
        }
        
        *, h1, h2, h3, h4, h5, h6, p, span {
            color: #fff;
            font-size: 20px;
            font-weight: normal;
        }
        
        .success {
            color: #63FB72;
        }
        
        .text-muted {
            opacity: 0.8;
        }
        
        .text-small {
            font-size: 14px;
        }
        
        hr {
            width: 100%;
            height: 2px;
            background: #2F2C2C;
            border: none;
            margin: 40px 0;
        }
        
        .container {
            padding: 40px;
            max-width: 1440px;
            margin: auto;
        }
        .container.terminal-container {
            background-color: #101010;
            border-radius: 30px;
            margin-bottom: 40px;
        }
        
        .app-icon {
            border-radius: 20px;
        }
        
        .app {
            display: flex;
            margin: 20px 0 40px 0;
        }
        
        .app > .app-icon {
            flex-shrink: 0;
            height: 140px;
            width: 140px;
            box-shadow: 0 0 40px 0 rgba(0,0,0,0.95);
            margin-right: 24px;
        }
        
        .app > .app-details > .app-status {
            display: block;
            font-size: 20px;
            margin: 10px 0 0 0;
        }
        
        .app > .app-details > .app-name {
            font-size: 52px;
            line-height: 52px;
            font-weight: bold;
            margin: 10px 0 0 0;
        }

        #терминал > iframe {
            border: 0px !important;
            position: relative !important;
            height: 70vh !important;
            width: 100% !important;
            max-width: 1360px !important;
        }
        
        /* Firefox iframe-ception fix */

        body.iframe {
            background-color: #101010;
        }

        body.iframe .app-container {
            display: none;
        }

        body.iframe .container.terminal-container {
            border-radius: 0;
            padding: 0;
            margin: 0;
        }
        
        /* Match terminal background to container */
        .xterm {
            background-color: #101010 !important;
        }
        
        .xterm-viewport {
            background-color: #101010 !important;
        }
    </style>
    <link rel="shortcut icon" type="image/jpg" href="favicon.png" />
  </head>
  <body>
    <section class="container app-container">
        <div class="app">
            <svg class="app-icon" width="256" height="256" viewBox="0 0 256 256" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
                <rect width="256" height="256" fill="#3C194F"/>
                <rect width="256" height="256" fill="url(#pattern0)" fill-opacity="0.2"/>
                <path d="M128 51.5V79.5M128 205V176M104 56.5L128 79.5M128 79.5L152 56.5M128 79.5V176M104 200.5L128 176M128 176L152 200.5" stroke="white" stroke-width="5" stroke-linecap="round"/>
                <path d="M73.7295 73.9787L93.5285 93.7776M182.27 182.52L161.764 162.013M60.2945 94.4848L93.5285 93.7776M93.5285 93.7776L94.2356 60.5436M93.5285 93.7776L161.764 162.013M162.118 196.308L161.764 162.013M161.764 162.013L196.059 162.367" stroke="white" stroke-width="5" stroke-linecap="round"/>
                <path d="M51.25 128.25H79.25M204.75 128.25H175.75M56.25 152.25L79.25 128.25M79.25 128.25L56.25 104.25M79.25 128.25H175.75M200.25 152.25L175.75 128.25M175.75 128.25L200.25 104.25" stroke="white" stroke-width="5" stroke-linecap="round"/>
                <path d="M73.7296 182.522L93.5286 162.723M182.27 73.9807L161.764 94.4868M94.2357 195.957L93.5286 162.723M93.5286 162.723L60.2945 162.016M93.5286 162.723L161.764 94.4868M196.059 94.1333L161.764 94.4868M161.764 94.4868L162.118 60.1922" stroke="white" stroke-width="5" stroke-linecap="round"/>
                <defs>
                <pattern id="pattern0" patternContentUnits="objectBoundingBox" width="1" height="1">
                <use xlink:href="#image0_1306_5927" transform="scale(0.00195312)"/>
                </pattern>
                <image id="image0_1306_5927" width="512" height="512" xlink:href="data:image/png;base64,[encoded data, 111088 characters]"/>
                </defs>
            </svg>                                                  
            <div class="app-details">
                <span class="app-status success">&#9679; Running</span>
                <h1 class="app-name">Tor Snowflake Proxy</h1>
            </div>
        </div>
        <p class="text-muted">
            Thank you for installing Tor Snowflake Proxy on your DFWebOS to help defeat Tor censorship around the world.
        </p>
        <p class="text-muted">
            No further action is required from your end other than keeping your DFWebOS running. :)
        </p>
        <hr/>
        <p class="text-muted text-small">
            Hourly statistics of the data relayed through your proxy are available below.
        </p>
    </section>
    <section class="container terminal-container">
        <div id="terminal"></div>
    </section>
    <script src="./auth_token.js"></script>
    <script src="./config.js"></script>
    <script src="./js/gotty.js"></script>
    <!-- Firefox iframe-ception fix -->
    <script> 
        if (window.location !== window.parent.location) {
          document.body.classList.add('iframe');
        }
    </script>
  </body>
</html>
EOF

echo "Updated index.html file for snowflake."

fi
