1##
2##  Makefile -- FreeBSD UFS/ZFS Backup Snapshot Installation/Uninstallation
3##
4
5#   make sure system tools are used first
6PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH"
7
8#   edit a textfile
9editfile () {
10	file="$1"; shift
11    if [ ! -f $file ]; then
12        cp /dev/null $file
13    fi
14    if [ ! -s $file ]; then
15        echo "" >>$file # workaround sed(1) bug related to '$r ...'
16    fi
17	sed <$file >$file.new "$@"
18	if cmp $file $file.new >/dev/null 2>&1; then
19		rm -f $file.new
20	else
21		cp -p $file $file.old
22		cat $file.new >$file
23		rm -f $file.new
24	fi
25}
26
27#   dispatch into commands
28cmd="$1"
29shift
30case "$cmd" in
31    install )
32        #   install programs
33        install -c -o root -g wheel -m 555 snapshot /usr/sbin/snapshot
34        install -c -o root -g wheel -m 555 periodic-snapshot /usr/sbin/periodic-snapshot
35
36        #   install manual pages
37        install -c -o root -g wheel -m 444 snapshot.8 /usr/share/man/man8/snapshot.8
38        install -c -o root -g wheel -m 444 periodic-snapshot.8 /usr/share/man/man8/periodic-snapshot.8
39        gzip -9 -f /usr/share/man/man8/snapshot.8
40        gzip -9 -f /usr/share/man/man8/periodic-snapshot.8
41
42		#   install /etc/amd.map.snap configuration
43        install -c -o root -g wheel -m 444 amd.map.snap /etc/amd.map.snap
44
45        #   install /etc/crontab extension
46        editfile /etc/crontab \
47            -e '/maintenance (FreeBSD UFS\/ZFS snapshots only)/,/periodic-snapshot weekly/d' \
48            -e '/periodic monthly/r crontab'
49
50        #   install /etc/rc.conf extension
51        editfile /etc/rc.conf \
52            -e '/UFS\/ZFS Snapshot Access/,/amd_flags/d' \
53            -e '$r rc.conf'
54
55        #   install /etc/periodic.conf extension
56        editfile /etc/periodic.conf \
57            -e '/UFS\/ZFS Snapshot Creation/,/snapshot_schedule/d' \
58            -e '$r periodic.conf'
59        ;;
60
61    uninstall )
62        #   uninstall programs
63        rm -f /usr/sbin/snapshot
64        rm -f /usr/sbin/periodic-snapshot
65
66        #   uninstall manual pages
67        rm -f /usr/share/man/man8/snapshot.8.gz
68        rm -f /usr/share/man/man8/periodic-snapshot.8.gz
69
70		#   uninstall /etc/amd.map.snap configuration
71        rm -f /etc/amd.map.snap
72
73        #   uninstall /etc/crontab extension
74        editfile /etc/crontab \
75            -e '/maintenance (FreeBSD UFS\/ZFS snapshots only)/,/periodic-snapshot weekly/d'
76
77        #   install /etc/rc.conf extension
78        editfile /etc/rc.conf \
79            -e '/UFS\/ZFS Snapshot Access/,/amd_flags/d'
80
81        #   install /etc/periodic.conf extension
82        editfile /etc/periodic.conf \
83            -e '/UFS\/ZFS Snapshot Creation/,/snapshot_schedule/d'
84        ;;
85esac
86
87