summaryrefslogtreecommitdiff
path: root/usr/src/tools/scripts/hgsetup.sh
blob: 422e10213bb69f825c9da4d05e02ec363df406c3 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#! /usr/bin/ksh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"%Z%%M%	%I%	%E% SMI"
#

#
# Easy setup script for populating a user's ~/.hgrc
# This currently does the following:
#	* Load the cadmium extension
#	* Populate the author/email fields to be correct
#	* Alias canonical repositories like onnv-gate
#	* Configures mercurial to use appropriate merge tools
#
# See hgrc(5) for more information
#

HGRC=$HOME/.hgrc

usage() {
	prog=$(basename "$0")
	echo \
"usage: $prog [-f] [-c cdm_path] [-m merge_path] [-n name] [-e email] [-p proxy]
	-f            : force overwriting $HGRC
	-c cdm_path   : override Cadmium path
	-m merge_path : override path to merge tool
	-n name       : override name (for ui.username)
	-e email      : override email (for email.from)
	-p proxy      : enable use of web proxy with specified proxy

	if -e isn't provided, and you are on SWAN, an LDAP query is done
	if -n isn't provided, the entry from /etc/passwd is used
	
	proxy should be in the form of hostname:port
	if on-SWAN, $prog will lookup your email address.  this can be
	overridden by using the -e flag.
	"
	exit 1
}

while getopts c:e:fm:n:p: opt; do
	case "$opt" in
	c) cdm_path=$OPTARG;;
	e) email=$OPTARG;;
	f) force=1;;
	m) merge_path=$OPTARG;;
	n) name=$OPTARG;;
	p) proxy=$OPTARG;;
	*) usage;;
	esac
done

if [ -f $HGRC -a "$force" -eq 0 ]; then
	echo "Error: You have an existing .hgrc in $HGRC"
	echo "Please move it aside."
	exit 1
fi

AWK="/usr/xpg4/bin/awk"
SED="/usr/bin/sed"
LDAPCLIENT="/usr/bin/ldapsearch"

login=$(/usr/bin/id -un)

#
# Try and determine where Cadmium is installed.  In order of
# preference, look in:
#
#   1. $(whence $0), on the assumption that you want the version
#      of cadmium that best matches the hgsetup script you invoked
#
#   2. /opt/onbld, because local is generally better
#
#   3. /ws/onnv-tools/onbld, it's nfs and it might be slow, but it
#      should resolve from most places on-SWAN
#
paths="$(dirname $(whence $0)) /opt/onbld /ws/onnv-tools/onbld"
cdmbin="lib/python/onbld/hgext/cdm.py"

for dir in $paths; do
	if [[ -f "$dir/$cdmbin" && -z "$cdm_path" ]]; then
		cdm_path="$dir/$cdmbin"
		break
	fi
done

if [[ -n $proxy ]]; then
	proxyConfig="[http_proxy]
host=$proxy
"
fi

if getent hosts sunweb.central.sun.com >/dev/null; then
	# on SWAN
	echo "Detected SWAN connection"
	ldapemail='preferredrfc822recipient'
	ldapquery="uid=$login $ldapemail"
	ldapcmd="$LDAPCLIENT -1 -h sun-ds -b dc=sun,dc=com $ldapquery"
	if [[ -z "$email" ]]; then 
		echo "Looking up e-mail address in LDAP"
		email=${email:=$($ldapcmd | $AWK /^$ldapemail:/'{print $2}')}
	fi
fi

if [[ -z $email ]]; then
	my_id=$(id -un)
	my_checkhostname=$(check-hostname)
	my_fqhn=${my_checkhostname##* }
	email="$my_id@$my_fqhn"
	echo "No e-mail address provided, defaulting to $email"
fi

if [[ -z "$name" ]]; then
	name=${name:=$(getent passwd $login | awk -F: '{print $5}')}
fi
username="$name <$email>"

echo "Configured the following:"
if [[ -n $proxy ]]; then
	echo "	proxy: $proxy"
fi
echo "	email: $email"
echo "	username: $name"
echo "	cadmium: $cdm_path"

if [[ -z "$cdm_path" ]]; then
	echo "Warning: you will need to edit your .hgrc file\n" \
	     "to specify a path for cadmium."
fi

if [[ -n $merge_path ]]; then
	echo "	merge: $merge_path"
fi

cat <<EOF >$HGRC
$proxyConfig[extensions]
hgext.cdm=$cdm_path

[email]
from=$email

[paths]
onnv-gate=ssh://anon@hg.opensolaris.org/hg/onnv/onnv-gate

[merge-tools]
filemerge.gui=True
filemerge.args=-a \$base \$local \$other \$output
filemerge.priority=1

meld.gui=True
meld.priority=0

gpyfm.gui=True
gpyfm.priority=0

[ui]
username=$username
EOF

if [[ -n $merge_path ]]; then
	echo "merge=$merge_path" >> $HGRC
fi

echo "Please check $HGRC and verify everything looks correct"