summaryrefslogtreecommitdiff
path: root/mk/plist/doc-compress
blob: 5f40a460fc46c7428a52a7c85518516285be4aab (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
#!/bin/sh
#
# $NetBSD: doc-compress,v 1.1 2006/01/12 23:43:57 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.
#

: ${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"
}

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

# 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} $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} $pathgz
			${TEST} "$verbose" = no ||
				${ECHO} "Decompressing: $file.gz"
		fi
		;;
	esac
done