#!/bin/bash
# Install "No Terms Required" ToS + privacy for the exchange (styled like merchant terms).
# Run as root inside the exchange container.
set -euo pipefail
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin${PATH:+:$PATH}"

CONF="${TALER_EXCHANGE_CONFIG:-/etc/taler-exchange/taler-exchange.conf}"
DATA_HOME=$(taler-exchange-config -c "$CONF" -f -s PATHS -o TALER_DATA_HOME 2>/dev/null || true)
DATA_HOME="${DATA_HOME:-/var/lib/taler-exchange/}"
TERMS_DIR="${DATA_HOME%/}/terms"
LANG_DIR="$TERMS_DIR/en"
mkdir -p "$LANG_DIR"

# Bump when HTML/style changes (long Cache-Control on /terms).
TOS_ETAG="${TERMS_ETAG:-no-terms-v2}"
# Prefer Swiss FADP privacy install (install_swiss_privacy.sh); fallback etag here
PP_ETAG="${PRIVACY_ETAG:-exchange-pp-swiss-v0}"

# Shared palette with merchant-tos-dual / bank terms pages
COMMON_CSS='
    :root { color-scheme: dark light; }
    body {
      font-family: system-ui, -apple-system, sans-serif;
      max-width: 40rem; margin: 2rem auto; padding: 0 1.1rem 3rem;
      line-height: 1.5; color: #e8e6e3; background: #1a1520;
    }
    h1 { font-size: 1.35rem; font-weight: 800; margin: 0 0 1rem; color: #f5f0ea; }
    h2 { font-size: 1.05rem; margin: 1.4rem 0 0.5rem; color: #e8c878; }
    p, li { font-size: 0.98rem; }
    ul { padding-left: 1.2rem; }
    code, a { color: #5eead4; }
    a { text-decoration: none; }
    a:hover { text-decoration: underline; }
    .badge {
      display: inline-block; font-size: 0.72rem; font-weight: 700;
      letter-spacing: 0.06em; text-transform: uppercase;
      color: #c4b5fd; border: 1px solid rgba(196,181,253,0.35);
      border-radius: 999px; padding: 0.2rem 0.65rem; margin-bottom: 0.85rem;
    }
    .cur {
      border-radius: 12px; padding: 0.75rem 0.9rem; margin: 0.85rem 0 1rem;
      border: 1px solid rgba(255,255,255,0.1); background: rgba(0,0,0,0.25);
    }
    .cur strong { display: block; font-size: 1.05rem; margin-bottom: 0.25rem; color: #5eead4; }
    .muted { color: #a39e98; font-size: 0.88rem; }
    footer { margin-top: 2rem; font-size: 0.85rem; color: #a39e98; }
'

write_tos() {
  local base="$1"
  local title="No Terms Required"
  local body_txt body_md

  body_txt='No Terms Required.

This is an experimental / exploration GNU Taler exchange for the currency GOA (hacktivism.ch).

No formal terms of service are required to use this service.

By withdrawing or using GOA coins you acknowledge that:
- This service is for exploration and testing only.
- GOA is not legal tender and has no guaranteed real-world value or redemption.
- There is no guaranteed availability, support, or uptime.
- Operators may reset balances or change configuration without notice.
- Do not use real money you cannot afford to lose.

If you do not agree, do not use this exchange.

Related:
- Bank: https://bank.hacktivism.ch/intro/
- Merchant terms: https://taler.hacktivism.ch/terms
- Bank terms: https://bank.hacktivism.ch/terms

Privacy:
Processing under Swiss FADP (revDSG). What data is retained is listed at
https://exchange.hacktivism.ch/privacy
'

  body_md='# No Terms Required

This is an **experimental / exploration** GNU Taler exchange for the currency **GOA** (hacktivism.ch).

**No formal terms of service** are required to use this service.

By withdrawing or using GOA coins you acknowledge that:

- This service is for exploration and testing only.
- GOA is not legal tender and has no guaranteed real-world value or redemption.
- There is no guaranteed availability, support, or uptime.
- Operators may reset balances or change configuration without notice.
- Do not use real money you cannot afford to lose.

If you do not agree, do not use this exchange.

## Related

- [Bank intro](https://bank.hacktivism.ch/intro/)
- [Bank terms](https://bank.hacktivism.ch/terms)
- [Merchant terms](https://taler.hacktivism.ch/terms)
- [Exchange privacy](https://exchange.hacktivism.ch/privacy)

## Privacy

Processing under Swiss FADP (revDSG). What data is retained is listed on
[exchange.hacktivism.ch/privacy](https://exchange.hacktivism.ch/privacy).
'

  printf '%s\n' "$body_txt" >"$LANG_DIR/${base}.txt"
  printf '%s\n' "$body_md" >"$LANG_DIR/${base}.md"
  cat >"$LANG_DIR/${base}.html" <<HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <title>${title}</title>
  <style>
${COMMON_CSS}
  </style>
</head>
<body>
  <div class="badge">exchange.hacktivism.ch · GOA</div>
  <h1>${title}</h1>
  <p>This is an <strong>experimental / exploration</strong> GNU Taler exchange for the
    currency <strong>GOA</strong> at <code>exchange.hacktivism.ch</code> (hacktivism.ch).</p>
  <p><strong>No formal terms of service</strong> are required to use this service.</p>

  <div class="cur">
    <strong>GOA · explorational</strong>
    Not legal tender. No guaranteed real-world value, redemption, or convertibility.
    Issued only for exploration and testing on this stack.
  </div>

  <h2>By withdrawing or using GOA coins you acknowledge</h2>
  <ul>
    <li>This service is for exploration and testing only.</li>
    <li>There is no guaranteed availability, support, or uptime.</li>
    <li>Operators may reset balances or change configuration without notice.</li>
    <li>Do not use real money you cannot afford to lose.</li>
    <li>Software is provided as-is, without warranty.</li>
  </ul>
  <p>If you do not agree, do not use this exchange.</p>

  <h2>Related</h2>
  <ul>
    <li><a href="https://bank.hacktivism.ch/intro/">Bank intro</a></li>
    <li><a href="https://bank.hacktivism.ch/terms">Bank terms</a></li>
    <li><a href="https://taler.hacktivism.ch/terms">Merchant terms</a></li>
    <li><a href="https://exchange.hacktivism.ch/intro/">Exchange intro</a></li>
    <li><a href="https://exchange.hacktivism.ch/privacy">Exchange privacy</a></li>
  </ul>

  <h2>Privacy</h2>
  <p class="muted">Processing under Swiss FADP (revDSG). What data is retained
    (reserves, wire-in, coins, logs, …) is listed on
    <a href="https://exchange.hacktivism.ch/privacy">/privacy</a>.</p>
  <footer class="muted">Version ${base} · hacktivism.ch</footer>
</body>
</html>
HTML
}

write_pp() {
  # Legacy short PP only if not using install_swiss_privacy.sh etag
  local base="$1"
  local title="Privacy notice · GOA Exchange"
  local body_txt body_md

  body_txt='No Privacy Policy Required.

This is an experimental / exploration GNU Taler exchange for GOA.

No formal privacy policy is required for this demo service.

High-level notes:
- Wire transfers via the regional bank may identify bank account holders.
- The exchange processes withdrawals, deposits, and related protocol operations.
- Logs may be kept for operation and debugging.

Do not use this service if that is unacceptable.

Related:
- Exchange terms: https://exchange.hacktivism.ch/terms
'

  body_md='# No Privacy Policy Required

This is an **experimental / exploration** GNU Taler exchange for **GOA**.

**No formal privacy policy** is required for this demo service.

## High-level notes

- Wire transfers via the regional bank may identify bank account holders.
- The exchange processes withdrawals, deposits, and related protocol operations.
- Logs may be kept for operation and debugging.

Do not use this service if that is unacceptable.

## Related

- [Exchange terms](https://exchange.hacktivism.ch/terms)
'

  printf '%s\n' "$body_txt" >"$LANG_DIR/${base}.txt"
  printf '%s\n' "$body_md" >"$LANG_DIR/${base}.md"
  cat >"$LANG_DIR/${base}.html" <<HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <title>${title}</title>
  <style>
${COMMON_CSS}
  </style>
</head>
<body>
  <div class="badge">exchange.hacktivism.ch · privacy</div>
  <h1>${title}</h1>
  <p>This is an <strong>experimental / exploration</strong> GNU Taler exchange for
    <strong>GOA</strong> at <code>exchange.hacktivism.ch</code>.</p>
  <p><strong>No formal privacy policy</strong> is required for this demo service.</p>

  <h2>High-level notes</h2>
  <ul>
    <li>Wire transfers via the regional bank may identify bank account holders.</li>
    <li>The exchange processes withdrawals, deposits, and related protocol operations.</li>
    <li>Logs may be kept for operation and debugging.</li>
  </ul>
  <p>Do not use this service if that is unacceptable.</p>

  <h2>Related</h2>
  <ul>
    <li><a href="https://exchange.hacktivism.ch/terms">Exchange terms</a></li>
    <li><a href="https://bank.hacktivism.ch/terms">Bank terms</a></li>
    <li><a href="https://taler.hacktivism.ch/terms">Merchant terms</a></li>
  </ul>
  <footer class="muted">Version ${base}</footer>
</body>
</html>
HTML
}

write_tos "$TOS_ETAG"
# Prefer Swiss FADP privacy installer when present (precise retention tables)
if [ -x /usr/local/bin/install_swiss_privacy.sh ]; then
  PRIVACY_ETAG="$PP_ETAG" /usr/local/bin/install_swiss_privacy.sh
else
  write_pp "$PP_ETAG"
fi

chmod -R a+rX "$TERMS_DIR"
if id taler-exchange-httpd >/dev/null 2>&1; then
  chown -R taler-exchange-httpd: "$TERMS_DIR" 2>/dev/null || true
fi

echo "Installed under $LANG_DIR:"
ls -la "$LANG_DIR"/${TOS_ETAG}.* "$LANG_DIR"/${PP_ETAG}.* 2>/dev/null || ls -la "$LANG_DIR"
echo "Config should set:"
echo "  TERMS_ETAG = ${TOS_ETAG}"
echo "  PRIVACY_ETAG = ${PP_ETAG}"
echo "  TERMS_DIR / PRIVACY_DIR = \${TALER_DATA_HOME}terms/"
