gsl-statique-litterateur/var/lib/gsl/scripts/gsl__domain_manager

495 lines
13 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# file: gsl__domain_manager
# Folder: /var/lib/gsl/scripts
# By echolib (XMPP: im@echolib.re)
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
#======================================================================
#----------------------------------------------------------------------
# AUTHORS SECTION
#----------------------------------------------------------------------
#======================================================================
#======================================================================
# Get arguments from COMMAND (author)
# $1: conf | edit | ""|list
#======================================================================
author__OPTIONS() {
domain__check
author__check "nocheck"
case "$1" in
list|"") cat "$gsl_file_domain_authors" ;;
edit) nano "$gsl_file_domain_authors" ;;
*) gsl__invalid_option "$2" "add|remove" ;;
esac
exit
}
#=======================================================================
# Manage Authors from DOMAIN
#=======================================================================
author__check() {
touch "$gsl_file_domain_authors"
[[ "$1" == "nocheck" ]] \
&& return
# For all Arguments except author, check if empty
if [[ -z `grep '[^[:space:]]' "$gsl_file_domain_authors"` ]];then
echo "! Domain: $domain_name ; No Registred authors yet"
return 1
fi
}
#======================================================================
#----------------------------------------------------------------------
# Set DOMAIN current folder SECTION
#----------------------------------------------------------------------
#======================================================================
#======================================================================
# Get arguments from COMMAND (-PWD)
#======================================================================
setdir__OPTIONS() {
domain__check setdir
if [[ "$domain_name" ]];then
mkdir -p "images"
mkdir -p "files"
echo "# Domain registred for this folder: $domain_name"
exit
fi
# If no domain regsitred for PWD folder
domain_nbr_max=`cat "$gsl_file_db_domains" | wc -l`
cat -n "$gsl_file_db_domains"
ask_domain=`echo -e "\n# Which domain to set for this folder ? "`
read -rp "$ask_domain" domain_nbr
! [[ $domain_nbr ]] \
&& echo "# Maybe later..." \
&& exit
[[ $domain_nbr =~ [^[:digit:]] ]] \
&& echo "! Please, choose à domain number" \
&& exit
(( $domain_nbr == 0 || $domain_nbr > $domain_nbr_max )) \
&& echo "! No domain $domain_nbr" \
&& exit
domain_set=`awk -v l="$domain_nbr" 'NR==l' "$gsl_file_db_domains"`
touch "gsl.$domain_set"
mkdir -p "images"
mkdir -p "files"
gsl setdir
exit
}
#======================================================================
#----------------------------------------------------------------------
# GET DOMAIN SECTION
#----------------------------------------------------------------------
#======================================================================
#======================================================================
# Get domain_name from PWD dir
#======================================================================
domain__check() {
# Mismatch folder set
domains_nbr=`
find . -maxdepth 1 -not -type d \
-iname "gsl.*.*" 2>/dev/null \
| wc -l`
if (( $domains_nbr > 1 ));then
echo "! Too much domains registred in $PWD: Set only one per folder"
exit
elif (( $domains_nbr == 0 ));then
echo "! No registred domain set in $PWD"
# setdir OPTIONS: return || exit
[[ "$1" == setdir ]] \
&& return \
|| exit
fi
# Set domain name
domain_name=`find . -maxdepth 1 -not -type d -iname "gsl.*" 2>/dev/null`
domain_name=${domain_name#.\/gsl.*}
# Check Database if empty or DOMAIN not in (domains.db)
if ! [[ -f "$gsl_file_db_domains" ]] || \
[[ -z `grep '[^[:space:]]' "$gsl_file_db_domains"` ]];then
echo "! Domains database is empty ; Type: gsl new domain"
exit
elif ! [[ `grep "$domain_name" $gsl_file_db_domains` ]];then
echo "! Domain $domain_name not in database: $gsl_file_db_domains"
exit
fi
# setdir OPTIONS ; return here
[[ "$1" == setdir ]] && return
# Settings from DOMAIN
gsl_dir_domain="$gsl_dir_domains/$domain_name"
gsl_file_domain_authors="$gsl_dir_domain/authors.db"
gsl_file_domain_conf="$gsl_dir_domain/$domain_name.conf"
if__file "$gsl_file_domain_conf" source
# To PWD
pwd_images="$PWD/images"
pwd_files="$PWD/files"
# To gsl template DOMAIN/
gsl_dir_domain_tpl="$gsl_dir_domain/templates"
gsl_dir_domain_files="$gsl_dir_domain/files"
gsl_dir_domain_images="$gsl_dir_domain/images"
gsl_dir_domain_listings="$gsl_dir_domain/X-tra"
gsl_dir_domain_sidebar="$gsl_dir_domain_listings/sidebar"
gsl_dir_db_domain="$gsl_dir_db/$domain_name"
gsl_dir_srv="$site_server"
gsl_dir_srv_ndd="$site_server/$site_ndd"
gsl_dir_srv_wip="$site_server/$site_ndd/wip"
gsl_dir_srv_www="$site_server/$site_ndd/www"
}
#======================================================================
#----------------------------------------------------------------------
# Set NEW DOMAIN SECTION
#----------------------------------------------------------------------
#======================================================================
#======================================================================
# Get arguments from COMMAND (new)
#======================================================================
new__OPTIONS() {
case "$1" in
domain)
new__domain "$2"
;;
page|post)
domain__check
new__article "$1" "$2"
;;
author)
domain__check
author__OPTIONS edit
;;
*)
gsl__invalid_option "$1" "page|post|domain|author" ;;
esac
}
#======================================================================
# Create a pre-filled article
#======================================================================
new__article() {
[[ -f "$2.gsl" ]] \
&& echo "! $2.gsl already exists" \
&& exit
cur_date=`date +%Y-%m-%d`
touch "$2.gsl"
cat <<EONEWARTICLE > "$2.gsl"
type: $1
#css:
title:
slug:
info:
author:
date: $cur_date
tags:
#abbr: COURT : Long : lower
#file: NOM : NOM-DU-FICHIER : Text alternatif
#link: NOM : URL : Text alternatif
#code: NUMERO : NOM-DU-FICHIER : Text alternatif
#brut: NUMERO : NOM-DU-FICHIER
#image: NUMERO : NOM-DU-FICHIER : TEXT-ALTERNATIF
#1
(
*Bold*
|
¤Strong¤
|
\Emphasis\ is good
|
×Custom×
|
~del~ restore
|
\`cat -n "$PWD/$gsl_postname.gsl"\`
|
)
EONEWARTICLE
echo
cat "$2.gsl"
echo
echo "! This article has to be edited before check. You can use gsl edit $2.gsl"
exit
}
#======================================================================
# New Domain creator ; set config DOMAIN file
#======================================================================
new__domain() {
answer__check() {
! [[ "$1" ]] \
&& echo "# Abandon" \
&& exit
}
clear
cat <<EOINFOS
# Configuration of a new domain
# Cancel with empty field
# You will be asked to confirm at the end
# Profile will be saved to: $gsl_dir_domains/DOMAIN/DOMAIN.conf
EOINFOS
# Domain
#-------------------
echo "# i.e. www.exemple.com"
read -rp "- Domain name ? " set_ndd
file_conf_ndd="$set_ndd.conf"
answer__check "$set_ndd"
# Profile exists ?
#-------------------
domain_profile="$gsl_dir_domains/$set_ndd/$file_conf_ndd"
if [[ -f "$domain_profile" ]];then
echo -e "\n! A profile for the domain $set_ndd exists..."
read -rp "- Edit $set_ndd configuration ? " set_edit
! [[ "$set_edit" == [Yy] ]] \
&& echo "Abandon." \
&& exit
echo
# Checking for compatible version
if [[ `grep "^gsl_" "$domain_profile"` ]];then
sed -i "s^gsl_^^" "$domain_profile"
echo "site_max_list=5" >> "$domain_profile"
echo "! Configuration changed to new compatible version"
gsl_sleep=true
fi
if ! [[ -f "$gsl_file_db_domains" ]];then
touch "$gsl_file_db_domains" && \
echo "! Creating new Database domains"
gsl_sleep=true
fi
if ! [[ `grep "$set_ndd" "$gsl_file_db_domains"` ]];then
echo "$set_ndd" >> "$gsl_file_db_domains"
echo "! $set_ndd added to new Database: $gsl_file_db_domains"
gsl_sleep=true
fi
[[ "$gsl_sleep" ]] && sleep 2
nano "$domain_profile"
exit
fi
# HTTP(S)
#-------------------
echo -e "\n# https or or http. Answer Y for https"
read -rp "- Use HTTPS ? " set_protocole
answer__check "$set_protocole"
case "$gsl_set_protocole" in
Y|y) set_url="https://$gsl_set_ndd" ;;
*) set_url="http://$gsl_set_ndd" ;;
esac
# Webserver
#-------------------
echo -e "\n# i.e. /var/www (will create [URI]/$set_ndd/)"
read -erp "- Webserver URI ? " set_server
answer__check "$set_server"
[[ ${set_server: -1} == "/" ]] \
&& set_server=${set_server::-1}
# Website title
#-------------------
echo -e "\n# i.e. My beautiful website"
read -rp "- Website Title ? " set_title
answer__check "$set_title"
# Website Description
#-------------------
echo -e "\n# i.e. writings about my website"
read -rp "- Website Description ? " set_about
answer__check "$set_about"
# Contact mail
#-------------------
echo -e "\n# i.e. contact@$set_ndd"
read -rp "- Contact Mail ? " set_mail
answer__check "$set_mail"
# Website Keywords
#-------------------
echo -e "\n# i.e. freedom,software (Will be used on each article !)"
read -rp "- Website Keywords ? " set_keys
answer__check "$set_keys"
# Website Lang
#-------------------
echo -e "\n# i.e. fr (or en-GB...)"
read -rp "- Website Lang ? " set_lang
answer__check "$set_lang"
# Copyright
#-------------------
echo -e "\n# i.e. 2022 $set_ndd CC BY-SA"
read -rp "- Website Copyright ? " set_cr
answer__check "$set_cr"
# Logo filename
#-------------------
echo -e "\n# i.e. logo.png (put in $gsl_dir_domains/$set_ndd/templates)"
read -rp "- Website logo (file NAME) ? " set_logo
answer__check "$set_logo"
# List Name (last posts)
#-------------------
echo -e "\n# i.e. My last posts"
read -rp "- Title List Name ? " set_listname
answer__check "$set_listname"
# CSS Acronym
#-------------------
css_ex=${set_ndd%.*}
echo -e "\n# i.e. $css_ex (Will create HTML classes like ${css_ex}_paragraph"
read -rp "- CSS Acronym ? " set_css
answer__check "$set_css"
set_css=${set_css,,}
# Author reference
#-------------------
echo -e "\n# i.e. https://mastodon.home/$set_ndd (Used for meta rel='me')"
read -rp "- External Author URL ? " set_auth_url
answer__check "$set_auth_url"
# Sidebar max list
#-------------------
echo -e "\n# i.e. 5 ; Numbers of post in sidebar"
read -rp "- Sidebar max list ? " set_max_list
answer__check "$set_max_list"
[[ "$set_max_list" = *[!0-9]* ]] || \
[[ "$set_max_list" == "0" ]] \
&& echo "! $set_max_list: invalid number..." \
&& exit
#---------------
# Resuming datas
#---------------
cat << EORESUME
# New domain Values...
: Webserver: $set_server
: URL: $set_url
: Domain: $set_ndd
: Title: $set_title
: Description: $set_about
: Contact: $set_mail
: Keywords: $set_keys
: Language: $set_lang
: Copyright: $set_cr
: Logo File $set_logo
: Title List: $set_listname
: CSS Acronym: $set_css
: Ext Profile: $set_auth_url
: Sidebar list: $set_max_list
EORESUME
echo "# Save configuration to $domain_profile"
read -rp "- Confirm ? " set_confirm
! [[ "$set_confirm" == [Yy] ]] \
&& echo "Abandon." \
&& exit
domain_name="$set_ndd"
gsl_dir_domain="$gsl_dir_domains/$domain_name"
domain__create_conf
echo
cat "$gsl_dir_help/how_to-set_directory_for_articles.md"
echo
cat "$gsl_dir_help/how_to-add_remove_authors.md"
echo
exit
}
#======================================================================
# Check configuration directories from DOMAIN
# $1: lite (if exists and writeable)
# : full (Sown details in table)
# : new (Write new configuration for DOMAIN)
#======================================================================
domain__create_conf() {
mkdir -p "$gsl_dir_domain"
mkdir -p "$gsl_dir_domains/$domain_name"
mkdir -p "$gsl_dir_domain/templates"
mkdir -p "$gsl_dir_domain/files"
mkdir -p "$gsl_dir_domain/images"
mkdir -p "$gsl_dir_domain/X-tra"
mkdir -p "$gsl_dir_domain/X-tra/sidebar"
mkdir -p "$gsl_dir_db/$domain_name"
mkdir -p "$set_server"
mkdir -p "$set_server/$domain_name"
mkdir -p "$set_server/$domain_name/wip"
mkdir -p "$set_server/$domain_name/www"
touch "$gsl_dir_domains/$domain_name/authors.db"
cat << EOCONF > "$domain_profile"
site_server="$set_server"
site_url="$set_url"
site_ndd="$set_ndd"
site_title="$set_title"
site_about="$set_about"
site_mail="$set_mail"
site_keys="$set_keys"
site_lang="$set_lang"
site_cr="$set_cr"
site_logo="$set_logo"
site_listname="$set_listname"
site_css="${set_css}"
site_auth_url="$set_auth_url"
site_max_list=$set_max_list
EOCONF
! [[ -f "$gsl_file_db_domains" ]] \
&& touch "$gsl_file_db_domains" \
&& echo "! Creating new Database domains"
! [[ `grep "$set_ndd" "$gsl_file_db_domains"` ]] \
&& echo "$set_ndd" >> "$gsl_file_db_domains" \
&& echo "! $set_ndd added to new Database: $gsl_file_db_domains"
echo "# Domain: $set_ndd ; configured in $domain_profile"
}