1#!/bin/sh
2# $NetBSD: prepare-import.sh,v 1.1 2013/02/23 14:16:49 jmmv Exp $
3#
4# Use this script to recreate the 'dist' subdirectory from a newly released
5# distfile.  The script takes care of unpacking the distfile, removing any
6# files that are not relevant to NetBSD and checking if there are any new
7# files in the new release that need to be addressed.
8#
9
10set -e
11
12ProgName=${0##*/}
13
14CLEAN_PATTERNS=
15CLEAN_PATTERNS="${CLEAN_PATTERNS} *.m4"
16CLEAN_PATTERNS="${CLEAN_PATTERNS} INSTALL TODO"
17CLEAN_PATTERNS="${CLEAN_PATTERNS} Doxyfile*"
18CLEAN_PATTERNS="${CLEAN_PATTERNS} Makefile* */Makefile* */*/Makefile*"
19CLEAN_PATTERNS="${CLEAN_PATTERNS} admin"
20CLEAN_PATTERNS="${CLEAN_PATTERNS} api-docs"
21CLEAN_PATTERNS="${CLEAN_PATTERNS} config.h.in"
22CLEAN_PATTERNS="${CLEAN_PATTERNS} configure*"
23CLEAN_PATTERNS="${CLEAN_PATTERNS} m4"
24CLEAN_PATTERNS="${CLEAN_PATTERNS} utils/defs.hpp"
25
26err() {
27	echo "${ProgName}:" "${@}" 1>&2
28	exit 1
29}
30
31log() {
32	echo "${ProgName}:" "${@}"
33}
34
35backup_dist() {
36	if [ -d dist.old ]; then
37		log "Removing dist; dist.old exists"
38		rm -rf dist
39	else
40		log "Backing up dist as dist.old"
41		mv dist dist.old
42	fi
43}
44
45extract_distfile() {
46	local distfile="${1}"; shift
47	local distname="${1}"; shift
48
49	log "Extracting ${distfile}"
50	tar -xzf "${distfile}"
51	[ -d "${distname}" ] || err "Distfile did not create ${distname}"
52	log "Renaming ${distname} to dist"
53	mv "${distname}" dist
54}
55
56get_distname() {
57	local distfile="${1}"; shift
58	basename "${distfile}" | sed -e 's,\.tar.*,,'
59}
60
61cleanup_dist() {
62	log "Removing unnecessary files from dist"
63	( cd dist && rm -rf ${CLEAN_PATTERNS} )
64}
65
66diff_dirs() {
67	local old_dir="${1}"; shift
68	local new_dir="${1}"; shift
69
70	local old_list=$(mktemp -t kyua-cli-import.XXXXXX)
71	local new_list=$(mktemp -t kyua-cli-import.XXXXXX)
72	local diff=$(mktemp -t kyua-cli-import.XXXXXX)
73	trap "rm -f '${old_list}' '${new_list}' '${diff}'; exit 1" \
74	    HUP INT QUIT TERM
75
76	( cd "${old_dir}" && find . | sort >>"${old_list}" )
77	( cd "${new_dir}" && find . | sort >>"${new_list}" )
78
79	diff -u "${old_list}" "${new_list}" | grep '^+\.' >>"${diff}" || true
80	if [ -s "${diff}" ]; then
81		log "New files found"
82		diff -u "${old_list}" "${new_list}" | grep '^+\.'
83		log "Check if any files have to be cleaned up and update" \
84		    "the prepare-import.sh script accordingly"
85	else
86		log "No new files; all good!"
87	fi
88
89	rm -f "${old_list}" "${new_list}" "${diff}"
90}
91
92main() {
93	[ ${#} -eq 1 ] || err "Must provide a distfile name"
94	local distfile="${1}"; shift
95
96	[ -f Makefile -a -f prepare-import.sh ] || \
97	    err "Must be run from the src/external/bsd/kyua-cli subdirectory"
98
99	local distname="$(get_distname ${distfile})"
100
101	backup_dist
102	extract_distfile "${distfile}" "${distname}"
103	cleanup_dist
104	diff_dirs dist.old dist
105}
106
107main "${@}"
108