#!/bin/bash set -e remove=0 if [[ "$1" == "--remove" ]]; then remove=1 fi source="$(pwd)" name="$(basename "$source")" target="$HOME/projects/$name/doc" if [[ "$source" == "$HOME" ]]; then echo "Fehler: \$HOME selbst kann nicht gemountet werden." >&2 exit 1 fi if [[ -z "$name" || "$name" == "/" ]]; then echo "Fehler: ungültiger Ordnername." >&2 exit 1 fi if [[ "$source" == "$target" ]]; then echo "Fehler: Source und Target sind identisch." >&2 exit 1 fi case "$target/" in "$source"/*) echo "Fehler: Target liegt unter Source (Rekursionsgefahr)." >&2; exit 1;; esac case "$source/" in "$target"/*) echo "Fehler: Source liegt unter Target (Rekursionsgefahr)." >&2; exit 1;; esac fstab_line="$source $target none bind,nofail 0 0" fstab_pattern="\\# $target #d" if (( remove )); then if mountpoint -q "$target"; then sudo umount "$target" cp -a "$source/." "$target/" fi sudo sed -i "$fstab_pattern" /etc/fstab sudo systemctl daemon-reload echo "Entfernt: $target" exit 0 fi mkdir -p "$target" if ! mountpoint -q "$target" && [[ -n "$(ls -A "$target" 2>/dev/null)" ]]; then echo "Fehler: $target existiert und ist nicht leer (kein Mount-Point)." >&2 echo " → Inhalt vorher nach $source verschieben oder $target leeren." >&2 exit 1 fi if mountpoint -q "$target"; then sudo umount "$target" fi sudo sed -i "$fstab_pattern" /etc/fstab sudo mount --bind "$source" "$target" echo "$fstab_line" | sudo tee -a /etc/fstab > /dev/null sudo systemctl daemon-reload echo "Gespiegelt: $source ↔ $target"