diff --git a/install_dsync.sh b/install_dsync.sh new file mode 100644 index 0000000..f220d21 --- /dev/null +++ b/install_dsync.sh @@ -0,0 +1,174 @@ +#!/usr/bin/env bash +# Install dsync from https://git.ai.infran.ru/ilyukhin/dsync.git +# +# Usage: +# ./install_dsync.sh # install to ~/.local/bin (default) +# ./install_dsync.sh --system # install to /usr/local/bin (requires sudo) +# ./install_dsync.sh -d DIR # install to custom DIR +# ./install_dsync.sh -f # force overwrite if target exists +# ./install_dsync.sh -h # show help +set -euo pipefail + +REPO_URL="https://git.ai.infran.ru/ilyukhin/dsync.git" +RAW_BASE="https://git.ai.infran.ru/ilyukhin/dsync/-/raw" # raw URL pattern: $RAW_BASE//dsync +DEFAULT_USER_DIR="$HOME/.local/bin" + +DEST_DIR="$DEFAULT_USER_DIR" +FORCE=0 + +print_help() { + cat <&2 + exit 2 + fi + DEST_DIR="$2" + shift 2 + ;; + -f|--force) + FORCE=1 + shift + ;; + -h|--help) + print_help + exit 0 + ;; + --) + shift + break + ;; + -*) + echo "Unknown option: $1" >&2 + print_help + exit 2 + ;; + *) + break + ;; + esac +done + +DEST_PATH="$DEST_DIR/dsync" + +# create temporary dir and ensure cleanup +TMPDIR="$(mktemp -d)" +trap 'rm -rf "$TMPDIR"' EXIT + +echo "Installing dsync to: $DEST_DIR" + +# ensure target directory exists (create with sudo if system dir) +if [[ ! -d "$DEST_DIR" ]]; then + echo "Target directory $DEST_DIR does not exist — attempting to create it..." + if ! mkdir -p "$DEST_DIR" 2>/dev/null; then + echo "No permission to create $DEST_DIR, trying with sudo..." + if ! command -v sudo >/dev/null 2>&1; then + echo "sudo not available — cannot create $DEST_DIR" >&2 + exit 1 + fi + sudo mkdir -p "$DEST_DIR" + fi +fi + +FOUND="" + +# 1) Try to clone repo with git (if git available) +if command -v git >/dev/null 2>&1; then + echo "git found — attempting shallow clone..." + if git clone --depth 1 "$REPO_URL" "$TMPDIR/repo" >/dev/null 2>&1; then + echo "Repository cloned." + else + echo "Shallow clone failed, attempting normal clone (may show output)..." + git clone --depth 1 "$REPO_URL" "$TMPDIR/repo" || true + fi + + if [[ -d "$TMPDIR/repo" ]]; then + FOUND="$(find "$TMPDIR/repo" -maxdepth 6 -type f -name dsync -print -quit || true)" + fi +fi + +# 2) If git did not find the file, try to download raw file via curl (main/master) +if [[ -z "$FOUND" ]]; then + if command -v curl >/dev/null 2>&1; then + for BR in main master; do + RAW_URL="$RAW_BASE/$BR/dsync" + echo "Trying to download raw file: $RAW_URL" + if curl --fail -L --max-redirs 5 -o "$TMPDIR/dsync" "$RAW_URL"; then + # check that downloaded file looks like text/script, not HTML error + if file "$TMPDIR/dsync" | grep -qE 'text|python|ASCII'; then + FOUND="$TMPDIR/dsync" + break + else + echo "Downloaded file from $RAW_URL does not look like a script - skipping." + fi + else + echo "Failed to download $RAW_URL (404 or error)." + fi + done + else + echo "curl not installed and git did not yield a dsync file — cannot download." >&2 + exit 1 + fi +fi + +if [[ -z "$FOUND" ]]; then + echo "Could not locate dsync in the repository and raw download failed." + echo "Repository may be private. If it's private, ensure you have access (SSH keys or credentials)." + exit 1 +fi + +echo "Found source file at: $FOUND" + +# Warn if target exists +if [[ -e "$DEST_PATH" && "$FORCE" -ne 1 ]]; then + echo "Error: $DEST_PATH already exists. Use --force to overwrite." >&2 + exit 1 +fi + +# copy into destination (use sudo if target dir is not writable) +if touch "$DEST_DIR/.write_test" >/dev/null 2>&1; then + rm -f "$DEST_DIR/.write_test" + cp -f "$FOUND" "$DEST_PATH" + chmod +x "$DEST_PATH" + echo "Installed: $DEST_PATH" +else + echo "No write permission to $DEST_DIR — using sudo to copy and set executable bit." + if ! command -v sudo >/dev/null 2>&1; then + echo "sudo is not available — cannot write to $DEST_DIR" >&2 + exit 1 + fi + sudo cp -f "$FOUND" "$DEST_PATH" + sudo chmod +x "$DEST_PATH" + echo "Installed (via sudo): $DEST_PATH" +fi + +# quick PATH check +echo +echo "PATH check:" +if command -v dsync >/dev/null 2>&1; then + echo "dsync is found: $(command -v dsync)" +else + echo "dsync was installed to $DEST_PATH but is not found in current PATH." + echo "Make sure $DEST_DIR is in your PATH (add to ~/.profile, ~/.bashrc or ~/.zshrc if needed):" + echo " export PATH=\"$DEST_DIR:\$PATH\"" + echo "Then run 'hash -r' (bash) or 'rehash' (zsh) or restart your shell." +fi + +echo "Done."