1 #!/bin/ksh -p
   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 # Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24 # Use is subject to license terms.
  25 #
  26 # ident "%Z%%M% %I%     %E% SMI"
  27 #
  28 #       This script sets up the environment variables for a SunOS
  29 #       codemgr workspace and spawns a shell with the environment
  30 #       setup.  
  31 #
  32 #       The following Environment variables are set:
  33 #               CODEMGR_WS
  34 #               ONBLD_DIR
  35 #               SRC
  36 #               TSRC
  37 #               ROOT
  38 #               PARENT_ROOT
  39 #               MACH
  40 #               MAKEFLAGS
  41 #               ENVCPPFLAGS{1-4}
  42 #               ENVLDLIBS{1-3}
  43 #       
  44 #       The MAKEFLAGS environment variable is set to force make
  45 #       to read default make variables from the environment.    
  46 #
  47 #       Workspace names can be specified in two forms: pathname
  48 #       and hostname:pathname.  If the hostname:pathname form is used
  49 #       the script accesses the environment through the /net automounter
  50 #       map.
  51 #
  52 
  53 #
  54 # function to produce a pathname from a workspace name or subdirectory.
  55 # The workspace name can have hostname:pathname format.
  56 #
  57 
  58 fmtwsname()
  59 {
  60         awk -F: '$1 != $0 { print "/net/"$1$2 } \
  61                  $1 == $0 { print $0 }'
  62 }
  63 
  64 #
  65 # Return a valid proto area, if one exists.
  66 #
  67 check_proto()
  68 {
  69         if [[ -z $1 ]]; then
  70                 return
  71         fi
  72 
  73         if [ "$SCM_MODE" = "teamware" ]; then
  74                 # Check for problematic parent specification and adjust
  75                 proto=`echo $1|fmtwsname`
  76                 echo "${proto}/root_${MACH}"
  77         elif [ "$SCM_MODE" = "mercurial" ]; then
  78                 proto=$1
  79                 #
  80                 # If the proto is a local repository then we can use it
  81                 # to point to the parents proto area. Don't bother to
  82                 # check if it exists or not, we never did for Teamware,
  83                 # since it might appear later anyway.
  84                 #
  85                 if [ "${proto##ssh://}" == "$proto" -a \
  86                      "${proto##http://}" == "$proto" -a \
  87                      "${proto##https://}" == "$proto" ]; then
  88                         echo "${proto}/root_${MACH}"
  89                 fi
  90         fi
  91 }
  92 
  93 cleanup_env()
  94 {
  95         # keep the env. clean when returning
  96         unset setenv osbld_flag os_rev wsosdir protofile wsname ofs proto \
  97                 pwd parent PROTO1 PROTO2 PROTO3 tmpwsname
  98         return 0
  99 }
 100 
 101 if [ "$1" = "-e" ]; then
 102         setenv=true
 103         shift
 104 else
 105         setenv=false
 106 fi
 107 
 108 WHICH_SCM=$(dirname $(whence $0))/which_scm
 109 if [[ ! -x $WHICH_SCM ]]; then
 110         WHICH_SCM=which_scm
 111 fi
 112 
 113 #
 114 # No workspace/repository path was given, so try and detect one from our
 115 # current directory we're in
 116 #
 117 if [ $# -lt 1 ]; then
 118         if env CODEMGR_WS="" $WHICH_SCM | read SCM_MODE tmpwsname && \
 119             [[ $SCM_MODE != unknown ]]; then
 120                 echo "Defaulting to $SCM_MODE repository $tmpwsname"
 121         else
 122                 echo "usage: ws [-e] [workspace_name]" >&2
 123                 if $setenv; then
 124                         cleanup_env
 125                         return 1
 126                 else
 127                         exit 1
 128                 fi
 129         fi
 130 else
 131         #
 132         # A workspace/repository path was passed in, grab it and pop
 133         # it off the stack
 134         #
 135         tmpwsname=$1
 136         shift
 137 fi
 138 
 139 #
 140 #       This variable displays the nested activations of workspaces.
 141 #       This is done here to get the exact name the user entered.
 142 #
 143 WS_STACK="$tmpwsname $WS_STACK"; export WS_STACK
 144 
 145 #
 146 # Set the workspace name and unset tmpwsname (as we reuse it later)
 147 #
 148 wsname=`echo $tmpwsname|fmtwsname`
 149 unset tmpwsname
 150 
 151 #
 152 # Checking for CODEMGR_WSPATH
 153 #
 154 if [ "(" "${CODEMGR_WSPATH}x" != "x" ")" -a "(" ! -d $wsname ")" -a \
 155      "(" `expr "$wsname" : "\/"` = "0" ")" ] 
 156 then
 157         ofs=$IFS
 158         IFS=":  "
 159         for i in $CODEMGR_WSPATH 
 160         do
 161                 if [ -d ${i}/${wsname} ]; then
 162                         wsname=${i}/${wsname}
 163                         break
 164                 fi
 165         done
 166         IFS=$ofs
 167 fi
 168 
 169 #
 170 # to translate it to an absolute pathname.  We need an
 171 # absolute pathname in order to set CODEMGR_WS.
 172 #
 173 if [ `expr "$wsname" : "\/"` = "0" ] 
 174 then
 175         pwd=`pwd`
 176         wsname="$pwd/$wsname"
 177 fi
 178 
 179 #
 180 #       Check to see if this is a valid workspace
 181 #
 182 if [ ! -d $wsname ]; then
 183         echo "$wsname . . . no such directory" >&2
 184         if $setenv; then
 185                 cleanup_env
 186                 return 1
 187         else
 188                 exit 1
 189         fi
 190 fi
 191 
 192 #
 193 # This catches the case of a passed in workspace path
 194 # Check which type of SCM is in use by $wsname.
 195 #
 196 (cd $wsname && env CODEMGR_WS="" $WHICH_SCM) | read SCM_MODE tmpwsname
 197 if [[ $? != 0 || "$SCM_MODE" == unknown ]]; then
 198         echo "Error: Unable to detect a supported SCM repository in $wsname"
 199         if $setenv; then
 200                 cleanup_env
 201                 return 1
 202         else
 203                 exit 1
 204         fi
 205 fi
 206 
 207 wsname=$tmpwsname
 208 CODEMGR_WS=$wsname ; export CODEMGR_WS
 209 SRC=$wsname/usr/src; export SRC
 210 TSRC=$wsname/usr/ontest; export TSRC
 211 
 212 if [ "$SCM_MODE" = "teamware" -a -d ${wsname}/Codemgr_wsdata ]; then
 213         CM_DATA="Codemgr_wsdata"
 214         wsosdir=$CODEMGR_WS/$CM_DATA/sunos
 215         protofile=$wsosdir/protodefs
 216 elif [ "$SCM_MODE" = "mercurial" -a -d ${wsname}/.hg ]; then
 217         CM_DATA=".hg"
 218         wsosdir=$CODEMGR_WS/$CM_DATA
 219         protofile=$wsosdir/org.opensolaris.protodefs
 220 else
 221         echo "$wsname is not a supported workspace; type is $SCM_MODE" >&2
 222         if $setenv; then
 223                 cleanup_env
 224                 return 1
 225         else
 226                 exit 1
 227         fi
 228 fi
 229 
 230 MACH=`uname -p`
 231 
 232 if [ ! -f $protofile ]; then
 233         if [ ! -w $CODEMGR_WS/$CM_DATA ]; then
 234                 #
 235                 # The workspace doesn't have a protodefs file and I am
 236                 # unable to create one.  Tell user and use /tmp instead.
 237                 #
 238                 echo "Unable to create the proto defaults file ($protofile)."
 239 
 240                 # Just make one in /tmp
 241                 wsosdir=/tmp
 242                 protofile=$wsosdir/protodefs
 243         fi
 244 
 245         if [ ! -d $wsosdir ]; then
 246                 mkdir $wsosdir
 247         fi
 248 
 249         cat << PROTOFILE_EoF > $protofile
 250 #!/bin/sh
 251 #
 252 #       Set default proto areas for this workspace
 253 #       NOTE: This file was initially automatically generated.
 254 #
 255 #       Feel free to edit this file.  If this file is removed
 256 #       it will be rebuilt containing default values.
 257 #
 258 #       The variable CODEMGR_WS is available to this script.
 259 #
 260 #       PROTO1 is the first proto area searched and is typically set
 261 #       to a proto area associated with the workspace.  The ROOT
 262 #       environment variable is set to the same as PROTO1.  If you
 263 #       will be doing make installs this proto area needs to be writable.
 264 #
 265 #       PROTO2 and PROTO3 are set to proto areas to search before the
 266 #       search proceeds to the local machine or the proto area specified by
 267 #       TERMPROTO.
 268 #
 269 #       TERMPROTO (if specified) is the last place searched.  If
 270 #       TERMPROTO is not specified the search will end at the local
 271 #       machine.
 272 #
 273 
 274 PROTO1=\$CODEMGR_WS/proto
 275 PROTOFILE_EoF
 276         
 277         if [ "$SCM_MODE" = "teamware" ]; then
 278                 cat << PROTOFILE_EoF >> $protofile
 279 if [ -f "\$CODEMGR_WS/Codemgr_wsdata/parent" ]; then
 280    #
 281    # If this workspace has an codemgr parent then set PROTO2 to
 282    # point to the parents proto space.
 283    #
 284    parent=\`workspace parent \$CODEMGR_WS\`
 285    if [[ -n \$parent ]]; then
 286            PROTO2=\$parent/proto
 287    fi
 288 fi
 289 PROTOFILE_EoF
 290         elif [ "$SCM_MODE" = "mercurial" ]; then
 291                 cat << PROTOFILE_EoF >> $protofile
 292 parent=\`(cd \$CODEMGR_WS && hg path default 2>/dev/null)\`
 293 if [[ \$? -eq 0 && -n \$parent ]]; then
 294    [[ -n \$(check_proto \$parent/proto) ]] && PROTO2=\$parent/proto
 295 fi
 296 PROTOFILE_EoF
 297         fi
 298 fi
 299 
 300 . $protofile
 301 
 302 # This means you don't have to type make -e all of the time
 303 
 304 MAKEFLAGS=e; export MAKEFLAGS
 305 
 306 #
 307 #       Set up the environment variables
 308 #
 309 ROOT=/proto/root_${MACH}        # default
 310 
 311 ENVCPPFLAGS1=
 312 ENVCPPFLAGS2=
 313 ENVCPPFLAGS3=
 314 ENVCPPFLAGS4=
 315 ENVLDLIBS1=
 316 ENVLDLIBS2=
 317 ENVLDLIBS3=
 318 
 319 PROTO1=`check_proto $PROTO1`
 320 if [[ -n "$PROTO1" ]]; then     # first proto area specifed
 321         ROOT=$PROTO1
 322         ENVCPPFLAGS1=-I$ROOT/usr/include
 323         export ENVCPPFLAGS1
 324         ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
 325         export ENVLDLIBS1
 326 
 327         PROTO2=`check_proto $PROTO2`
 328         if [[ -n "$PROTO2" ]]; then     # second proto area specifed
 329                 ENVCPPFLAGS2=-I$PROTO2/usr/include
 330                 export ENVCPPFLAGS2
 331                 ENVLDLIBS2="-L$PROTO2/lib -L$PROTO2/usr/lib"
 332                 export ENVLDLIBS2
 333 
 334                 PROTO3=`check_proto $PROTO3`
 335                 if [[ -n "$PROTO3" ]]; then     # third proto area specifed
 336                         ENVCPPFLAGS3=-I$PROTO3/usr/include
 337                         export ENVCPPFLAGS3
 338                         ENVLDLIBS3="-L$PROTO3/lib -L$PROTO3/usr/lib"
 339                         export ENVLDLIBS3
 340                 fi
 341         fi
 342 fi
 343 
 344 export ROOT
 345 
 346 if [[ -n "$TERMPROTO" ]]; then  # fallback area specifed
 347         TERMPROTO=`check_proto $TERMPROTO`
 348         ENVCPPFLAGS4="-Y I,$TERMPROTO/usr/include"
 349         export ENVCPPFLAGS4
 350         ENVLDLIBS3="$ENVLDLIBS3 -Y P,$TERMPROTO/lib:$TERMPROTO/usr/lib"
 351         export ENVLDLIBS3
 352 fi
 353 
 354 osbld_flag=0
 355 
 356 if [[ -z "$ONBLD_DIR" ]]; then
 357         ONBLD_DIR=$(dirname $(whence $0))
 358 fi
 359 
 360 if ! echo ":$PATH:" | grep ":${ONBLD_DIR}:" > /dev/null; then
 361         PATH="${ONBLD_DIR}:${ONBLD_DIR}/${MACH}:${PATH}"
 362         osbld_flag=1
 363 fi
 364 
 365 export PATH
 366 
 367 if [[ -n "$PROTO2" ]]; then
 368    # This should point to the parent's proto
 369    PARENT_ROOT=$PROTO2
 370    export PARENT_ROOT
 371 else
 372    # Clear it in case it's already in the env.
 373    PARENT_ROOT=
 374 fi
 375 export ONBLD_DIR
 376 export MACH
 377 
 378 os_rev=`uname -r`
 379 os_name=`uname -s`
 380 
 381 if [[ $os_name != "SunOS" || `expr $os_rev : "5\."` != "2" ]]; then
 382    #
 383    # This is not a SunOS 5.x machine - something is wrong
 384    #
 385    echo "***WARNING: this script is meant to be run on SunOS 5.x."
 386    echo "            This machine appears to be running: $os_name $os_rev"
 387 fi
 388 
 389 echo ""
 390 echo "Workspace                    : $wsname"
 391 if [ -n "$parent" ]; then
 392    echo "Workspace Parent             : $parent"
 393 fi
 394 echo "Proto area (\$ROOT)           : $ROOT"
 395 if [ -n "$PARENT_ROOT" ]; then
 396    echo "Parent proto area (\$PARENT_ROOT) : $PARENT_ROOT"
 397 fi
 398 echo "Root of source (\$SRC)        : $SRC"
 399 echo "Root of test source (\$TSRC)  : $TSRC"
 400 if [ $osbld_flag = "1" ]; then
 401    echo "Prepended to PATH            : $ONBLD_DIR"
 402 fi
 403 echo "Current directory (\$PWD)     : $wsname"
 404 echo ""
 405 
 406 cd $wsname
 407 
 408 if $setenv; then
 409         cleanup_env
 410 else
 411         exec ${SHELL:-sh} "$@"
 412 fi