summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/flameshot-once/default.nix
blob: 3626409f3de3ee17974564460da41d13b3182d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{ name ? "flameshot-once", pkgs, ... }@args:
with pkgs.stockholm.lib;

let
  # config cannot be declared in the input attribute set because that would
  # cause callPackage to inject the wrong config.  Instead, get it from ...
  # via args.
  config = args.config or {};

  cfg = evalModulesConfig (singleton {
    _file = toString ./default.nix;
    _module.args.pkgs = pkgs;
    imports = [
      config
      ./config.nix
    ];
  });
in

pkgs.symlinkJoin {
  inherit name;
  paths = [
    (pkgs.write "flameshot-once" {
      "/bin/flameshot-once" = {
        executable = true;
        text = /* sh */ ''
          #! ${pkgs.dash}/bin/dash
          export PATH=${makeBinPath [
            pkgs.qt5.qtbase
          ]}:''${PATH+:$PATH}
          ${optionalString (config != null) /* sh */ ''
            export XDG_CONFIG_HOME=${placeholder "out"}/etc
            ${optionalString cfg.imgur.enable /* sh */ ''
              export IMGUR_CREATE_URL=${shell.escape cfg.imgur.createUrl}
              export IMGUR_DELETE_URL=${shell.escape cfg.imgur.deleteUrl}
              ${optionalString cfg.imgur.xdg-open.enable /* sh */ ''
                export PATH=${placeholder "out"}/lib/imgur/bin''${PATH+:$PATH}
              ''}
            ''}
          ''}
          ${cfg.package}/bin/flameshot &
          exec ${cfg.package}/bin/flameshot gui
        '';
      };
      "/etc/flameshot/flameshot.ini".text =
        lib.generators.toINI {} (stripAttr cfg.settings);
      ${if cfg.imgur.enable then "/lib/imgur/bin/xdg-open" else null} = {
        executable = true;
        text = /* sh */ ''
          #! ${pkgs.dash}/bin/dash
          set -efu
          uri=$1
          prefix=$(${pkgs.coreutils}/bin/dirname "$uri")
          case $prefix in
            (${shell.escape cfg.imgur.xdg-open.createPrefix})
              echo "opening image in browser: $uri" >&2
              exec ${config.imgur.xdg-open.browser} "$uri"
              ;;
            (${shell.escape cfg.imgur.xdg-open.deletePrefix})
              echo "deleting image: $uri" >&2
              exec ${pkgs.curl}/bin/curl -fsS -X DELETE "$uri"
              ;;
            (*)
              echo "don't know how to open URI: $uri" >&2
              exit 1
          esac
        '';
      };
    })
  ];
}
// {
  dev = pkgs.write "flameshot-once-tools" {
    "/bin/get-buttonTypes" = {
      executable = true;
      text = /* sh */ ''
        #! ${pkgs.dash}/bin/dash
        indent=$(${placeholder "out"}/bin/indent-of buttonTypes)
        src=${cfg.package.src}/src/tools/capturetool.h
        ${pkgs.coreutils}/bin/cat "$src" |
        ${pkgs.gnused}/bin/sed -nr '
          s/^\s*(TYPE_\S+)\s*=\s*([0-9]+),/\1 = \2;/p
        ' |
        ${placeholder "out"}/bin/prefix "  $indent"
      '';
    };
    "/bin/get-iterableButtonTypes" = {
      executable = true;
      text = /* sh */ ''
        #! ${pkgs.dash}/bin/dash
        indent=$(${placeholder "out"}/bin/indent-of iterableButtonTypes)
        src=${cfg.package.src}/src/widgets/capture/capturetoolbutton.cpp
        ${pkgs.coreutils}/bin/cat "$src" |
        ${pkgs.gnused}/bin/sed -n '/\<iterableButtonTypes = {/,/^}/p' |
        ${pkgs.gcc}/bin/cpp |
        ${pkgs.coreutils}/bin/tr , \\n |
        ${pkgs.gnused}/bin/sed -rn 's/^ *CaptureTool::(TYPE_[A-Z_]+).*/"\1"/p' |
        ${pkgs.coreutils}/bin/sort |
        ${placeholder "out"}/bin/prefix "  $indent"
      '';
    };
    "/bin/get-recognizedGeneralOptions" = {
      executable = true;
      text = /* sh */ ''
        #! ${pkgs.dash}/bin/dash
        src=${cfg.package.src}/src/utils/confighandler.cpp
        ${pkgs.coreutils}/bin/cat "$src" |
        ${pkgs.gnused}/bin/sed -n '/\<recognizedGeneralOptions = {/,/^};/p' |
        ${pkgs.gcc}/bin/cpp |
        ${pkgs.gnugrep}/bin/grep -F OPTION |
        ${pkgs.coreutils}/bin/sort
      '';
    };
    "/bin/get-Shortcuts" = {
      executable = true;
      text = /* sh */ ''
        #! ${pkgs.dash}/bin/dash
        indent=$(${placeholder "out"}/bin/indent-of Shortcuts)
        src=${cfg.package.src}/src/utils/confighandler.cpp
        ${pkgs.coreutils}/bin/cat "$src" |
        ${pkgs.gnused}/bin/sed -n '/recognizedShortcuts = {/,/^};/p ' |
        ${pkgs.gcc}/bin/cpp |
        ${pkgs.gnused}/bin/sed -nr 's/^\s*SHORTCUT\("(TYPE_[^"]+).*/"\1"/p' |
        ${pkgs.coreutils}/bin/sort |
        ${placeholder "out"}/bin/prefix "  $indent"
      '';
    };
    "/bin/indent-of" = {
      executable = true;
      text = /* sh */ ''
        #! ${pkgs.dash}/bin/dash
        # usage: indent-of NAME NIX_FILE
        exec ${pkgs.gawk}/bin/awk -v name="$1" '
          $1 == name && $2 == "=" {
            sub("[^ ].*", "")
            print
          }
        ' ${./config.nix}
      '';
    };
    "/bin/prefix" = {
      executable = true;
      text = /* sh */ ''
        #! ${pkgs.dash}/bin/dash
        ${pkgs.gawk}/bin/awk -v prefix="$1" '{ print prefix $0 }'
      '';
    };
  };
}