diff --git a/.local/bin/rshare b/.local/bin/rshare index 7cb04d76..8a3be5d2 100755 --- a/.local/bin/rshare +++ b/.local/bin/rshare @@ -1,8 +1,9 @@ #!/bin/sh -PORT=8888 +PORT=8080 CONFIG=$HOME/.config/rsync/rsync.conf CACHE_DIR=$HOME/.cache/rsyncd +OPT_HTTP=false IP=$(ip addr | awk '/state UP/ {getline; getline; $0=$2; gsub(/\/.*/, "");print; exit}') @@ -10,23 +11,32 @@ usage () { cat <&2 Usage: ${0##*/} PATH -Startup an rsync daemon and share PATH as read-only under the 'files' module. -Clients can sync with, for instance: +Share PATH over the network. - rsync -iavzzP rsync://$IP:$PORT/files +By default, startup an rsync daemon and share PATH as read-only under the +'files' module. Clients can sync with, for instance: + + rsync -iavzzP rsync://$IP:$PORT/files DESTINATION + +An HTTP server can be started instead. Options: -p PORT: Specify a port number (default: $PORT). Must be above 1024 to run without privileges. + + -H: Start an HTTP server instead. + EOF } -while getopts ":hp:" opt; do +while getopts ":hHp:" opt; do case $opt in h) usage exit ;; + H) + OPT_HTTP=true ;; p) PORT="$OPTARG" ;; \?) @@ -41,11 +51,12 @@ shift $(($OPTIND - 1)) [ "$1" = "-h" ] && usage && exit [ "$1" = "--" ] && shift -mkdir -p "$(dirname "$CONFIG")" "$CACHE_DIR" TARGET=$(realpath "$1") -cat<"$CONFIG" +share_rsync(){ + mkdir -p "$(dirname "$CONFIG")" "$CACHE_DIR" + cat<"$CONFIG" pid file = $CACHE_DIR/rsyncd.pid lock file = $CACHE_DIR/rsyncd.lock log file = $CACHE_DIR/rsyncd.log @@ -60,5 +71,32 @@ timeout = 300 EOF -rsync --daemon --config="$CONFIG" && \ - echo "rsync daemon listening on $IP:$PORT" + rsync --daemon --config="$CONFIG" && \ + echo >&2 "rsync daemon listening on $IP:$PORT" +} + +share_woof() { + woof -c 9999 -p $PORT "$TARGET" +} + +share_python() { + echo >&2 "Python HTTP server will listen on $IP:$PORT." + if command -v guix >/dev/null 2>&1; then + guix environment -C -N --expose="$TARGET"="$TARGET" --ad-hoc python -- \ + python3 -m http.server -d "$TARGET" $PORT + elif command -v python3 >/dev/null 2>&1; then + python3 -m http.server -d "$TARGET" $PORT + else + python -m http.server -d "$TARGET" $PORT + fi +} + +if $OPT_HTTP; then + if [ -f "$TARGET" ]; then + share_woof "$TARGET" + else + share_python "$TARGET" + fi +else + share_rsync +fi