#!/bin/bash

# Syntax: gen_forge_table.sh [text]
#
# Add "text" option to write the tables in simple text.
# Without the text option, all output will be markdown.

typeset -r db_file=$(mktemp --dry-run --suffix=.db)

typeset -A sym=([red_circle]=$'\xF0\x9F\x94\xB4'
                [green_circle]=$'\xf0\x9f\x9f\xa2'
                [red_crossmark]=$'\xe2\x9d\x8c'
                [green_checkmark]=$'\xe2\x9c\x85'
                [test_tube]=$'\xf0\x9f\xa7\xaa'
                [cloud_lightening]=$'\xf0\x9f\x8c\xa9'
                [detective]=$'\xf0\x9f\x95\xb5'
                [okhand]=$'\xF0\x9F\x91\x8C'
                [eye]=$'\xf0\x9f\x91\x81'
                [onion]=$'\xF0\x9F\xA7\x85'
                [skull]=$'\xF0\x9F\x92\x80'
                [vomit]=$'\xF0\x9F\xA4\xAE'
                [noentry]=$'\xE2\x9B\x94'
                [warning]=$'\xE2\x9A\xA0'
                [biohaz]=$'\xE2\x98\xA3'
                [hammerpick]=$'\xE2\x9A\x92')

# temporary hack.  git.sdf.org upgraded to 1.14.1 and it broke the icons.
sym+=([red_circle]=n
      [green_circle]=y
      [cloud_lightening]=y
      [eye]=y
      [onion]=onion
      [skull]=${sym[red_crossmark]}
      [noentry]=${sym[red_crossmark]})
                
#local red_circle='๐Ÿ”ด'
#local green_circle='๐ŸŸข'
#local test_tube='๐Ÿงช'
#local cloud_lightening='๐ŸŒฉ'
#local detective='๐Ÿ•ต'
#local okhand='๐Ÿ‘Œ'
#local eye='๐Ÿ‘'
#local onion='๏ฟผ'
#local skull='๐Ÿ’€'
#local noentry='โ›”'

intro()
{
    local lst=$1
    
    case "$lst" in
        white)
            printf %s\\n 'The following forges have no significant ethical issues:'
            ;;
        gray)
            printf %s\\n 'These forges are not as seriously flawed as the blacklisted ones, but they should still be avoided if possible.  Non-Cloudflare sites that use a Cloudflare NS server pose a risk for disruptions because they can trivially and spontaneously flip a switch and route all your traffic through Cloudflare, potentially cutting access to some of your contributors.  Sites that are dead or previously dead are also graylisted because if they come back online, they are known to be unreliable.  Resource deprived instances are graylisted because they may become unstable or unreliable in the future, or if your repo is resource heavy you may be asked to leave.  Codeberg is graylisted for falsely accusing a repository of illegal conduct and deleting the content of all forks from that project without evidence or redress.'
            ;;
        black)
            printf %s\\n 'These forges have severe ethical or trust issues and should be boycotted:'
            ;;
    esac
};#intro

table_populate()
{
    sqlite3 "${db_file}" < input_data/forges.sql
};#table_populate

table_text()
{
    # table_text not used yet, so we don't have it in working order #
    
    # .ft B (bold font)
    # .ft I (italics font)
    # .ft P (previous font)
    # .ll 6i (6 inch width)

    for lst in white gray black
    do
        if [[ "$lst" == black ]]
        then
            url_clause="''"
        else
            url_clause="case when url_clrnet like '_%' then char(10)||'('||replace(url_clrnet,'https://','')||')||||||' else '' end"
        fi
        printf %s "# ${lst^}"'list

```
.TS
box tab(|);
c|c|c|c|c|c.
.ft BI
forge | Tor-    | sensitive info | forced      | forced execution | notes
      | hostile | exposed to     | re/hCAPTCHA | of non-free      |
      |         | CloudFlare     |             | software         |
.ft P
.T&
l|l|l|l|l|lp40.
'
        sqlite3 "${db_file}" "select case when url_clrnet is null then '' else replace(url_clrnet,'https://','[')||']('||url_clrnet||')' end||'(placeholder)',
                                     case when antitor       then 'y' else 'n' end,
                                     case when cflogin       then 'y' else 'n' end,
                                     case when hrecaptcha    then 'y' else 'n' end,
                                     case when forced_nfsw   then 'y' else 'n' end,
                                     case when notes is null then ''  else 'T{
.ll 6i
'|| replace(replace(notes,'[',''),']',' ') ||'
T}' end||
                                     $url_clause
                          from forgesTbl where lst_kind = '$lst'
                          order by url_clrnet collate nocase;"

        printf %s '.TE

```

'
    done
}; #table_text

table_md()
{
    printf %s\\n "# Directory of forges"

    for lst in white gray black
    do
        if [[ "$lst" == black ]]
        then
            name_clause="case when url_clrnet is null then '' else replace(url_clrnet,'https://','') end"
        else
            name_clause="case when url_clrnet is null then '' else replace(url_clrnet,'https://','[')||']('||url_clrnet||')' end"
        fi
        if [[ "$lst" != white ]]
        then
            printf %s\\n "## ${lst^}list"
        fi
        printf %s "
$(intro $lst)"'

| *forge* | *registration publicly open* | *software* | *Tor-hostile* | *Cloudflare MitM* | *forced re/hCAPTCHA* | *forced execution of non-free software* | *notes* |
|---|---|---|---|---|---|---|---|
'
    sqlite3 "${db_file}" "select $name_clause||case when url_onion is null then '' else '([${sym[onion]}]('||url_onion||'))' end,
                                 case when dead then '${sym[skull]}' when (cflogin and hrecaptcha = 'unavoidable') or antitor or nation_discrimination then '${sym[noentry]} (exclusive walled garden)' when not openpubreg then '${sym[red_crossmark]}' else '${sym[green_checkmark]}' end,
                                 case software when 'gitlab_ce' then 'Gitlab (CE)' when 'gitlab_ee' then 'Gitlab (EE)' when 'other' then 'other/unknown' else replace(replace(software,'1.05','1.5'),'1.09','1.9') end,
                                 case when antitor                    then '${sym[eye]}'              else 'n' end,
                                 case when cflogin                    then '${sym[cloud_lightening]}' else 'n' end,
                                 case when hrecaptcha = 'unavoidable' then '${sym[hammerpick]}'       else 'n' end,
                                 case when forced_nfsw                then '${sym[biohaz]}'           when forced_nfsw is null then '?' else 'n' end,
                                 case when notes is null then '|' else notes||'|' end
                          from forgesTbl where lst_kind = '$lst'
                          order by software,url_clrnet collate nocase;"

    # for the future => order by naturalsort(software),url_clrnet...
    printf $'\n'
    done
};#table_md

table_populate

case "$1" in
    txt|text)
        table_text | tbl | nroff -Tascii | uniq
        ;;
    *)
        printf '%s\n\n' '[//]: # (** DO NOT EDIT this file directly! **  It is auto-generated.  Changes should be made to financial_institutions.sql or '"${0##*/}"' instead.)'
        table_md
        printf '\n%s\n' 'โš  Gitea versions note: Gitea 1.13.8 [breaks][gitea-bug] emoji in some browsers. The developers [believe](https://codeberg.org/Codeberg/Community/issues/444#issuecomment-198199) that by not supplying fonts to the web visitors who need them, they are improving server performance.  Of course the server load is only reduced when talking to a browser that does not have the needed fonts.  The idiots could also simply arbitrarily deny service to users at random to get a performance increase.  Obviously they have misunderstood the point of performance in the first place: availability!

The best Gitea version ATM is 1.13.7 but there are no known instances of this.  

[gitea-bug]: https://codeberg.org/Codeberg/Community/issues/444'
        ;;
esac