summaryrefslogtreecommitdiff
path: root/mk/plist/doc-compress
blob: 545659a032c3241d31c5571283fa811db047c06e (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
#!/bin/sh
#
# $NetBSD: doc-compress,v 1.3 2006/01/19 17:22:26 jlam Exp $
#
# This script is derived from software contributed to The NetBSD Foundation
# by Alistair Crooks.
#
# This script compresses or decompresses files listed in standard input.
# It handles symlinks by recreating the symlinks to point to the
# compressed or uncompressed targets.
#

######################################################################
#
# NAME
#	doc-compress -- handle compression of PLIST entries
#
# SYNOPSIS
#	doc-compress [-v] [-z] prefix
#
# DESCRIPTION
#	doc-compress handles compression of files passed in via standard
#	input.  The file paths must be relative to the specified prefix.
#	doc-compress handles symlinks to compressed files intelligently by
#	symlinking to the compressed target if compression is desired, and
#	similarly for decompression.
#
# OPTIONS
#	The following command line arguments are supported.
#
#	-v	Output the action taken for each file (compressing,
#		decompressing, or symlinking) to standard output.
#
#	-z	Compress the files.  By default, the files are decompressed.
#
# ENVIRONMENT
#	MANZ	This variable controls the default action taken.  If "yes",
#		then this is equivalent to specifying the "-z" option.
#
#	PKG_VERBOSE
#		This controls the default verbosity of the output.  If
#		non-empty, then this is equivalent to specifying the "-v"
#		option.
#
######################################################################

: ${ECHO=echo}
: ${EXPR=expr}
: ${GZIP_CMD=gzip}
: ${GUNZIP_CMD=gunzip}
: ${LN=ln}
: ${LS=ls}
: ${RM=rm}
: ${TEST=test}

self="${0##*/}"

usage() {
	${ECHO} 1>&2 "usage: $self [-v] [-z] prefix"
}

compress=no
verbose=no
prefix=/nonexistent

case "$MANZ" in
[yY][eE][sS])	compress=yes ;;
esac
case "$PKG_VERBOSE" in
"")	;;
*)	verbose=yes ;;
esac

# Process optional arguments
while ${TEST} $# -gt 0; do
	case "$1" in
	-v)	verbose=yes; shift ;;
	-z)	compress=yes; shift ;;
	--)	shift; break ;;
	-*)	${ECHO} 1>&2 "$self: unknown option -- ${1#-}"
		usage
		exit 1
		;;
	*)	break ;;
	esac
done

${TEST} $# -gt 0 || { usage; exit 1; }

# Process required arguments
prefix="$1"

while read file; do
	file="${file%.gz}"
	path="$prefix/$file"
	pathgz="$path.gz"
	case "$compress" in
	yes)
		# If compressed pages were requested and we find an
		# uncompressed page, then compress it, but if it was
		# a symlink, then remove it and create a "compressed"
		# symlink by symlinking to the compressed target.
		#
		if ${TEST} -h "$path"; then
			target=`${LS} -l $path`
			target="${target##*-> }"
			${RM} -f $pathgz
			${LN} -s $target.gz $pathgz
			${RM} -f $path
			${TEST} "$verbose" = no ||
				${ECHO} "Symlinking:    $file"
		elif ${TEST} -f "$path"; then
			${GZIP_CMD} -nf $path
			${TEST} "$verbose" = no ||
				${ECHO} "Compressing:   $file"
		fi
		;;
	no)
		# If uncompressed pages were requested and we find a
		# compressed page, then decompress it, but if it was
		# a symlink, then remove it and create an "uncompressed"
		# symlink by symlinking to the uncompressed target.
		#
		if ${TEST} -h "$pathgz"; then
			target=`${LS} -l $pathgz`
			target="${target##*-> }"
			target="${target%.gz}"
			${RM} -f $path
			${LN} -s $target $path
			${RM} -f $pathgz
			${TEST} "$verbose" = no ||
				${ECHO} "Symlinking:    $file.gz"
		elif ${TEST} -f "$pathgz"; then
			${GUNZIP_CMD} -f $pathgz
			${TEST} "$verbose" = no ||
				${ECHO} "Decompressing: $file.gz"
		fi
		;;
	esac
done