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

510 lines
13 KiB
Bash

#!/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
#------------
# funny stats
#------------
# lines: 509
# functions: 7
#-----------------------------------------------------------------------
#**********************************************************************
#======================================================================
# --------------
# DOMAIN Section
# --------------
#======================================================================
#----------------------------------------------------------------------
# domain OPTIONS | Manage arguments from command line
# $1: main argument
# $2: change
#----------------------------------------------------------------------
domain__OPTIONS() {
case "$2" in
'') domain_show=true ;;
new) domain_show=true;domain_add=true;
[[ "$3" ]] && domain_name="$3" ;;
edit) edit__OPTIONS "" conf ;;
*) noarg "$2" "Use: new, edit" ;;
esac
domain__get
}
#----------------------------------------------------------------------
# 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
}
#----------------------------------------------------------------------
# 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
domain__pass show
else
echo "# No domain set for this directory: $PWD"
domain__pass missing
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}
# 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
Y|y) domain_protocol="https://" ;;
N|n) domain_protocol="http://" ;;
*) echo "# Maybe later...";exit ;;
esac
domain_ask+="$domain_name"
fi
}
# ----------
# Processing question to config domain
# ----------
printf '\n%s\n%s\n\n' \
"# You will be asked to confirm after resume" \
"# Empty anwser or mismatch value cancel process !"
# DOMAIN is set in command line ?
# ----------
case "$domain_name" in
'')
read -rp "- Which domain to set (i.e. www.example.com) ? " domain_name
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
Y|y) true ;;
*) echo "# Maybe later...";exit ;;
esac
;;
esac
# Where is the server ? /var/www ?
# ----------
read -erp "- Where is your server URI ? " srv_uri
is__set "srv_uri"
[[ "${srv_uri: -1}" == "/" ]] \
&& srv_uri=${srv_uri::-1}
! [[ -d "$srv_uri" ]] \
&& echo "! Missing directory: $srv_uri" \
&& exit
if__writeable "$srv_uri"
domain_srv="$srv_uri/$domain_name"
# Installation: server or local
# ----------
read -rp "- STL server (Y|N for local = no WWW, WIP only) ? " install
case "$install" in
Y|y) stl_install="server" ;;
N|n) stl_install="local" ;;
*) echo "# Maybe later..." ;;
esac
# Domain TITLE
# ----------
read -rp "- Domain Title ? " domain_title
is__set "$domain_title"
[[ "$domain_title" =~ \" ]] \
&& domain_title=`sed -e 's^"^\\\\"^g' <<<"$domain_title"`
# Domain ABOUT
# ----------
read -rp "- Domain About ? " domain_about
is__set "$domain_about"
[[ "$domain_about" =~ \" ]] \
&& domain_about=`sed -e 's^"^\\\\"^g' <<<"$domain_about"`
# Domain LANG
# ----------
read -rp "- Domain lang (i.e. fr ; 2 characters) ? " domain_lang
is__set "$domain_lang"
! (( ${#domain_lang} == 2 )) \
&& echo "! Lang must be 2 characters (i.e. fr, gb, en...)" \
&& exit
# Domain MAIL
# ----------
read -rp "- Domain mail ? " domain_mail
is__set "$domain_mail"
! [[ "$domain_mail" =~ @ ]] \
&& echo "! Mail must contain @..." \
&& exit
# Domain TAGS
# ----------
read -rp "- Domain MAIN tags (comma separated) ? " domain_tags
is__set "$domain_tags"
# Domain Copyright
# ----------
read -rp "- Domain copyright (i.e. CC BY-NC-SA) ? " domain_cr
is__set "$domain_cr"
[[ "$domain_cr" =~ \" ]] \
&& domain_cr=`sed -e 's^"^\\\\"^g' <<<"$domain_cr"`
# Domain SIDEBAR TITLE
# ----------
read -rp "- Domain sidebar title (i.e. last news) ? " domain_sidebar_title
is__set "$domain_sidebar_title"
# Replace " with \" if exists
[[ "$domain_sidebar_title" =~ \" ]] \
&& domain_sidebar_title=`sed -e 's^"^\\\\"^g' <<<"$domain_sidebar_title"`
# Domain SIDEBAR title NUMBERS
# ----------
read -rp "- Domain sidebar items max number (1-24) ? " domain_sidebar_items
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
# Domain CSS PREFIX
# ----------
read -rp "- Domain CSS prefix (alpha numeric and -) ? " domain_css
is__set "$domain_css"
! [[ $domain_css =~ ^[a-zA-Z0-9_-]+$ ]] \
&& echo "! '$domain_css' is not alpha numeric. '-' is authorized" \
&& exit
domain_css=${domain_css,,}
case "${domain_css: -1}" in
"_"|"-") domain_css=${domain_css::-1} ;;
esac
# Domain LOGO
# ----------
read -rp "- Domain logo filename (i.e. mylogo.png) ? " domain_logo
# External URL Profile
# ----------
read -rp "- Domain external URL Profile (empty if unknown) ? " domain_ext_url
# Statoolinfos
# ----------
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
# ----------
# Resume before writing conf
# ----------
clear
echo -e "# Resume configuration...\n"
cat <<EODOMAINCONF
STL Inslallation : $stl_install
STL stats generator : $domain_stats
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...)
Domain : $domain_name
URL : $domain_protocol$domain_name
Server URI : $domain_srv
Server WIP : $domain_srv/wip/
$(
if [[ $stl_install == "server" ]];then
echo "Server WWW : $domain_srv/www/"
fi
)
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
Sidebar load file : $PWD/articles/sidebar/stl.sidebar
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
)
External URL Profile : $domain_ext_url
EODOMAINCONF
echo -e "! Use $domain_name only in $PWD"
read -rp "- Write to $PWD/.stl.conf (Y|*) ? " set_conf
case "$set_conf" in
Y|y) true ;;
*) echo "# Maybe later...";exit ;;
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
stl_install="$stl_install"
domain_stats="$domain_stats"
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"
# Sidebar
domain_dir_sidebar="$PWD/articles/sidebar"
domain_file_sidebar_load="$PWD/articles/sidebar/stl.sidebar"
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"
mkdir -p "$PWD/articles/sidebar"
# Create folders for WIP
mkdir -p "$domain_srv/wip"
mkdir -p "$domain_srv/wip/template"
mkdir -p "$domain_srv/wip/files"
mkdir -p "$domain_srv/wip/images"
# Create conf, according to installation (server, local)
case "$stl_install" in
server)
printf '%s\n%s\n%s\n%s\n' \
"domain_dir_www=\"$domain_srv/www\"" \
"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"
mkdir -p "$domain_srv/www"
mkdir -p "$domain_srv/www/template"
mkdir -p "$domain_srv/www/files"
mkdir -p "$domain_srv/www/images"
;;
esac
create__file_sidebar_load
printf '%s%s\n\n' \
"! files template (logo, style.css...) must be put in " \
"$domain_srv/wip/template/"
}
#======================================================================
# 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
}