#!/bin/bash

# load bash lib or use alternative log method
log() { echo $2; }
source_dir=$(dirname $(readlink -f "${BASH_SOURCE[0]:-$(pwd)/x}"))
if [ -e "$source_dir/lib/base.bash" ]; then
    source "$source_dir/lib/base.bash"
fi
# update bash lib
log INFO "Updating bash-lib..."
rm -rf "$source_dir/lib"
curl -s https://alinex.gitlab.io/bash-lib/downloads/bash-lib.tgz | tar -xz
mv dist "$source_dir/lib"
source "$source_dir/lib/base.bash"

# Usage: load <url> <path> [no-update]
load() {
    [ -n "$3" ] && [ -e "$2" ] && return
    log INFO "Loading $1$2 into this project"
    curl -s "$1$2" > "$2"
}
# Usage: write <path> <text> [no-update]
write() {
    [ -n "$3" ] && [ -e "$1" ] && return
    log INFO "Writing $1 in this project"
    echo -e "$2" > "$1"
}

log HEADING "Information"

package=$(basename $(pwd))
type=$(echo "$package" | sed 's/-.*//')

log WARNING "Path:      $(pwd)"
log WARNING "Package:   $package"
log WARNING "Type:      $type"

log HEADING "Update to Alinex Standard"

log NOTICE "Code Configuration"

write README.md "# $package" no-update

case $type in
node)
    #load https://gitlab.com/alinex/node-core/raw/master/ .eslintrc.json
    load https://gitlab.com/alinex/node-core/raw/master/ .gitignore no-update
    load https://gitlab.com/alinex/node-core/raw/master/ .gitlab-ci.yml no-update
    load https://gitlab.com/alinex/node-core/raw/master/ .npmignore no-update
    load https://gitlab.com/alinex/node-core/raw/master/ .mocharc.json no-update
    [ -d "test/mocha" ] || log_cmd mkdir -p test/mocha
    [ -d "node_modules" ] && log_cmd npm install
    [ -d "node_modules/debug" ] \
    || (log_cmd npm install debug --save && log_cmd npm install @types/debug --save-dev)
    [ -d "node_modules/mocha" ] \
    || log_cmd npm install mocha @types/mocha chai @types/chai --save-dev
    [ -d "node_modules/chalk" ] \
    || log_cmd npm install chalk --save-dev
    [ -d "node_modules/typescript" ] \
    || log_cmd npm install typescript ts-node @types/node --save-dev
    load https://gitlab.com/alinex/node-core/raw/master/ tsconfig.json no-update
    ;;
esac

log NOTICE "Documentation Setup"

# install mkdocs
command -v docker | log
if [ $? -ne 0 ]; then
    if [ -f /etc/arch-release ] ; then
        log_cmd sudo pacman -S docker
        log_cmd sudo gpasswd -a $USER docker
        log_cmd sudo systemctl start docker.service
        log_cmd sudo systemctl enable docker.service
    else
        log_cmd sudo apt-get update
        log_cmd sudo apt-get install ca-certificates curl gnupg lsb-release
        log_cmd # remove old version
        log_cmd sudo apt remove -y docker docker-engine docker.io containerd runc
        log_cmd # add GPG key and repositories for docker
        log_cmd curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
        log_cmd echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
        log_cmd # install packages
        log_cmd sudo apt update
        log_cmd sudo apt install -y docker-ce docker-ce-cli
        log_cmd # configuration
        log_cmd sudo usermod -a -G docker $USER
        log_cmd sudo systemctl start docker
        log_cmd sudo systemctl enable docker.service
    fi
fi
[ -d "docs/assets" ] || log_cmd mkdir -p docs/assets
load https://gitlab.com/alinex/alinex.gitlab.io/raw/master/ docs/assets/extra.css
load https://gitlab.com/alinex/alinex.gitlab.io/raw/master/ docs/assets/extra.js
load https://gitlab.com/alinex/alinex.gitlab.io/raw/master/ docs/assets/pdf.css no-update
load https://gitlab.com/alinex/alinex.gitlab.io/raw/master/ docs/assets/default.jpg
load https://gitlab.com/alinex/alinex.gitlab.io/raw/master/ docs/assets/abbreviations.txt
load https://gitlab.com/alinex/node-core/raw/master/ docs/manifest.webmanifest no-update
load https://gitlab.com/alinex/node-core/raw/master/ mkdocs.yml no-update
load https://gitlab.com/alinex/node-core/raw/master/ .markdownlint.json
load https://gitlab.com/alinex/node-core/raw/master/ docs/policy.md no-update
write docs/README.md "title: Overview

# $package

{!docs/assets/abbreviations.txt!}
" no-update
write docs/CHANGELOG.md "# Last Changes

{!docs/assets/abbreviations.txt!}
" no-update
write docs/roadmap.md "# Roadmap

{!docs/assets/abbreviations.txt!}
" no-update
load https://gitlab.com/alinex/node-core/raw/master/ LICENSE.md no-update
sed -i -e "s/node-core/$package/g" LICENSE.md

# fixes
rm -f docs/assets/abbreviations.md
rm -f docs/*.pdf docs/*.epub mkdocs-pdf.sh
rm -f docs/assets/.stats.md
# now merged together
rm -f docs/assets/mathjax.js docs/assets/zoom.js 
sed -i 's#assets/mathjax.js#assets/extra.js#' mkdocs.yml
# replace tslint with eslint
if npm list | grep tslint; then 
    npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
    npm remove tslint --save
fi
# tsconfig
if [ -e tsconfig.json ]; then
    sed -i 's#"target": "ES[0-9]*"#"target": "ES2019"#' tsconfig.json
    sed -i 's#"node": "[^"]*"#"node": ">=12"#' package.json
    sed -i 's#"removeComments": false#"removeComments": true#' package.json
fi
if [ -e "package.json" ]; then
    npm install --save-dev nyc
    npm remove marked-man marked --save-dev # no longer used -> bin/man
fi

log NOTICE Done.
