Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

generate-flatpakrepo-webpage.sh (Source)

#!/bin/sh
# File: /mnt/public/Support/Programs/flatpak-repo/generate-flatpakrepo-webpage.sh
# Location: server3
# Author: bgstack15
# Startdate: 2025-04-04-6 12:02
# SPDX-License-Identifier: GPL-3.0-only
# Title: Generate Webpage for Flatpak Repo
# Purpose: generate index.html for my flatpak repo
# History:
# Usage:
#    REPODIR="/mnt/public/www/internal/repo/flatpak/" sh ./generate-flatpakrepo-webpage.sh
# References:
#    https://gist.github.com/intika/8e4a7faeb72c3a393e42ac9af85b62b7
# Improve:
# Dependencies:
#    dep-devuan: flatpak, ostree
# Documentation:
#    /mnt/public/www/Support/Programs/flatpak-repo/flatpak-repo.md
#    the repo icon should be 128x128 for gnome-software to read it
ferror() {
   printf '%s\n' "${@}" 1>&2
}
REPODIR="${REPODIR:-${1}}" ; test -z "${REPODIR}" && { ferror "Fatal! Please set REPODIR. Aborted." ; exit 1 ; }
# only grab first repo file
_repofile="$( find "${REPODIR}" -maxdepth 1 -mindepth 1 -iname '*.flatpakrepo' -print -quit | head -n1 )"
_reponame="$( basename "${_repofile%%.flatpakrepo}" )"
test -z "${_reponame}" && { ferror "Fatal! Unable to find a *.flatpakrepo file in ${REPODIR}. Aborted." ; }
# If you do not cd, then just add --repo="${REPODIR}"
# If using a repo by name, then you have to add it and update it.
# must have up-to-date info on this client side
flatpak remote-add --user --if-not-exists "${_reponame}" "${_repofile}"
flatpak update --appstream "${_reponame}"
# this env var makes it machine-readable output with tabs
apps="$( FLATPAK_FANCY_OUTPUT=0 flatpak --app remote-ls "${_reponame}" --columns 'name:full,application:full,description:full,version:full,installed-size:full,download-size:full,branch:full,arch:full' )"
# just view the remote-ls by file://
# but this does not show version number!
#flatpak --app remote-ls file:///${REPODIR}
count="$( echo "${apps}" | wc -l )"
cd "${REPODIR}"
index_file="${REPODIR}/index.html"
_fullreponame="$( awk -F'=' '$1~/Title/{print $2}' "${_repofile}" )"
_repowww="$( awk -F'=' '$1~/Url/{print $2}' "${_repofile}" )"
# strip trailing slash; every usage will add it to make sure it is there.
_repowww="${_repowww%%/}"
{
   echo "<html>"
   echo "<head>"
   echo '<meta name="viewport" content="width=device-width, initial-scale=1">'
   echo '<link rel="stylesheet" href="flatpak.css">'
   echo "<title>${_fullreponame}</title>"
   echo "</head>"
   echo "<body>"
   echo "<h1><img src=\"icon.png\" class='headimg'>${_fullreponame}</h1>"
   echo "<a href=\"${_reponame}.flatpakrepo\">Install this repo</a>"
   echo "<pre>flatpak remote-add --if-not-exists --user \"${_reponame}\" \"${_repowww}/${_reponame}.flatpakrepo\"</pre>"
   x=0
   _tmpfile1="$( mktemp )"
   echo "${apps}" | while IFS="$( printf '\t' )" read -r name app description version installsize downloadsize branch arch ;
   do
      x=$(( x + 1 ))
      test $x -eq 1 && {
         printf '%s\n' "<h2>Packages (${count})</h2>"
         printf '%s\n%s\n' "<table>" "<tr><th>Name</th><th>Application ID</th></th><th>Description</th><th>Version</th><th>Installed size</th><th>Download size</th></tr>"
      }
      _reffile="${REPODIR}/files/${app}.flatpakref"
      _iconfile="${REPODIR}/cache/${app}.png"
      # get icon extracted:
      # ref="$( ostree refs --list | grep -vE '\.Debug|^appstream[0-9]*' )" # app/com.example.AppName/x86_64/master
      _ref="app/${app}/${arch}/${branch}"
      _reficonfile="$( ostree ls --repo="${REPODIR}" --nul-filenames-only -R "${_ref}" 2>/dev/null | tr '\0' '\n' | grep --null -iE '\/icons\/.*64.*(png|svg)$' | head -n1 )"
      if test -n "${_reficonfile}" ;
      then
         ostree cat --repo="${REPODIR}" "${_ref}" "${_reficonfile}" > "${_iconfile}"
      fi
      # do nothing to _iconfile if we do not have material to generate it. A 404, or any previous image left behind, will suffice.
      printf '%s' "<td><a href=\"files/${app}.flatpakref\">"
      test -n "${_reficonfile}" && { printf '%s' "<img src=\"cache/${app}.png\">" ; }
      printf '%s\n' "${name}</a></td><td>${app}</td><td>${description}</td><td>${version}</td><td>${installsize}</td><td>${downloadsize}</td>"
      {
         echo "[Flatpak Ref]"
         echo "Title=${name}"
         echo "Name=${app}"
         echo "Branch=${branch}"
         echo "Url=${_repowww}/"
         echo "RuntimeRepo=${_repowww}/${_reponame}.flatpakrepo"
         echo "IsRuntime=false" # case sensitive, must be lowercase "false"
      } > "${_reffile}"
      echo "${x}" > "${_tmpfile1}"
   done
   x="$( cat "${_tmpfile1}" )"
   rm -f "${_tmpfile1}"
   test $x -gt 0 && {
      printf '%s\n' "</table>"
   }
   echo "</body>"
   echo "<footer>"
   _now="$( TZ=UTC date "+%FT%TZ" )"
   echo "Last generated: ${_now}<br/>"
   echo "</footer>"
   echo "</html>"
} > "${index_file}"
ferror "Generated at ${_now} with ${x} packages."