1#!/bin/mksh
2# $MirOS: src/scripts/mnt-cvsroot,v 1.18 2009/03/29 13:04:20 tg Exp $
3#-
4# Copyright (c) 2005, 2008
5#	Thorsten “mirabilos” Glaser <tg@mirbsd.org>
6#
7# Provided that these terms and disclaimer and all copyright notices
8# are retained or reproduced in an accompanying document, permission
9# is granted to deal in this work without restriction, including un-
10# limited rights to use, publicly perform, distribute, sell, modify,
11# merge, give away, or sublicence.
12#
13# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
14# the utmost extent permitted by applicable law, neither express nor
15# implied; without malicious intent or gross negligence. In no event
16# may a licensor, author or contributor be held liable for indirect,
17# direct, other damage, loss, or other issues arising in any way out
18# of dealing in the work, even if advised of the possibility of such
19# damage or existence of a defect, except proven that it results out
20# of said person's immediate fault when using the work as intended.
21#-
22# Change CVSROOT of a checked out tree (and save space with it)
23# With option -T: change Tag instead of Root
24
25me=${0##*/}
26if [[ $1 = -T ]]; then
27	tagmode=-T
28	fn=Tag
29	shift
30else
31	tagmode=
32	fn=Root
33fi
34newroot=$1
35useroot=0
36if [[ $newroot = - ]]; then
37	shift
38	newroot=$(realpath "$1")
39	[[ -d $newroot ]] && if [[ -d $newroot/CVS ]]; then
40		newroot=$newroot/CVS/$fn
41	else
42		newroot=$newroot/$fn
43	fi
44	useroot=1
45fi
46if [[ -z $newroot || $newroot = -? ]]; then
47	print -u2 "Syntax: $me newroot [dir [...]]"
48	print -u2 "\t$me - .../CVS/Root [dir [...]]"
49	print -u2 "\t$me -T - .../CVS/Tag [dir [...]]"
50	exit 1
51fi
52shift
53
54[[ -z $1 ]] && if [[ $useroot = 0 ]]; then
55	exec $SHELL $me $tagmode "$newroot" .
56else
57	exec $SHELL $me $tagmode - "$newroot" .
58fi
59
60# realpath(2)ise arguments
61set -A arg
62let i=0
63for name in "$@"; do
64	arg[i++]=$(realpath "$name")
65done
66
67if ! T="$(mktemp ${arg[0]}/$me.XXXXXXXXXX)"; then
68	print -u2 "$me: fatal: cannot mktemp"
69	exit 1
70fi
71
72if [[ $useroot = 1 ]]; then
73	rm -f "$T"
74	ln "$newroot" "$T" || cp "$newroot" "$T"
75fi
76
77trap 'rm -f "$T"; exit 0' 0
78trap 'rm -f "$T"; trap - EXIT; exit 1' 1 2 3 5 13 15
79
80if [[ $useroot = 0 ]]; then
81	chmod 664 "$T"
82	print -r -- "$newroot" >"$T"
83fi
84
85let rv=0
86find "${arg[@]}" -path \*/CVS/$fn |&
87while read -p name; do
88	if ! rm "$name"; then
89		print -u2 "$me: error: cannot rm <$name>"
90		exit 1
91	fi
92	ln -f "$T" "$name" || if ! U="$(mktemp ${arg[0]}/$me.XXXXXXXXXX)"; then
93		cp "$T" "$name"
94	elif cat "$T" >"$U" && ln -f "$U" "$name"; then
95		rm -f "$T"
96		T="$U"
97	else
98		rm -f "$U"
99		cp "$T" "$name"
100	fi
101done
102
103exit 0
104