dotfiles/packages/lntocp/default.nix

72 lines
2.2 KiB
Nix

{
# Snowfall Lib provides a customized `lib` instance with access to your flake's library
# as well as the libraries available from your flake's inputs.
lib,
# You also have access to your flake's inputs.
inputs,
# All other arguments come from NixPkgs. You can use `pkgs` to pull packages or helpers
# programmatically or you may add the named attributes as arguments here.
pkgs,
stdenv,
...
}:
pkgs.writeShellApplication {
name = "lntocp";
runtimeInputs = with pkgs; [ ];
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
'';
}