ScoDoc-PE/tools/create_dept.sh

99 lines
2.1 KiB
Bash
Raw Normal View History

2020-09-26 16:19:37 +02:00
#!/bin/bash
#
# ScoDoc: creation initiale d'un departement
#
# Ce script prend en charge la creation de la base de donnees
# et doit être lancé par l'utilisateur unix root dans le repertoire .../tools
2020-09-26 16:19:37 +02:00
# ^^^^^^^^^^^^^^^^^^^^^
# E. Viennet, Juin 2008
#
2021-04-22 22:14:25 +02:00
set -eo pipefail
2020-09-26 16:19:37 +02:00
source config.sh
source utils.sh
2020-12-26 00:11:55 +01:00
check_uid_root "$0"
2020-09-26 16:19:37 +02:00
2020-12-26 00:11:55 +01:00
usage() {
echo "$0 [-n DEPT]"
echo "(default to interactive mode)"
exit 1
}
[ $# = 0 ] || [ $# = 2 ] || usage
2020-09-26 16:19:37 +02:00
2020-12-26 00:11:55 +01:00
if [ "$1" = "-n" ]
then
interactive=0
if [ $# -lt 2 ]
then
usage
fi
DEPT=$2
else
interactive=1
echo -n "Nom du departement (un mot sans ponctuation, exemple \"Info\"): "
read -r DEPT
fi
2020-09-26 16:19:37 +02:00
if [[ ! "$DEPT" =~ ^[A-Za-z0-9]+$ ]]
then
echo 'Nom de departement invalide !'
2020-12-26 00:11:55 +01:00
exit 2
2020-09-26 16:19:37 +02:00
fi
export DEPT
2020-12-26 00:11:55 +01:00
db_name=SCO$(to_upper "$DEPT")
export db_name
2020-09-26 16:19:37 +02:00
cfg_pathname="${SCODOC_VAR_DIR}/config/depts/$DEPT".cfg
2020-12-26 00:11:55 +01:00
if [ -e "$cfg_pathname" ]
2020-09-26 16:19:37 +02:00
then
2020-12-26 00:11:55 +01:00
echo 'Erreur: Il existe deja une configuration pour "'"$DEPT"'"'
2020-09-26 16:19:37 +02:00
exit 1
fi
# --- Ensure postgres user scodoc exists
2020-09-26 16:19:37 +02:00
init_postgres_user
# ----------------------- Create database
2020-12-26 00:11:55 +01:00
su -c ./create_database.sh "$POSTGRES_SUPERUSER"
2020-09-26 16:19:37 +02:00
# ----------------------- Create tables
# POSTGRES_USER == regular unix user (scodoc)
2020-12-27 13:45:24 +01:00
if [ "$interactive" = 1 ]
then
su -c ./initialize_database.sh "$POSTGRES_USER"
else
su -c ./initialize_database.sh "$POSTGRES_USER" > /dev/null 2>&1
fi
2020-09-26 16:19:37 +02:00
# ----------------------- Enregistre fichier config
2020-12-26 00:11:55 +01:00
echo "dbname=${db_name}" > "$cfg_pathname"
2020-09-26 16:19:37 +02:00
2020-12-26 00:11:55 +01:00
if [ "$interactive" = 1 ]
2020-09-26 16:19:37 +02:00
then
2020-12-26 00:11:55 +01:00
# ----------------------- Force mise à jour
echo -n "Voulez vous mettre a jour ScoDoc (tres recommande) ? (y/n) [y] "
read -r ans
if [ "$(norm_ans "$ans")" != 'N' ]
then
(cd "$SCODOC_DIR/tools" || terminate "no config directory"; ./upgrade.sh)
2020-12-26 00:11:55 +01:00
fi
# -----------------------
echo
echo " Departement $DEPT cree"
echo
echo " Attention: la base de donnees n'a pas de copies de sauvegarde"
echo
echo " Maintenant, vous pouvez ajouter le departement via l'application web"
echo " en suivant le lien \"Administration de ScoDoc\" sur la page d'accueil."
echo
2020-09-26 16:19:37 +02:00
fi