#!/bin/sh set -euo pipefail export MM_SERVER=zeus.mattermost.gent mattermost_name="Zeus Mattermost" team_name="zeus" dir="$(dirname "$0")" out="$dir/dist/channels/index.html" mkdir -p "$(dirname "$out")" cp go_to_channel.png dist/ cp main.css dist/ custom_emoji_file="$(mktemp --tmpdir custom_emoji.XXXXXXXXXX.json)" mmcli listcustomemoji | jq -s > "$custom_emoji_file" jqo() { json="$1" shift 1 printf '%s' "$json" | jq -cr "$@" || { printf 'jq errored performing %s on this input: %s\n' "$*" "$json" >&2; } } htmlescape() { sed 's/&/\&/g; s//\>/g; s/"/\"/g; s/'"'"'/\'/g' } emoji() { env --chdir "$dir" \ python3 "$dir/emoji.py" "$custom_emoji_file" } markdown() { htmlescape | \ { pandoc --from=markdown+autolink_bare_uris+emoji --to=html || printf "Markdown conversion failed\n" >&2; } | \ sed -E ' s|<(h[0-6])[^>]*>|
|g s||
|g s/src="\(.*\)"/src=""/g ' | \ sed -f "$channels_sed_file" | \ emoji } format_number() { awk '{printf "%'\''d\n", $1}' } format_timestamp() { format_str="$1" xargs -Ixxx date --date "@xxx" "+$format_str" } channels="$(mmcli ls "$team_name" | jq -cs 'sort_by(- .last_post_at) | .[]')" channel_count="$(jqo "$channels" -s 'length')" channels_sed_file="$(mktemp --tmpdir "mattermost_channels.XXXXXXXXX.sed")" jqo "$channels" -s 'map(.name) | sort_by(length) | reverse | .[]' | sed 's|.*|s/~&/CHANNEL[&]<\\/a>/g|' > "$channels_sed_file" printf 's/CHANNEL\\[\\([^]]*\\)\\]/~\\1/g\n' >> "$channels_sed_file" substitute_vars() { sed ' s/$lastupdated/'"$(date '+%Y-%m-%d %H:%M')"'/g s/$mattermost_name/'"$mattermost_name"'/g s/$team_name/'"$team_name"'/g s/$channel_count/'"$channel_count"'/g ' } substitute_vars < "$dir"/header.html > "$out" printf '%s\n' "$channels" | while IFS="" read -r chan || [ -n "$chan" ]; do if [ "$(jqo "$chan" '.type')" != O ]; then continue; fi name="$(jqo "$chan" '.name' | htmlescape)" display_name="$(jqo "$chan" '.display_name' | htmlescape)" purpose="$(jqo "$chan" '.purpose' | markdown)" header="$(jqo "$chan" '.header' | markdown)" create_at="$(jqo "$chan" '.create_at / 1000' | format_timestamp '%Y-%m-%d')" last_post_at="$(jqo "$chan" '.last_post_at / 1000' | format_timestamp '%Y-%m-%d %H:%M')" total_msg_count="$(jqo "$chan" '.total_msg_count' | format_number)" cat >> "$out" <

$display_name Go to channel

HERE if [ -n "$purpose" ]; then cat >> "$out" <
Purpose
$purpose
HERE fi if [ -n "$header" ]; then cat >> "$out" <
Header
$header
HERE fi cat >> "$out" <
Created on
$create_at
Last message
$last_post_at
Amount of messages
$total_msg_count
HERE done substitute_vars < "$dir"/footer.html >> "$out"