summaryrefslogtreecommitdiffstats
path: root/sbin/keyword_package
blob: 4ecb034e54d1feaf84324bc989eceaaee72a0180 (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
#!/bin/sh

# collecting required information
ARCH="$(portageq envvar ARCH)"
PORTCONF_DIR="/etc/portage"

# check if keywords file exists
if [ ! -f ${PORTCONF_DIR}/package.keywords ]
then
	echo "Creating ${PORTCONF_DIR}/package.keywords..."
	touch ${PORTCONF_DIR}/package.keywords
fi

# check if special lines in keyword file exists
SPECIAL_LINE_COUNT="$(grep -nE "(/\*)|(\*/)|(^[ \t]*#)" ${PORTCONF_DIR}/package.keywords | tail -n 1 | awk -F: '{ print $1 }')"
if [ -z "${SPECIAL_LINE_COUNT}" ]
then
	SPECIAL_LINE_COUNT=0
fi

# create tmpfile
TMP_FILE=$(mktemp)

# copy untouched special lines to new file
head -n $SPECIAL_LINE_COUNT ${PORTCONF_DIR}/package.keywords > $TMP_FILE

(
	while [ -n "${1}" ] ; do
		LINE="${1} ~${ARCH}"
		if [ -z "$(grep "^${LINE}$" ${PORTCONF_DIR}/package.keywords)" ]
		then
			# adding requested package
			echo "${LINE}"
			echo "adding \"${LINE}\" to ${PORTCONF_DIR}/package.keywords" | logger -st $(basename $0)
		else
			# requested package already there
			echo "Don't adding \"${1}\". Already there." > /dev/stderr
		fi

		shift 1
	done
	
	# listing all remaining packages in file
	tail -n $(($(cat ${PORTCONF_DIR}/package.keywords | wc -l)-$SPECIAL_LINE_COUNT)) ${PORTCONF_DIR}/package.keywords

# sort packages and seperate categories by new line
) | grep -v "^$" | sort | awk -F/ '{ if (OLD_PREFIX!=$1) print "";  OLD_PREFIX=$1 ; print }' >> $TMP_FILE

# creatbackup and move temporary file to destination
mv ${PORTCONF_DIR}/package.keywords ${PORTCONF_DIR}/package.keywords.bak
mv $TMP_FILE ${PORTCONF_DIR}/package.keywords
chmod +r ${PORTCONF_DIR}/package.keywords