xref: /titanic_50/usr/src/tools/scripts/hgsetup.sh (revision 492887eea95d0c0640c3c97d4e8be8bd9e0b422a)
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 2008 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]
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
52	if -e isn't provided, and you are on SWAN, an LDAP query is done
53	if -n isn't provided, the entry from /etc/passwd is used
54
55	proxy should be in the form of hostname:port
56	if on-SWAN, $prog will lookup your email address.  this can be
57	overridden by using the -e flag.
58	"
59	exit 1
60}
61
62while getopts c:e:fm:n:p: opt; do
63	case "$opt" in
64	c) cdm_path=$OPTARG;;
65	e) email=$OPTARG;;
66	f) force=1;;
67	m) merge_path=$OPTARG;;
68	n) name=$OPTARG;;
69	p) proxy=$OPTARG;;
70	*) usage;;
71	esac
72done
73
74if [ -f $HGRC -a "$force" -eq 0 ]; then
75	echo "Error: You have an existing .hgrc in $HGRC"
76	echo "Please move it aside."
77	exit 1
78fi
79
80AWK="/usr/xpg4/bin/awk"
81SED="/usr/bin/sed"
82LDAPCLIENT="/usr/bin/ldapsearch"
83
84login=$(/usr/bin/id -un)
85
86#
87# Try and determine where Cadmium is installed.  In order of
88# preference, look in:
89#
90#   1. $(whence $0), on the assumption that you want the version
91#      of cadmium that best matches the hgsetup script you invoked
92#
93#   2. /opt/onbld, because local is generally better
94#
95#   3. /ws/onnv-tools/onbld, it's nfs and it might be slow, but it
96#      should resolve from most places on-SWAN
97#
98paths="$(dirname $(whence $0)) /opt/onbld /ws/onnv-tools/onbld"
99cdmbin="lib/python/onbld/hgext/cdm.py"
100
101for dir in $paths; do
102	if [[ -f "$dir/$cdmbin" && -z "$cdm_path" ]]; then
103		cdm_path="$dir/$cdmbin"
104		break
105	fi
106done
107
108if [[ -n $proxy ]]; then
109	proxyConfig="[http_proxy]
110host=$proxy
111"
112fi
113
114if getent hosts sunweb.central.sun.com >/dev/null; then
115	# on SWAN
116	echo "Detected SWAN connection"
117	ON_SWAN=1
118	ldapemail='preferredrfc822recipient'
119	ldapquery="uid=$login $ldapemail"
120	ldapcmd="$LDAPCLIENT -1 -h sun-ds -b dc=sun,dc=com $ldapquery"
121	if [[ -z "$email" ]]; then
122		echo "Looking up e-mail address in LDAP"
123		email=${email:=$($ldapcmd | $AWK /^$ldapemail:/'{print $2}')}
124	fi
125fi
126
127if [[ -z $email ]]; then
128	my_id=$(id -un)
129	my_checkhostname=$(check-hostname)
130	my_fqhn=${my_checkhostname##* }
131	email="$my_id@$my_fqhn"
132	echo "No e-mail address provided, defaulting to $email"
133fi
134
135if [[ -z "$name" ]]; then
136	name=${name:=$(getent passwd $login | awk -F: '{print $5}')}
137fi
138username="$name <$email>"
139
140echo "Configured the following:"
141if [[ -n $proxy ]]; then
142	echo "	proxy: $proxy"
143fi
144echo "	email: $email"
145echo "	username: $name"
146echo "	cadmium: $cdm_path"
147
148if [[ -z "$cdm_path" ]]; then
149	echo "Warning: you will need to edit your .hgrc file\n" \
150	     "to specify a path for cadmium."
151fi
152
153if [[ -n $merge_path ]]; then
154	echo "	merge: $merge_path"
155fi
156
157cat <<EOF >$HGRC
158$proxyConfig[extensions]
159hgext.cdm=$cdm_path
160
161[email]
162from=$email
163
164[paths]
165EOF
166
167if [[ -n $ON_SWAN ]]; then
168	cat <<EOF >> $HGRC
169onnv-gate=ssh://onnv.sfbay.sun.com//export/onnv-gate
170onnv-clone=ssh://onnv.sfbay.sun.com//export/onnv-clone
171onnv-closed=ssh://onnv.sfbay.sun.com//export/onnv-gate/usr/closed
172onnv-closed-clone=ssh://onnv.sfbay.sun.com//export/onnv-clone/usr/closed
173
174EOF
175else
176	cat <<EOF >> $HGRC
177onnv-gate=ssh://anon@hg.opensolaris.org//hg/onnv/onnv-gate
178
179EOF
180fi
181
182cat <<EOF >> $HGRC
183[merge-tools]
184filemerge.gui=True
185filemerge.args=-a \$base \$local \$other \$output
186filemerge.priority=1
187
188meld.gui=True
189meld.priority=0
190
191gpyfm.gui=True
192gpyfm.priority=0
193
194[ui]
195username=$username
196EOF
197
198if [[ -n $merge_path ]]; then
199	echo "merge=$merge_path" >> $HGRC
200fi
201
202echo "Please check $HGRC and verify everything looks correct"
203