|
#!/bin/bash |
|
set -e |
|
|
|
|
|
######### |
|
# INFOS # |
|
######### |
|
|
|
# * Root path must exist before running this script |
|
# See the PATH_ROOT var in the setting below |
|
# |
|
# * I use NPM but you can replace it by Yarn |
|
# |
|
# * Some themes can't be simply installed directly with Git, so use Curl |
|
# |
|
# * Arguments: |
|
# -h, --help |
|
# -i, --install |
|
# -u, --update |
|
# -b, --backup |
|
|
|
|
|
########### |
|
# SETTING # |
|
########### |
|
|
|
# Colors |
|
INFO="\033[0;36m" # Cyan |
|
HIGHLIGHT="\033[0;35m" # Magenta |
|
OK="\033[0;32m" # Green |
|
ERROR="\033[0;31m" # Red |
|
NC="\033[0m" # No color |
|
|
|
# Folders name |
|
FOLDER_CORE="shaarli-core" |
|
FOLDER_PLUGINS="shaarli-plugins" |
|
FOLDER_THEMES="shaarli-themes" |
|
FOLDER_BACKUP="shaarli-backup" |
|
FOLDER_DATA="data" |
|
|
|
# Paths |
|
PATH_ROOT="/var/www" |
|
PATH_CORE="${PATH_ROOT}/${FOLDER_CORE}" |
|
PATH_PLUGINS="${PATH_ROOT}/${FOLDER_PLUGINS}" |
|
PATH_THEMES="${PATH_ROOT}/${FOLDER_THEMES}" |
|
PATH_BACKUP="${PATH_ROOT}/${FOLDER_BACKUP}" |
|
PATH_DATA="${PATH_CORE}/${FOLDER_DATA}" |
|
|
|
# Date Format |
|
DATE_FORMAT="%d-%m-%Y_%H-%M-%S" |
|
|
|
|
|
########### |
|
# HELPERS # |
|
########### |
|
|
|
# Check the existence of a folder |
|
is_directory() { |
|
if [ -d "$1" ]; then |
|
return 0 # Is a directory |
|
else |
|
return 1 # Is not a directory |
|
fi |
|
} |
|
|
|
# Check if a folder is emppty or not |
|
is_empty() { |
|
if [ -z "$(ls -A ${1})" ]; then |
|
return 0 # Is an empty directory |
|
else |
|
return 1 # Is not an empty directory |
|
fi |
|
} |
|
|
|
|
|
############# |
|
# FUNCTIONS # |
|
############# |
|
|
|
# Check folder before installing anything |
|
check_install() { |
|
declare -i nb_install_error=0 |
|
|
|
# Check the root |
|
if ! (is_directory $PATH_ROOT) || ! (is_empty $PATH_ROOT); then |
|
nb_install_error=$nb_install_error+1 |
|
printf "${ERROR}Error:${NC} The directory ${HIGHLIGHT}${PATH_ROOT}${NC} does not exist or is not empty.\n" |
|
else |
|
# Check Shaarli core |
|
if (is_directory $PATH_CORE) && ! (is_empty $PATH_CORE); then |
|
nb_install_error=$nb_install_error+1 |
|
printf "${ERROR}Error:${NC} The directory ${HIGHLIGHT}${PATH_CORE}${NC} already exist and is not empty.\n" |
|
fi |
|
|
|
# Check Shaarli plugins |
|
if (is_directory $PATH_PLUGINS) && ! (is_empty $PATH_PLUGINS); then |
|
nb_install_error=$nb_install_error+1 |
|
printf "${ERROR}Error:${NC} The directory ${HIGHLIGHT}${PATH_PLUGINS}${NC} already exist and is not empty.\n" |
|
fi |
|
|
|
# Check Shaarli themes |
|
if (is_directory $PATH_THEMES) && ! (is_empty $PATH_THEMES); then |
|
nb_install_error=$nb_install_error+1 |
|
printf "${ERROR}Error:${NC} The directory ${HIGHLIGHT}${PATH_THEMES}${NC} already exist and is not empty.\n" |
|
fi |
|
fi |
|
|
|
# Exit now if there missed or empty folders |
|
if [ "$nb_install_error" -gt 0 ]; then |
|
printf "${ERROR}Error:${NC} Installation canceled.\n" |
|
exit 1; |
|
fi |
|
} |
|
|
|
# Check folder before updating anything |
|
check_update() { |
|
declare -i nb_update_error=0 |
|
|
|
# Check the root |
|
if ! (is_directory $PATH_ROOT) || (is_empty $PATH_ROOT); then |
|
nb_update_error=$nb_update_error+1 |
|
printf "${ERROR}Error:${NC} The directory ${HIGHLIGHT}${PATH_ROOT}${NC} does not exist or is empty\n" |
|
else |
|
# Check Shaarli core |
|
if ! (is_directory $PATH_CORE) || (is_empty $PATH_CORE); then |
|
nb_update_error=$nb_update_error+1 |
|
printf "${ERROR}Error:${NC} The directory ${HIGHLIGHT}${PATH_CORE}${NC} does not exist or is empty\n" |
|
fi |
|
|
|
# Check Shaarli plugins |
|
if ! (is_directory $PATH_PLUGINS) || (is_empty $PATH_PLUGINS); then |
|
nb_update_error=$nb_update_error+1 |
|
printf "${ERROR}Error:${NC} The directory ${HIGHLIGHT}${PATH_PLUGINS}${NC} does not exist or is empty\n" |
|
fi |
|
|
|
# Check Shaarli themes |
|
if ! (is_directory $PATH_THEMES) || (is_empty $PATH_THEMES); then |
|
nb_update_error=$nb_update_error+1 |
|
printf "${ERROR}Error:${NC} The directory ${HIGHLIGHT}${PATH_THEMES}${NC} does not exist or is empty\n" |
|
fi |
|
fi |
|
|
|
# Exit now if there missed or empty folders |
|
if [ "$nb_update_error" -gt 0 ]; then |
|
printf "${ERROR}Error:${NC} Update canceled.\n" |
|
exit 1; |
|
fi |
|
} |
|
|
|
# Shaarli core installation |
|
core_install() { |
|
cd ${PATH_ROOT} |
|
|
|
if mkdir -p ${FOLDER_CORE}; then |
|
cd ${PATH_CORE} |
|
|
|
printf "${INFO}Info:${NC} Downloading and installing Shaarli core…\n" |
|
git clone -b latest https://github.com/shaarli/Shaarli.git . |
|
composer install --no-dev --prefer-dist --quiet && |
|
npm install --silent && npm run build --silent && |
|
npm audit fix |
|
printf "${OK}Done:${NC} Shaarli core installed!\n" |
|
else |
|
printf "${ERROR}Error:${NC} Can't create the folder ${HIGHLIGHT}${FOLDER_CORE}${NC}.\n" |
|
exit 1; |
|
fi |
|
} |
|
|
|
# Shaarli core update |
|
core_update() { |
|
cd ${PATH_CORE} |
|
|
|
printf "${INFO}Info:${NC} Downloading and installing Shaarli core update…\n" |
|
git pull origin latest:latest |
|
composer install --no-dev --prefer-dist --quiet && |
|
npm install --silent && npm run build --silent && |
|
npm audit fix |
|
printf "${OK}Done:${NC} Shaarli core updated!\n" |
|
} |
|
|
|
# Plugins installation |
|
plugins_install() { |
|
cd ${PATH_ROOT} |
|
|
|
if mkdir -p ${FOLDER_PLUGINS}; then |
|
cd ${PATH_PLUGINS} |
|
|
|
# code-coloration |
|
printf "${INFO}Info:${NC} Downloading code-coloration plugin…\n" |
|
git clone https://github.com/ArthurHoaro/code-coloration.git |
|
printf "${OK}Done:${NC} code-coloration plugin installed!\n" |
|
else |
|
printf "${ERROR}Error:${NC} Can't create the folder ${HIGHLIGHT}${FOLDER_PLUGINS}${NC}.\n" |
|
exit 1; |
|
fi |
|
} |
|
|
|
# Plugins update |
|
plugins_update() { |
|
cd ${PATH_PLUGINS} |
|
|
|
printf "${INFO}Info:${NC} Updating all plugins…\n" |
|
|
|
# This command will update all git repository in the subfolders |
|
# See https://stackoverflow.com/questions/3497123/run-git-pull-over-all-subdirectories#comment46218583_12495234 |
|
find . -mindepth 1 -maxdepth 1 -type d -print -exec git -C {} pull \; |
|
printf "${OK}Done:${NC} plugins updated!\n" |
|
} |
|
|
|
# Themes installation |
|
themes_install() { |
|
cd ${PATH_ROOT} |
|
|
|
if mkdir -p ${FOLDER_THEMES}; then |
|
cd ${PATH_THEMES} |
|
|
|
# Material |
|
printf "${INFO}Info:${NC} Downloading and installing Material theme…\n" |
|
curl -L -o material.zip https://github.com/kalvn/Shaarli-Material/releases/download/v0.10.4/shaarli-material.v0.10.4.zip && |
|
unzip -qq material.zip && |
|
rm material.zip |
|
printf "${OK}Done:${NC} Material theme installed!\n" |
|
else |
|
printf "${ERROR}Error:${NC} Can't create the folder ${HIGHLIGHT}${FOLDER_THEMES}${NC}.\n" |
|
exit 1 |
|
fi |
|
} |
|
|
|
# Themes update |
|
themes_update() { |
|
cd ${PATH_THEMES} |
|
|
|
# Material |
|
MATERIAL_FOLDER="material" |
|
if (is_directory $MATERIAL_FOLDER) && ! (is_empty $MATERIAL_FOLDER); then |
|
printf "${INFO}Info:${NC} Downloading and installing Material theme…\n" |
|
rm -rf "${MATERIAL_FOLDER}" "${MATERIAL_FOLDER}/.[a-zA-Z]*" |
|
material_url=$(curl -s https://api.github.com/repos/kalvn/Shaarli-Material/releases | grep browser_download_url | grep '[.]zip' | head -n 1 | cut -d '"' -f 4) |
|
curl -L -o material.zip $material_url |
|
unzip -qq material.zip && |
|
rm material.zip |
|
printf "${OK}Done:${NC} Material theme updated!\n" |
|
else |
|
exit 1 |
|
fi |
|
} |
|
|
|
# Create backup |
|
create_backup() { |
|
# Check the root |
|
if ! (is_directory $PATH_ROOT) || (is_empty $PATH_ROOT); then |
|
printf "${ERROR}Error:${NC} The directory ${HIGHLIGHT}${PATH_ROOT}${NC} does not exist or is empty\n" |
|
exit 1; |
|
fi |
|
|
|
# Check backup folder |
|
if ! (is_directory $PATH_BACKUP) && ! (mkdir -p ${FOLDER_BACKUP}); then |
|
printf "${ERROR}Error:${NC} The directory ${HIGHLIGHT}${PATH_BACKUP}${NC} does not exist and can't be created.\n" |
|
exit 1; |
|
fi |
|
|
|
cd ${PATH_BACKUP} |
|
|
|
# Create the backup |
|
FOLDER_BACKUP_DATETIME="$(date +"${DATE_FORMAT}")" |
|
if mkdir -p ${FOLDER_BACKUP_DATETIME}; then |
|
cd ${FOLDER_BACKUP_DATETIME} |
|
|
|
# Data backup |
|
if zip -r -qq "data.zip" ${PATH_DATA}; then |
|
printf "${OK}Done:${NC} Data backup successfully completed!\n" |
|
else |
|
printf "${ERROR}Error:${NC} Can't create the data backup.\n" |
|
exit 1; |
|
fi |
|
else |
|
printf "${ERROR}Error:${NC} Can't create the folder ${HIGHLIGHT}${FOLDER_BACKUP_DATETIME}${NC}.\n" |
|
exit 1; |
|
fi |
|
} |
|
|
|
# Create symbolic links |
|
symbolic_links() { |
|
printf "${INFO}Info:${NC} Create symbolic links for themes and plugins…\n" |
|
ln -s "${PATH_THEMES}/material" "${PATH_CORE}/tpl/material" |
|
ln -s "${PATH_PLUGINS}/code-coloration/code_coloration" "${PATH_CORE}/plugins/code_coloration" |
|
printf "${OK}Done:${NC} Symbolic links created!\n" |
|
} |
|
|
|
|
|
############### |
|
# SCRIPT CORE # |
|
############### |
|
|
|
# Exit if there no arguments passed |
|
if [ $# -eq 0 ] ; then |
|
printf "${ERROR}Error:${NC} You need to pass an argument. Use ${HIGHLIGHT}--help${NC} to get tips.\n" |
|
exit 1 |
|
fi |
|
|
|
# Check for the argument passed to run the proper function |
|
# Only one argument can be set at a time |
|
while test $# -eq 1; do |
|
case $1 in |
|
-h|--help) |
|
# Help |
|
printf "Shaarli Manager ${HIGHLIGHT}help${NC}\n" |
|
printf "Usage: shaarli-manager [<args>]\n" |
|
printf "\n" |
|
printf "Arguments list:\n" |
|
printf -- "-h, --help Show help section.\n" |
|
printf -- "-i, --install Install core, plugins and themes.\n" |
|
printf -- "-u, --update Update core, plugins and themes.\n" |
|
printf -- "-b, --backup Create a backup of the data folder.\n" |
|
printf "\n" |
|
printf "See ${HIGHLIGHT}https://gist.github.com/yoanmalie/48462cd7cf08d9d3fa2e369db9daa2f1/${NC} for more informations.\n" |
|
shift |
|
exit 0 |
|
;; |
|
-i|--install) |
|
# Install |
|
printf "${INFO}Info:${NC} Running install…\n" |
|
|
|
check_install && |
|
core_install && |
|
plugins_install && |
|
themes_install && |
|
symbolic_links |
|
|
|
shift |
|
exit 0 |
|
;; |
|
-u|--update) |
|
# Update |
|
printf "${INFO}Info:${NC} Running update…\n" |
|
|
|
check_update && |
|
core_update && |
|
plugins_update && |
|
themes_update && |
|
symbolic_links && |
|
|
|
shift |
|
exit 0 |
|
;; |
|
-b|--backup) |
|
# Backup |
|
printf "${INFO}Info:${NC} Running backup…\n" |
|
|
|
create_backup |
|
|
|
shift |
|
exit 0 |
|
;; |
|
*) |
|
printf "${ERROR}Error:${NC} Invalid argument. Use ${HIGHLIGHT}--help${NC} to get tips.\n" |
|
shift |
|
exit 1 |
|
;; |
|
esac |
|
done |