dotfiles/pkgs/lntocp/default.nix

59 lines
1.8 KiB
Nix

{writeShellApplication}:
writeShellApplication {
name = "lntocp";
runtimeInputs = [];
text = ''
#!/bin/bash
# This script accepts any number of files or directories as input.
# If the input is a symbolic link, it deletes the symlink and copies
# the original file or folder (where the symlink points to) to the location
# where the symlink was. Non-symlink files or directories are ignored.
# Function to copy original file/folder in place of the symlink
process_symlink() {
symlink="$1"
# Resolve the target (the file or directory the symlink points to)
target=$(readlink "$symlink")
# Check if the target is absolute or relative
if [[ ! "$target" =~ ^/ ]]; then
# If the target is relative, compute the full path
target="$(dirname "$symlink")/$target"
fi
# Check if the target exists
if [ -e "$target" ]; then
echo "Processing symlink: $symlink -> $target"
# Remove the symlink
rm "$symlink"
# Copy the original target to the symlink location
if [ -d "$target" ]; then
# If it's a directory, use 'cp -r'
cp -r "$target" "$symlink"
else
# Otherwise, it's a file, use 'cp'
cp "$target" "$symlink"
fi
echo "Copied $target to $symlink"
else
echo "Target $target does not exist!"
fi
}
# Loop through all arguments (files and directories passed to the script)
for item in "$@"; do
if [ -L "$item" ]; then
# If the item is a symlink, process it
process_symlink "$item"
else
echo "$item is not a symlink. Skipping."
fi
done
'';
}