aboutsummaryrefslogtreecommitdiff
path: root/Keywords/sample.ucl
blob: 61918f1c840614668350e0a518eb08b00b54f922 (plain) (blame)
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
# MAINTAINER: portmgr@FreeBSD.org
#
# @sample etc/somefile.conf.sample
# or
# @sample file1 file2
#
# Where file1 is considered as a sample file and file2 the target file
#
# This will install the somefile.conf.sample and automatically copy to
# somefile.conf if it doesn't exist. On deinstall it will remove the
# somefile.conf if it still matches the sample, otherwise it is
# kept.
#
# This replaces the old pattern:
#  @unexec if cmp -s %D/etc/pkgtools.conf %D/etc/pkgtools.conf.sample; then rm -f %D/etc/pkgtools.conf; fi
#  etc/pkgtools.conf.sample
#  @exec [ -f %B/pkgtools.conf ] || cp %B/%f %B/pkgtools.conf

actions: [file(1)]
arguments: true
prepackaging: <<EOS
  if #arg == 1 then
    if string.find(arg[1], "%.sample$") == nil then
      io.stderr:write("@sample with 1 single argument expect \".sample\" extension\n")
      return(1)
    end
  elseif #arg == 2 then
    if arg[1] == arg[2] then
      io.stderr:write("@sample: 2 identicals arguments are provided\n")
      return(1)
    end
  else
    io.stderr:write("@sample expect 1 or 2 arguments, got " .. #arg .. "\n")
    return(1)
  end
EOS
post-install-lua: <<EOS
  args = {}
  for arg in string.gmatch("%@", "%S+") do
    table.insert(args, arg)
  end
  sample_file = pkg.prefixed_path(args[1])
  if args[2] == nil then
    target_file = string.gsub(sample_file,'%.sample$', "")
  else
    target_file = pkg.prefixed_path(args[2])
  end
  if not pkg.stat(target_file) then
    pkg.copy(sample_file, target_file)
  end
EOS

pre-deinstall-lua: <<EOS
  args = {}
  for arg in string.gmatch("%@", "%S+") do
    table.insert(args, arg)
  end
  sample_file = pkg.prefixed_path(args[1])
  if args[2] == nil then
    target_file = string.gsub(sample_file,'%.sample$', "")
  else
    target_file = pkg.prefixed_path(args[2])
  end
  if pkg.filecmp(sample_file, target_file) == 0 then
    os.remove(target_file)
  else
    pkg.print_msg("You may need to manually remove " .. target_file .. " if it is no longer needed.")
  end
EOS