#!/bin/bash # # Helper script to clean up, restore symlinks and set permissions # in a just downloaded and uncompressed (zipball) instance. # # Author: Kovács Zoltán # Kovács Zoltán # License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html) # 2023-09-11 v1.0 # new: forked from the "Smartfront's DOCKER_skeleton" repository. # 2022-11-04 v0.2 # fix: Content regexp now matches well to the special chars allowed. # 2022-09-22 v0.1 Initial release. # Where I'm? # https://gist.github.com/TheMengzor/968e5ea87e99d9c41782 [[ -z "$(which dirname)" ]] && exit 1 [[ -z "$(which readlink)" ]] && exit 1 # SOURCE="$0" while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink SCRPATH="$( cd -P "$(dirname "$SOURCE" )" && echo "$PWD" )" #" SOURCE="$(readlink "$SOURCE")" # if $SOURCE was a relative symlink, we need to resolve it # relative to the path where the symlink file was located [[ $SOURCE != /* ]] && SOURCE="$SCRPATH/$SOURCE" done; SCRPATH="$( cd -P "$(dirname "$SOURCE" )" && echo "$PWD" )" #" # We've the absolute path of the script. # Removing some unnecessary files. # if [ -n "$(which find)" ]; then find "$SCRPATH" -name .gitignore -delete fi if [ -d "$SCRPATH/.git" ]; then rm -Rf "$SCRPATH/.git" 2>/dev/null fi # Restoring the symlinks. # All files considered as a symlink which is containing a single line # with a valid pathname inside. # if [ -n "$(which awk)" -a -n "$(which cut)" -a -n "$(which find)" -a -n "$(which ln)" ]; then # Files containing maximum 1 line - the .metadata store is a special exception. IFS=$'\n' read -r -d '' -a SUSPECTS < <( (find "$SCRPATH" ! -name '.metadata' -type f -exec awk 'END { if (NR < 2) print FILENAME }' {} \;) && printf '\0' ) # Enumerates these files. for suspect in "${SUSPECTS[@]}" do if [ -n "$suspect" -a -s "$suspect" ]; then # Checks the content: it seems to be a valid pathname? # For this tricky read see https://stackoverflow.com/questions/46163678/ IFS= read -r -n 1024 -d '' content <"$suspect" || [[ $content ]] if [[ "$content" =~ ^([[:alnum:]]|[-_/. ])+$ ]]; then # Replaces the suspect with a symlink pointing to it's content. rm "$suspect" 2>/dev/null ; ln -s "$content" "$suspect" 2>/dev/null fi fi done fi # We'll use the metastore DB (if any) to restore # the dates and saved permissions individually. # if [ -n "$(which metastore)" -a -x "$(which metastore)" ]; then ( cd "$SCRPATH" if [ -e ".metadata" ]; then "$(which metastore)" -amqq 2>/dev/null fi ) fi # # Finally we correct the permissions according to umask 007, # allow access to directories for backup purposes # and we grant R/W permission to the user running the script. chmod -R g+rw,o-rw "$SCRPATH" find "$SCRPATH" -type d -exec chmod 2771 {} \; SETFACL="$(which setfacl)" if [ -n "$SETFACL" ]; then "$SETFACL" -R -d -m u:$USER:rwX,g::rwX "$SCRPATH" 2>/dev/null find "$SCRPATH" -type d -exec "$SETFACL" -m u:$USER:rwx {} 2>/dev/null \; find "$SCRPATH" -type f -exec "$SETFACL" -m u:$USER:rw {} 2>/dev/null \; fi # That's all, Folks! :)