stl-statilque-litterateur/var/lib/stl/scripts/manage__domain

510 lines
13 KiB
Plaintext
Raw Permalink Normal View History

2022-07-20 19:37:21 +02:00
#!/bin/bash
# Name: Statique Littérateur
# Type: Domain manager
# file: manage__domain
# Folder: /var/lib/stl/scripts/
# By echolib (XMPP: im@echolib.re)
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
2022-08-23 16:48:33 +02:00
#------------
# funny stats
#------------
# lines: 509
# functions: 7
#-----------------------------------------------------------------------
#**********************************************************************
2022-07-20 19:37:21 +02:00
#======================================================================
# --------------
# DOMAIN Section
# --------------
#======================================================================
#----------------------------------------------------------------------
# domain OPTIONS | Manage arguments from command line
# $1: main argument
# $2: change
#----------------------------------------------------------------------
domain__OPTIONS() {
case "$2" in
2022-08-23 16:48:33 +02:00
'') domain_show=true ;;
new) domain_show=true;domain_add=true;
[[ "$3" ]] && domain_name="$3" ;;
edit) edit__OPTIONS "" conf ;;
*) noarg "$2" "Use: new, edit" ;;
2022-07-20 19:37:21 +02:00
esac
domain__get
}
2022-08-23 16:48:33 +02:00
#----------------------------------------------------------------------
# Domain is check from argument
#----------------------------------------------------------------------
domain__pass() {
case "$1" in
missing)
[[ $domain_add ]] \
&& domain__set \
|| exit 0
;;
show)
[[ $domain_show ]] \
&& echo "# Domain '$domain_name' set for this directory: $PWD" \
&& exit
;;
esac
}
2022-07-20 19:37:21 +02:00
#----------------------------------------------------------------------
# Check for PWD config file and source
# Indepdant call: just source config domain file (exit, if not exists)
#----------------------------------------------------------------------
domain__get() {
if [[ -f "$stl_file_pwd_conf" ]];then
source "$stl_file_pwd_conf" # Source config PWD file
2022-08-23 16:48:33 +02:00
domain__pass show
2022-07-20 19:37:21 +02:00
else
2022-08-09 17:17:23 +02:00
echo "# No domain set for this directory: $PWD"
2022-08-23 16:48:33 +02:00
domain__pass missing
2022-07-20 19:37:21 +02:00
fi
}
#----------------------------------------------------------------------
# New domain creation
# Create .stl.conf in PWD directoryy
# Register settings for domain
#----------------------------------------------------------------------
domain__set() {
if__writeable "$PWD"
# Check if anwser is not empty, else exit
# $1: answer
# ---------------------------------------
is__set() {
! [[ "$1" ]] \
&& echo "# Maybe later..." \
&& exit
}
# If DOMAIN already have protocol
# $1
# -------------------------------
is__protocol() {
[[ "${domain_name: -1}" == "/" ]] \
&& domain_name=${domain_name::-1}
2022-08-23 16:48:33 +02:00
2022-07-20 19:37:21 +02:00
# Confirm, configuration not already exists
if [[ -f "$stl_dir_db/$domain_name/$domain_name.conf" ]];then
source "$stl_dir_db/$domain_name/$domain_name.conf"
if [[ "$PWD" != "$conf_pwd" ]];then
echo "! A configuration for '$domain_name' exists in $pwd_conf"
exit
fi
fi
case "$domain_name" in
"http://"*)
domain_protocol="http://"
domain_name=${domain_name/$domain_protocol/}
;;
"https://"*)
domain_protocol="https://"
domain_name=${domain_name/$domain_protocol/}
;;
esac
if [[ "$domain_protocol" ]];then
domain_name=${domain_name/$domain_protocol/}
domain_ask+="$domain_name with protocol $domain_protocol"
else
read -rp "- Use 'https://' protocol (N for 'http://') ? " set_prot
case "$set_prot" in
2022-08-09 17:17:23 +02:00
Y|y) domain_protocol="https://" ;;
N|n) domain_protocol="http://" ;;
2022-08-24 12:38:38 +02:00
*) echo "# Maybe later...";exit ;;
2022-07-20 19:37:21 +02:00
esac
domain_ask+="$domain_name"
fi
}
# ----------
# Processing question to config domain
# ----------
2022-08-09 17:17:23 +02:00
printf '\n%s\n%s\n\n' \
"# You will be asked to confirm after resume" \
"# Empty anwser or mismatch value cancel process !"
2022-07-20 19:37:21 +02:00
# DOMAIN is set in command line ?
2022-08-09 17:17:23 +02:00
# ----------
2022-07-20 19:37:21 +02:00
case "$domain_name" in
'')
2022-08-09 17:17:23 +02:00
read -rp "- Which domain to set (i.e. www.example.com) ? " domain_name
2022-07-20 19:37:21 +02:00
is__set "$domain_name"
is__protocol
;;
*)
domain_ask="- Add your domain here: "
is__protocol
read -rp "$domain_ask (Y|*) ? " domain_confirm
case "$domain_confirm" in
2022-08-24 12:38:38 +02:00
Y|y) true ;;
2022-08-09 17:17:23 +02:00
*) echo "# Maybe later...";exit ;;
2022-07-20 19:37:21 +02:00
esac
;;
esac
# Where is the server ? /var/www ?
2022-08-09 17:17:23 +02:00
# ----------
2022-07-20 19:37:21 +02:00
read -erp "- Where is your server URI ? " srv_uri
is__set "srv_uri"
[[ "${srv_uri: -1}" == "/" ]] \
&& srv_uri=${srv_uri::-1}
! [[ -d "$srv_uri" ]] \
2022-08-09 17:17:23 +02:00
&& echo "! Missing directory: $srv_uri" \
2022-07-20 19:37:21 +02:00
&& exit
if__writeable "$srv_uri"
domain_srv="$srv_uri/$domain_name"
2022-08-09 17:17:23 +02:00
2022-07-20 19:37:21 +02:00
# Installation: server or local
2022-08-09 17:17:23 +02:00
# ----------
read -rp "- STL server (Y|N for local = no WWW, WIP only) ? " install
2022-07-20 19:37:21 +02:00
case "$install" in
Y|y) stl_install="server" ;;
N|n) stl_install="local" ;;
*) echo "# Maybe later..." ;;
2022-07-20 19:37:21 +02:00
esac
2022-08-09 17:17:23 +02:00
2022-07-20 19:37:21 +02:00
# Domain TITLE
2022-08-09 17:17:23 +02:00
# ----------
2022-07-20 19:37:21 +02:00
read -rp "- Domain Title ? " domain_title
is__set "$domain_title"
2022-08-09 17:17:23 +02:00
[[ "$domain_title" =~ \" ]] \
&& domain_title=`sed -e 's^"^\\\\"^g' <<<"$domain_title"`
2022-07-20 19:37:21 +02:00
# Domain ABOUT
2022-08-09 17:17:23 +02:00
# ----------
2022-07-20 19:37:21 +02:00
read -rp "- Domain About ? " domain_about
is__set "$domain_about"
2022-08-09 17:17:23 +02:00
[[ "$domain_about" =~ \" ]] \
&& domain_about=`sed -e 's^"^\\\\"^g' <<<"$domain_about"`
2022-07-20 19:37:21 +02:00
# Domain LANG
2022-08-09 17:17:23 +02:00
# ----------
read -rp "- Domain lang (i.e. fr ; 2 characters) ? " domain_lang
2022-07-20 19:37:21 +02:00
is__set "$domain_lang"
2022-08-09 17:17:23 +02:00
! (( ${#domain_lang} == 2 )) \
&& echo "! Lang must be 2 characters (i.e. fr, gb, en...)" \
&& exit
2022-07-20 19:37:21 +02:00
# Domain MAIL
2022-08-09 17:17:23 +02:00
# ----------
2022-07-20 19:37:21 +02:00
read -rp "- Domain mail ? " domain_mail
is__set "$domain_mail"
2022-08-09 17:17:23 +02:00
! [[ "$domain_mail" =~ @ ]] \
&& echo "! Mail must contain @..." \
&& exit
2022-07-20 19:37:21 +02:00
# Domain TAGS
2022-08-09 17:17:23 +02:00
# ----------
read -rp "- Domain MAIN tags (comma separated) ? " domain_tags
2022-07-20 19:37:21 +02:00
is__set "$domain_tags"
2022-08-09 17:17:23 +02:00
# Domain Copyright
# ----------
read -rp "- Domain copyright (i.e. CC BY-NC-SA) ? " domain_cr
2022-07-20 19:37:21 +02:00
is__set "$domain_cr"
2022-08-09 17:17:23 +02:00
[[ "$domain_cr" =~ \" ]] \
&& domain_cr=`sed -e 's^"^\\\\"^g' <<<"$domain_cr"`
2022-07-20 19:37:21 +02:00
# Domain SIDEBAR TITLE
2022-08-09 17:17:23 +02:00
# ----------
read -rp "- Domain sidebar title (i.e. last news) ? " domain_sidebar_title
2022-07-20 19:37:21 +02:00
is__set "$domain_sidebar_title"
2022-08-09 17:17:23 +02:00
# Replace " with \" if exists
[[ "$domain_sidebar_title" =~ \" ]] \
&& domain_sidebar_title=`sed -e 's^"^\\\\"^g' <<<"$domain_sidebar_title"`
2022-07-20 19:37:21 +02:00
2022-08-09 17:17:23 +02:00
# Domain SIDEBAR title NUMBERS
# ----------
read -rp "- Domain sidebar items max number (1-24) ? " domain_sidebar_items
2022-07-20 19:37:21 +02:00
is__set "$domain_sidebar_items"
! [[ $domain_sidebar_items =~ ^[0-9]+$ ]] \
&& echo "! $domain_sidebar_items is not digits between 1-24" \
&& exit
! (( $domain_sidebar_items >= 1 && $domain_sidebar_items <= 24 )) \
&& echo "! $domain_sidebar_items is not between 1-24" \
&& exit
2022-08-09 17:17:23 +02:00
# Domain CSS PREFIX
2022-08-09 17:17:23 +02:00
# ----------
read -rp "- Domain CSS prefix (alpha numeric and -) ? " domain_css
2022-07-20 19:37:21 +02:00
is__set "$domain_css"
! [[ $domain_css =~ ^[a-zA-Z0-9_-]+$ ]] \
&& echo "! '$domain_css' is not alpha numeric. '-' is authorized" \
&& exit
2022-08-09 17:17:23 +02:00
domain_css=${domain_css,,}
2022-07-20 19:37:21 +02:00
case "${domain_css: -1}" in
"_"|"-") domain_css=${domain_css::-1} ;;
esac
# Domain LOGO
2022-08-09 17:17:23 +02:00
# ----------
read -rp "- Domain logo filename (i.e. mylogo.png) ? " domain_logo
2022-07-20 19:37:21 +02:00
# External URL Profile
2022-08-09 17:17:23 +02:00
# ----------
read -rp "- Domain external URL Profile (empty if unknown) ? " domain_ext_url
2022-07-20 19:37:21 +02:00
# Statoolinfos
2022-08-09 17:17:23 +02:00
# ----------
read -rp "- Generate file stats for Statoolinfos (Y|N) ? " domain_stats
case "$domain_stats" in
Y|y) domain_stats="yes" ;;
N|n) domain_stats="no" ;;
*) echo "# Maybe later..." ;;
esac
2022-08-09 17:17:23 +02:00
# ----------
2022-07-20 19:37:21 +02:00
# Resume before writing conf
2022-08-09 17:17:23 +02:00
# ----------
2022-07-20 19:37:21 +02:00
clear
echo -e "# Resume configuration...\n"
cat <<EODOMAINCONF
STL Inslallation : $stl_install
STL stats generator : $domain_stats
2022-08-09 17:17:23 +02:00
Domain Directory : $PWD/
Domain Articles : $PWD/articles/
Domain Generic files : $PWD/articles/files (for PDF, scripts...)
Domain Generic images: $PWD/articles/images (for png, jpg, svg...)
2022-07-20 19:37:21 +02:00
Domain : $domain_name
URL : $domain_protocol$domain_name
Server URI : $domain_srv
2022-08-09 17:17:23 +02:00
Server WIP : $domain_srv/wip/
$(
if [[ $stl_install == "server" ]];then
echo "Server WWW : $domain_srv/www/"
fi
)
2022-07-20 19:37:21 +02:00
Title : $domain_title
About : $domain_about
Lang : $domain_lang
Mail : $domain_mail
Tags : $domain_tags
Copyright : $domain_cr
Sidebar Name : $domain_sidebar_title
Sidebar Items : $domain_sidebar_items
2022-08-23 16:48:33 +02:00
Sidebar load file : $PWD/articles/sidebar/stl.sidebar
2022-08-09 17:17:23 +02:00
CSS prefix : $domain_css (configuree your own style.css)
$(
if ! [[ $domain_logo ]];then
echo "Logo filename : To add one, type stl domain edit and put it in $domain_srv/wip/template/"
else
echo "Logo filename : $domain_logo (put it in $domain_srv/wip/template/)"
fi
)
2022-07-20 19:37:21 +02:00
External URL Profile : $domain_ext_url
EODOMAINCONF
echo -e "! Use $domain_name only in $PWD"
2022-08-09 17:17:23 +02:00
read -rp "- Write to $PWD/.stl.conf (Y|*) ? " set_conf
2022-07-20 19:37:21 +02:00
case "$set_conf" in
2022-08-23 16:48:33 +02:00
Y|y) true ;;
2022-08-09 17:17:23 +02:00
*) echo "# Maybe later...";exit ;;
2022-07-20 19:37:21 +02:00
esac
# -------------------
# Write configuration
# -------------------
mkdir -p "$stl_dir_db/$domain_name"
mkdir -p "$stl_dir_db/$domain_name/articles"
cat <<EODBCONF > "$stl_dir_db/$domain_name/$domain_name.conf"
conf_pwd="$PWD"
EODBCONF
cat <<EODOMAINCONF > "$stl_file_pwd_conf"
# Domain Configuration
domain_name="$domain_name"
domain_url="$domain_protocol$domain_name"
domain_srv="$domain_srv"
domain_title="$domain_title"
domain_about="$domain_about"
domain_lang="$domain_lang"
domain_logo="$domain_logo"
domain_mail="$domain_mail"
domain_tags="$domain_tags"
domain_cr="$domain_cr"
domain_sidebar_title="$domain_sidebar_title"
domain_sidebar_items=$domain_sidebar_items
domain_css="$domain_css"
domain_ext_url="$domain_ext_url"
# You must know what you do if you change from here...
# STL configuration
2022-07-20 19:37:21 +02:00
stl_install="$stl_install"
domain_stats="$domain_stats"
2022-07-20 19:37:21 +02:00
domain_db_articles="$stl_dir_db/$domain_name/articles"
domain_conf="$PWD/$stl_file_pwd_conf"
# Domain directories for articles
domain_dir="$PWD"
domain_dir_articles="$PWD/articles"
domain_dir_images="$PWD/articles/images"
domain_dir_files="$PWD/articles/files"
# Domain directories for server
domain_dir_wip="$domain_srv/wip"
domain_dir_wip_tpl="$domain_srv/wip/template"
domain_dir_wip_images="$domain_srv/wip/images"
domain_dir_wip_files="$domain_srv/wip/files"
2022-08-23 16:48:33 +02:00
# Sidebar
domain_dir_sidebar="$PWD/articles/sidebar"
domain_file_sidebar_load="$PWD/articles/sidebar/stl.sidebar"
2022-07-20 19:37:21 +02:00
EODOMAINCONF
# Create a backup of the config in db
rsync -a "$PWD/$stl_file_pwd_conf" \
"$stl_dir_db/$domain_name/$stl_file_pwd_conf.bkp"
# Create folders base in PWD
mkdir -p "$PWD/articles"
mkdir -p "$PWD/articles/images"
mkdir -p "$PWD/articles/files"
2022-08-23 16:48:33 +02:00
mkdir -p "$PWD/articles/sidebar"
# Create folders for WIP
2022-07-21 16:03:40 +02:00
mkdir -p "$domain_srv/wip"
mkdir -p "$domain_srv/wip/template"
mkdir -p "$domain_srv/wip/files"
mkdir -p "$domain_srv/wip/images"
2022-07-20 19:37:21 +02:00
# Create conf, according to installation (server, local)
case "$stl_install" in
server)
printf '%s\n%s\n%s\n%s\n' \
2022-07-21 15:55:55 +02:00
"domain_dir_www=\"$domain_srv/www\"" \
2022-07-20 19:37:21 +02:00
"domain_dir_www_tpl=\"$domain_srv/www/template\"" \
"domain_dir_www_images=\"$domain_srv/www/images\"" \
"domain_dir_www_files=\"$domain_srv/www/files\"" \
>> "$stl_file_pwd_conf"
2022-07-21 16:03:40 +02:00
2022-07-20 19:37:21 +02:00
mkdir -p "$domain_srv/www"
mkdir -p "$domain_srv/www/template"
mkdir -p "$domain_srv/www/files"
mkdir -p "$domain_srv/www/images"
;;
esac
2022-07-21 16:03:40 +02:00
2022-08-23 16:48:33 +02:00
create__file_sidebar_load
2022-07-21 16:03:40 +02:00
printf '%s%s\n\n' \
"! files template (logo, style.css...) must be put in " \
"$domain_srv/wip/template/"
2022-07-20 19:37:21 +02:00
}
2022-08-23 16:48:33 +02:00
#======================================================================
# Creating static stl.sidebar (empty but commented)
# According to lang selected (en, fr)
#======================================================================
create__file_sidebar_load() {
touch "$domain_file_sidebar_load"
case "$domain_lang" in
fr|FR|Fr) file__sidebar_load_fr ;;
*) file__sidebar_load_en ;;
esac
}
#======================================================================
# stl.sidebar in fr
#======================================================================
file__sidebar_load_fr() {
cat <<EOsdbl > "$domain_file_sidebar_load"
# Name: Statique Littérateur
# Type: fichier brut
# Description: Fichier statique appelé par : stl sidebar load
# file: stl.sidebar
# Folder: articles/sidebar/
# (depuis le dossier de configuration du domaine)
# Les interlignes vides sont acceptés
# 1 URI de l'article.stl par ligne, rien d'autre
# l'URI NE DOIT PAS commencer par /
# exemples :
# index.stl
# dir1/index.stl
# Position de l'article: dans l'ordre de haut en bas
# ---------------------------------------------------------------------
EOsdbl
}
#======================================================================
# stl.sidebar in en
#======================================================================
file__sidebar_load_en() {
cat <<EOsdbl > "$domain_file_sidebar_load"
# Name: Statique Littérateur
# Type: brut file
# Description: Static file called by : stl sidebar load
# file: stl.sidebar
# Folder: articles/sidebar/
# (from domain configured folder)
# Empty lines are accepted
# ONLY 1 article's URI per line, nothing else
# URI must NOT begin with /
# examples :
# index.stl
# dir1/index.stl
# Article's Position: from top to bottom order
# ---------------------------------------------------------------------
EOsdbl
}