* create_link.sh
#!/usr/bin/env sh
# Function to display usage
usage() {
echo "Usage: $0 /path/to/file"
exit 1
}
# Check if the path is provided as an argument
if [ "$#" -ne 1 ]; then
usage
fi
path="$1"
# Check if the provided path is valid
if [ ! -f "$path" ]; then
echo "Error: File '$path' not found."
usage
fi
file=$(basename "$path")
# Function to URL encode filenames
url_encode() {
local string="$1"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[a-zA-Z0-9.~_-]) o="$c" ;;
*) printf -v o '%%%02X' "'$c" ;;
esac
encoded+="$o"
done
echo "$encoded"
}
# Assume ed2k_hash is a command that generates the ed2k hash for the file
ed2k_output=$(ed2k_hash "$path")
file_size=$(stat -c%s "$path")
ed2k_link=$(echo "$ed2k_output" | awk -F'|' '{print $5}')
url_encoded_filename=$(url_encode "$file")
echo "ed2k://|file|$url_encoded_filename|$file_size|$ed2k_link|/"