<?php
error_reporting(1);
if (isset($_GET['Fdata'])) {
  $data = unserialize(urldecode($_GET['Fdata']));

  $username = $data['username'];


  $basePath = str_replace('/assets', '', dirname($_SERVER['SCRIPT_NAME']));
  $startUrl = "$basePath/$username/";
  $scope = "$basePath/$username/";


  if (!strpos($_SERVER['HTTP_HOST'], 'localhost') && (empty($basePath) || $basePath == '/' || $basePath == '\\')) {
    $startUrl = "/$username/";
    $scope = "/$username/";
  }

  $manifest = [
    "short_name" => $data['title'],
    "name" => $data['title'],
    "background_color" => "#{$data['background_color']}",
    "theme_color" => "#{$data['theme_color']}",
    "display" => "standalone",
    "icons" => [
      [
        "src" => $data['img_144'],
        "type" => "image/png",
        "sizes" => "144x144",
      ],
      [
        "src" => $data['img_192'],
        "type" => "image/png",
        "sizes" => "192x192",
      ],
      [
        "src" => $data['img_512'],
        "type" => "image/png",
        "sizes" => "512x512",
      ],
    ],
    "start_url" => $startUrl,
    "scope" => $scope,
  ];

  header('Content-Type: application/json');
  echo json_encode($manifest);
}
