cask "msgfiler" do
  version "4.3.1.66,20251231,133402"
  sha256 "4e4a719803828a23a2706daae9041188044bfed92b0d604c3abfb7fa855fb48c"

  url "https://files.msgfiler.com/MsgFiler_#{version.csv.first}_#{version.csv.second}_#{version.csv.third}.dmg"
  name "MsgFiler"
  desc "Keyboard-based email filing application for Apple Mail"
  homepage "https://msgfiler.com/"

  # The download page may link to an unstable version that uses the same file
  # name format as the stable version, so we also check the inner text of the
  # links to try to only match the stable version.
  livecheck do
    url "https://files.msgfiler.com/"
    regex(/
      href=.*?MsgFiler[._-]v?(\d+(?:\.\d+)+)[._-](\d+)[._-](\d+)\.dmg[^>]*?>
      \s*MsgFiler\s+v?\d+(?:\.\d+)+(?:\s+\(?Build\s+\d+\)?)?(?:\s+on\s+Gumroad)?\s*<
    /imx)
    strategy :page_match do |page, regex|
      page.scan(regex).map { |match| "#{match[0]},#{match[1]},#{match[2]}" }
    end
  end

  depends_on macos: :ventura

  app "MsgFiler #{version.major}.app"

  zap trash: [
    "~/Library/Application Scripts/com.atow.MsgFiler*",
    "~/Library/Application Scripts/group.com.atow.msgfiler.group",
    "~/Library/Containers/com.atow.MsgFiler*",
    "~/Library/Group Containers/group.com.atow.msgfiler.group",
  ]
end
