diff --git a/configs/nixos/modules/shell/fish/default.nix b/configs/nixos/modules/shell/fish/default.nix index 387c451..998f46d 100644 --- a/configs/nixos/modules/shell/fish/default.nix +++ b/configs/nixos/modules/shell/fish/default.nix @@ -30,6 +30,7 @@ in home-manager.users.${user} = { home.packages = with pkgs; [ bat # backs the manpager + e2fsprogs # provides chattr and lsattr for the readonly function fastfetch # the shell greeting ]; @@ -99,6 +100,11 @@ in body = builtins.readFile ./functions/deploy-to.fish; }; + readonly = { + description = "Toggle write protection for a Borderlands 2 save"; + body = builtins.readFile ./functions/readonly.fish; + }; + }; # Rendered by home-manager into ~/.config/fish/config.fish. diff --git a/configs/nixos/modules/shell/fish/functions/readonly.fish b/configs/nixos/modules/shell/fish/functions/readonly.fish new file mode 100644 index 0000000..0f10e9b --- /dev/null +++ b/configs/nixos/modules/shell/fish/functions/readonly.fish @@ -0,0 +1,35 @@ +if test (count $argv) -ne 1 + echo "usage: readonly " >&2 + return 2 +end + +set save_number $argv[1] +if not string match --quiet --regex '^[0-9]{4}$' -- "$save_number" + echo "readonly: save number must contain four digits" >&2 + return 2 +end + +set save_name "Save$save_number.sav" +set save "$HOME/.local/share/Steam/steamapps/compatdata/49520/pfx/drive_c/users/steamuser/Documents/My games/Borderlands 2/WillowGame/SaveData/76561198060918970/$save_name" + +if not test -f "$save" + echo "readonly: save not found: $save" >&2 + return 1 +end + +set attribute_output (lsattr -d -- "$save") +or begin + echo "readonly: failed to read attributes for $save_name" >&2 + return 1 +end + +set attribute_flags (string split --max 1 ' ' -- "$attribute_output")[1] +if string match --quiet '*i*' -- "$attribute_flags" + sudo chattr -i -- "$save" + or return $status + echo "$save_name is now writable" +else + sudo chattr +i -- "$save" + or return $status + echo "$save_name is now read-only" +end