Skip to content

Commit

Permalink
Compact script code
Browse files Browse the repository at this point in the history
  • Loading branch information
mreid-tt committed Nov 13, 2023
1 parent 5035481 commit ba9ad9c
Showing 1 changed file with 39 additions and 38 deletions.
77 changes: 39 additions & 38 deletions spk/selfoss/src/service-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,51 +47,50 @@ service_postinst ()
# Install web configurations
TEMPDIR="${SYNOPKG_PKGTMP}/web"
${MKDIR} ${TEMPDIR}
WS_CFG_PATH="/usr/syno/etc/packages/WebStation"
WS_CFG_DIR="/usr/syno/etc/packages/WebStation"
WS_CFG_FILE="WebStation.json"
WS_CFG_PATH="${WS_CFG_DIR}/${WS_CFG_FILE}"
TMP_WS_CFG_PATH="${TEMPDIR}/${WS_CFG_FILE}"
PHP_CFG_FILE="PHPSettings.json"
PHP_CFG_PATH="${WS_CFG_DIR}/${PHP_CFG_FILE}"
TMP_PHP_CFG_PATH="${TEMPDIR}/${PHP_CFG_FILE}"
PHP_PROF_NAME="Default PHP 7.4 Profile"
WS_BACKEND="$(${JQ} -r '.default.backend' ${WS_CFG_PATH}/${WS_CFG_FILE})"
WS_PHP="$(${JQ} -r '.default.php' ${WS_CFG_PATH}/${WS_CFG_FILE})"
CFG_UPDATE="no"
WS_BACKEND="$(${JQ} -r '.default.backend' ${WS_CFG_PATH})"
WS_PHP="$(${JQ} -r '.default.php' ${WS_CFG_PATH})"
RESTART_APACHE="no"
RSYNC_ARCH_ARGS="--backup --suffix=.bak --remove-source-files"
# Check if Apache is the selected back-end
if [ ! "$WS_BACKEND" = "2" ]; then
echo "Set Apache as the back-end server"
${JQ} '.default.backend = 2' ${WS_CFG_PATH}/${WS_CFG_FILE} > ${TEMPDIR}/${WS_CFG_FILE}
${MV} ${WS_CFG_PATH}/${WS_CFG_FILE} ${WS_CFG_PATH}/${WS_CFG_FILE}.bak
rsync -aX ${TEMPDIR}/${WS_CFG_FILE} ${WS_CFG_PATH}/ 2>&1
${RM} ${TEMPDIR}/${WS_CFG_FILE}
CFG_UPDATE="yes"
${JQ} '.default.backend = 2' ${WS_CFG_PATH} > ${TMP_WS_CFG_PATH}
rsync -aX ${RSYNC_ARCH_ARGS} ${TMP_WS_CFG_PATH} ${WS_CFG_DIR}/ 2>&1
RESTART_APACHE="yes"
fi
# Check if default PHP profile is selected
if [ -z "$WS_PHP" ] || [ "$WS_PHP" = "null" ]; then
echo "Enable default PHP profile"
# Locate default PHP profile
PHP_PROF_ID="$(${JQ} -r '. | to_entries[] | select(.value | type == "object" and .profile_desc == "'"$PHP_PROF_NAME"'") | .key' "${WS_CFG_PATH}/${PHP_CFG_FILE}")"
${JQ} ".default.php = \"$PHP_PROF_ID\"" "${WS_CFG_PATH}/${WS_CFG_FILE}" > ${TEMPDIR}/${WS_CFG_FILE}
${MV} ${WS_CFG_PATH}/${WS_CFG_FILE} ${WS_CFG_PATH}/${WS_CFG_FILE}.bak
rsync -aX ${TEMPDIR}/${WS_CFG_FILE} ${WS_CFG_PATH}/ 2>&1
${RM} ${TEMPDIR}/${WS_CFG_FILE}
CFG_UPDATE="yes"
PHP_PROF_ID="$(${JQ} -r '. | to_entries[] | select(.value | type == "object" and .profile_desc == "'"$PHP_PROF_NAME"'") | .key' "${PHP_CFG_PATH}")"
${JQ} ".default.php = \"$PHP_PROF_ID\"" "${WS_CFG_PATH}" > ${TMP_WS_CFG_PATH}
rsync -aX ${RSYNC_ARCH_ARGS} ${TMP_WS_CFG_PATH} ${WS_CFG_DIR}/ 2>&1
RESTART_APACHE="yes"
fi
# Check for Selfoss PHP profile
if ! ${JQ} -e '.["com-synocommunity-packages-selfoss"]' "${WS_CFG_PATH}/${PHP_CFG_FILE}" >/dev/null; then
if ! ${JQ} -e '.["com-synocommunity-packages-selfoss"]' "${PHP_CFG_PATH}" >/dev/null; then
echo "Add PHP profile for Selfoss"
${JQ} --slurpfile ocNode ${SYNOPKG_PKGDEST}/web/selfoss.json '.["com-synocommunity-packages-selfoss"] = $ocNode[0]' ${WS_CFG_PATH}/${PHP_CFG_FILE} > ${TEMPDIR}/${PHP_CFG_FILE}
${MV} ${WS_CFG_PATH}/${PHP_CFG_FILE} ${WS_CFG_PATH}/${PHP_CFG_FILE}.bak
rsync -aX ${TEMPDIR}/${PHP_CFG_FILE} ${WS_CFG_PATH}/ 2>&1
${RM} ${TEMPDIR}/${PHP_CFG_FILE}
CFG_UPDATE="yes"
${JQ} --slurpfile ocNode ${SYNOPKG_PKGDEST}/web/selfoss.json '.["com-synocommunity-packages-selfoss"] = $ocNode[0]' ${PHP_CFG_PATH} > ${TMP_PHP_CFG_PATH}
rsync -aX ${RSYNC_ARCH_ARGS} ${TMP_PHP_CFG_PATH} ${WS_CFG_DIR}/ 2>&1
RESTART_APACHE="yes"
fi
# Check for Selfoss Apache config
if [ ! -f "/usr/local/etc/apache24/sites-enabled/selfoss.conf" ]; then
echo "Add Apache config for Selfoss"
rsync -aX ${SYNOPKG_PKGDEST}/web/selfoss.conf /usr/local/etc/apache24/sites-enabled/ 2>&1
CFG_UPDATE="yes"
RESTART_APACHE="yes"
fi
# Restart Apache if configs have changed
if [ "$CFG_UPDATE" = "yes" ]; then
if ${JQ} -e 'to_entries | map(select((.key | startswith("com-synocommunity-packages-")) and .key != "com-synocommunity-packages-selfoss")) | length > 0' "${WS_CFG_PATH}/${PHP_CFG_FILE}" >/dev/null; then
if [ "$RESTART_APACHE" = "yes" ]; then
if ${JQ} -e 'to_entries | map(select((.key | startswith("com-synocommunity-packages-")) and .key != "com-synocommunity-packages-selfoss")) | length > 0' "${PHP_CFG_PATH}" >/dev/null; then
echo " [WARNING] Multiple PHP profiles detected, will require restart of DSM to load new configs"
else
echo "Restart Apache to load new configs"
Expand Down Expand Up @@ -121,28 +120,29 @@ service_postuninst ()
# Remove web configurations
TEMPDIR="${SYNOPKG_PKGTMP}/web"
${MKDIR} ${TEMPDIR}
WS_CFG_PATH="/usr/syno/etc/packages/WebStation"
WS_CFG_DIR="/usr/syno/etc/packages/WebStation"
PHP_CFG_FILE="PHPSettings.json"
CFG_UPDATE="no"
PHP_CFG_PATH="${WS_CFG_DIR}/${PHP_CFG_FILE}"
TMP_PHP_CFG_PATH="${TEMPDIR}/${PHP_CFG_FILE}"
RESTART_APACHE="no"
RSYNC_ARCH_ARGS="--backup --suffix=.bak --remove-source-files"
# Check for Selfoss PHP profile
if ${JQ} -e '.["com-synocommunity-packages-selfoss"]' "${WS_CFG_PATH}/${PHP_CFG_FILE}" >/dev/null; then
if ${JQ} -e '.["com-synocommunity-packages-selfoss"]' "${PHP_CFG_PATH}" >/dev/null; then
echo "Removing PHP profile for Selfoss"
${JQ} 'del(.["com-synocommunity-packages-selfoss"])' ${WS_CFG_PATH}/${PHP_CFG_FILE} > ${TEMPDIR}/${PHP_CFG_FILE}
${MV} ${WS_CFG_PATH}/${PHP_CFG_FILE} ${WS_CFG_PATH}/${PHP_CFG_FILE}.bak
rsync -aX ${TEMPDIR}/${PHP_CFG_FILE} ${WS_CFG_PATH}/ 2>&1
${RM} ${TEMPDIR}/${PHP_CFG_FILE}
${RM} "${WS_CFG_PATH}/php_profile/com-synocommunity-packages-selfoss"
CFG_UPDATE="yes"
${JQ} 'del(.["com-synocommunity-packages-selfoss"])' ${PHP_CFG_PATH} > ${TMP_PHP_CFG_PATH}
rsync -aX ${RSYNC_ARCH_ARGS} ${TMP_PHP_CFG_PATH} ${WS_CFG_DIR}/ 2>&1
${RM} "${WS_CFG_DIR}/php_profile/com-synocommunity-packages-selfoss"
RESTART_APACHE="yes"
fi
# Check for Selfoss Apache config
if [ -f "/usr/local/etc/apache24/sites-enabled/selfoss.conf" ]; then
echo "Removing Apache config for Selfoss"
${RM} /usr/local/etc/apache24/sites-enabled/selfoss.conf
CFG_UPDATE="yes"
RESTART_APACHE="yes"
fi
# Restart Apache if configs have changed
if [ "$CFG_UPDATE" = "yes" ]; then
if ${JQ} -e 'to_entries | map(select((.key | startswith("com-synocommunity-packages-")) and .key != "com-synocommunity-packages-selfoss")) | length > 0' "${WS_CFG_PATH}/${PHP_CFG_FILE}" >/dev/null; then
if [ "$RESTART_APACHE" = "yes" ]; then
if ${JQ} -e 'to_entries | map(select((.key | startswith("com-synocommunity-packages-")) and .key != "com-synocommunity-packages-selfoss")) | length > 0' "${PHP_CFG_PATH}" >/dev/null; then
echo " [WARNING] Multiple PHP profiles detected, will require restart of DSM to load new configs"
else
echo "Restart Apache to load new configs"
Expand All @@ -158,10 +158,11 @@ validate_preinst ()
{
# Check for modification to PHP template defaults on DSM 6
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
WS_TMPL_PATH="/var/packages/WebStation/target/misc"
WS_TMPL_DIR="/var/packages/WebStation/target/misc"
WS_TMPL_FILE="php74_fpm.mustache"
WS_TMPL_PATH="${WS_TMPL_DIR}/${WS_TMPL_FILE}"
# Check for PHP template defaults
if ! grep -q -E '^user = http$' "${WS_TMPL_PATH}/${WS_TMPL_FILE}" || ! grep -q -E '^listen\.owner = http$' "${WS_TMPL_PATH}/${WS_TMPL_FILE}"; then
if ! grep -q -E '^user = http$' "${WS_TMPL_PATH}" || ! grep -q -E '^listen\.owner = http$' "${WS_TMPL_PATH}"; then
echo "PHP template defaults have been modified. Installation is not supported."
exit 1
fi
Expand Down

0 comments on commit ba9ad9c

Please sign in to comment.