xref: /titanic_50/usr/src/tools/scripts/hgsetup.sh (revision 24fe0b3bf671e123467ce1df0b67cadd3614c8e4)
1#! /usr/bin/ksh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Easy setup script for populating a user's ~/.hgrc
30# This currently does the following:
31#	* Load the cadmium extension
32#	* Populate the author/email fields to be correct
33#	* Alias canonical repositories like onnv-gate
34#	* Configures mercurial to use appropriate merge tools
35#
36# See hgrc(5) for more information
37#
38
39HGRC=$HOME/.hgrc
40
41usage() {
42	prog=$(basename "$0")
43	echo \
44"usage: $prog [-f] [-c cdm_path] [-m merge_path] [-n name] [-e email] [-p proxy] [-s style_path]
45	-f            : force overwriting $HGRC
46	-c cdm_path   : override Cadmium path
47	-m merge_path : override path to merge tool
48	-n name       : override name (for ui.username)
49	-e email      : override email (for email.from)
50	-p proxy      : enable use of web proxy with specified proxy
51	-s style_path : override path to style file
52
53	if -e isn't provided, and you are on SWAN, an LDAP query is done
54	if -n isn't provided, the entry from /etc/passwd is used
55
56	proxy should be in the form of hostname:port
57	if on-SWAN, $prog will lookup your email address.  this can be
58	overridden by using the -e flag.
59	"
60	exit 1
61}
62
63while getopts c:e:fm:n:p:s: opt; do
64	case "$opt" in
65	c) cdm_path=$OPTARG;;
66	e) email=$OPTARG;;
67	f) force=1;;
68	m) merge_path=$OPTARG;;
69	n) name=$OPTARG;;
70	p) proxy=$OPTARG;;
71	s) style_path=$OPTARG;;
72	*) usage;;
73	esac
74done
75
76if [ -f $HGRC -a "$force" -eq 0 ]; then
77	echo "Error: You have an existing .hgrc in $HGRC"
78	echo "Please move it aside."
79	exit 1
80fi
81
82AWK="/usr/xpg4/bin/awk"
83SED="/usr/bin/sed"
84LDAPCLIENT="/usr/bin/ldapsearch"
85
86login=$(/usr/bin/id -un)
87
88#
89# Try and determine where SUNWonbld is installed.  In order of
90# preference, look in:
91#
92#   1. $(whence $0), on the assumption that you want the version
93#      of SUNWonbld that best matches the hgsetup script you invoked
94#
95#   2. /opt/onbld, because local is generally better
96#
97#   3. /ws/onnv-tools/onbld, it's nfs and it might be slow, but it
98#      should resolve from most places on-SWAN
99#
100paths="$(dirname $(dirname $(whence $0))) /opt/onbld /ws/onnv-tools/onbld"
101cdmbin="lib/python/onbld/hgext/cdm.py"
102stylefile="etc/hgstyle"
103
104for dir in $paths; do
105	if [[ -f "$dir/$cdmbin" && -z "$cdm_path" ]]; then
106		cdm_path="$dir/$cdmbin"
107	fi
108
109	if [[ -f "$dir/$stylefile" && -z "$style_path" ]]; then
110		style_path="$dir/$stylefile"
111	fi
112
113	if [[ -n "$cdm_path" && -n "$style_path" ]]; then
114		break
115	fi
116done
117
118if [[ -n $proxy ]]; then
119	proxyConfig="[http_proxy]
120host=$proxy
121"
122fi
123
124if getent hosts sunweb.central.sun.com >/dev/null; then
125	# on SWAN
126	echo "Detected SWAN connection"
127	ON_SWAN=1
128	ldapemail='preferredrfc822recipient'
129	ldapquery="uid=$login $ldapemail"
130	ldapcmd="$LDAPCLIENT -1 -h sun-ds -b dc=sun,dc=com $ldapquery"
131	if [[ -z "$email" ]]; then
132		echo "Looking up e-mail address in LDAP"
133		email=${email:=$($ldapcmd | $AWK /^$ldapemail:/'{print $2}')}
134	fi
135fi
136
137if [[ -z $email ]]; then
138	my_id=$(id -un)
139	my_checkhostname=$(check-hostname)
140	my_fqhn=${my_checkhostname##* }
141	email="$my_id@$my_fqhn"
142	echo "No e-mail address provided, defaulting to $email"
143fi
144
145if [[ -z "$name" ]]; then
146	name=${name:=$(getent passwd $login | awk -F: '{print $5}')}
147fi
148username="$name <$email>"
149
150echo "Configured the following:"
151if [[ -n $proxy ]]; then
152	echo "	proxy: $proxy"
153fi
154echo "	email: $email"
155echo "	username: $name"
156echo "	style: $style_path"
157echo "	cadmium: $cdm_path"
158
159if [[ -z "$cdm_path" ]]; then
160	echo "Warning: you will need to edit your .hgrc file\n" \
161	     "to specify a path for cadmium."
162fi
163
164if [[ -n $merge_path ]]; then
165	echo "	merge: $merge_path"
166fi
167
168cat <<EOF >$HGRC
169$proxyConfig[extensions]
170hgext.cdm=$cdm_path
171
172[email]
173from=$email
174
175[paths]
176EOF
177
178if [[ -n $ON_SWAN ]]; then
179	cat <<EOF >> $HGRC
180onnv-gate=ssh://onnv.sfbay.sun.com//export/onnv-gate
181onnv-clone=ssh://onnv.sfbay.sun.com//export/onnv-clone
182onnv-closed=ssh://onnv.sfbay.sun.com//export/onnv-gate/usr/closed
183onnv-closed-clone=ssh://onnv.sfbay.sun.com//export/onnv-clone/usr/closed
184
185EOF
186else
187	cat <<EOF >> $HGRC
188onnv-gate=ssh://anon@hg.opensolaris.org//hg/onnv/onnv-gate
189
190EOF
191fi
192
193cat <<EOF >> $HGRC
194[merge-tools]
195filemerge.gui=True
196filemerge.args=-a \$base \$local \$other \$output
197filemerge.priority=1
198filemerge.premerge=False
199
200meld.gui=True
201meld.priority=0
202meld.premerge=False
203
204gpyfm.gui=True
205gpyfm.priority=0
206gpyfm.premerge=False
207
208[ui]
209username=$username
210style=$style_path
211EOF
212
213if [[ -n $merge_path ]]; then
214	echo "merge=$merge_path" >> $HGRC
215fi
216
217echo "Please check $HGRC and verify everything looks correct"
218