#!/bin/sh
#
# $Id: //lvdist/dev/plat/unix/cintools/lvmkmf#3 $
#
#     Copyright 1989-1995 by National Instruments
#    All Rights Reserved.
#
# Creates Makefiles for building CINs and external subroutines.
#
# Codes used in Makefile.tmpl:
#   B=Solaris 1 V=Solaris 2 H=HP-UX L=Linux P=PowerMAX D=Digital Unix
#   C=regular CIN  O=no specific CIN specified when building makefile
#   Q=End of Makefile when no specific CIN specified
#
MAKEFILE=Makefile
TYPE=CIN

Usage ()
{
echo "Usage: lvmkmf [-o Makefile] [-t CIN] LVSBName";
exit 1;
}
LVDIR=LIBDIR	# RHS is replaced by hard-coded default path at installation
if test ! -f $LVDIR/cintools/Makefile.tmpl; then
   # Figure out path ourselves
   case $0 in
     */*) LVDIR=`dirname $0`
          ;;
       *) # Search path
          IFS=':'
          for i in $PATH
            do
 	      test -d $i -a -f $i/lvmkmf && LVDIR=$i
 	      test -d $i -a -f $i/cintools/lvmkmf && LVDIR=$i/cintools
            done
          IFS=
          ;;
   esac
   if test ! -f $LVDIR/Makefile.tmpl; then
      while test -h $LVDIR/lvmkmf; # follow symbolic link
         do
            TDIR=`ls -l $LVDIR/lvmkmf | sed 's,^.* -> ,,'`
            TDIR=`dirname $TDIR`
            case $TDIR in
               /*) LVDIR=$TDIR ;;
                *) LVDIR=$LVDIR/$TDIR ;;
            esac
         done
   fi
   LVDIR=`(cd $LVDIR; pwd)`
   LVDIR=`dirname $LVDIR`
   if test ! -d $LVDIR/cintools; then
      echo Unable to find cintools directory.
      exit 1
   fi
fi

for arg in $*; do
  case $1 in
    -o)
      MAKEFILE=$2
      shift
      ;;
    -t)
      TYPE=$2
      shift
      ;;
    -ext)
	  echo "External subroutines are no no longer supported.  Consider using a"
	  echo "shared library (External Function) instead."
	  exit 1
      shift
      ;;
    "")
      break
      ;;
    *)
      LVSBNAME=$1
      ;;
  esac
  shift
done
if (test -z "$LVSBNAME" ) then
  Usage;
fi
if (test -f /vmunix -o -f /vmlinux -o -f /vmlinuz ) then
  if (test -l "$TYPE" -gt 4) then
    echo "Type must be 4 or fewer characters."
    Usage;
  fi
else
  if (test `echo "$TYPE" | wc -c` -gt 5) then
    echo "Type must be 4 or fewer characters."
    Usage;
  fi
fi
if (test -f $MAKEFILE) then
  echo Backing up $MAKEFILE to $MAKEFILE.bak.
  mv $MAKEFILE $MAKEFILE.bak
fi
FILETYPE=`echo $TYPE | /usr/bin/tr "[A-Z]" "[a-z]"`
echo Copying Makefile.tmpl to $MAKEFILE with substitutions...
rm -f /tmp/lvmk$$.sed
echo "s+LVSBNAME+$LVSBNAME+g" > /tmp/lvmk$$.sed
echo "s+TYPENAME+$TYPE+g" >> /tmp/lvmk$$.sed
echo "s+FILETYPE+$FILETYPE+g" >> /tmp/lvmk$$.sed
if (test -f /stand/unix) then
  echo "  This appears to be a Concurrent PowerMAX machine."
  echo "/^#V\ /D" >> /tmp/lvmk$$.sed
  echo "/^#B\ /D" >> /tmp/lvmk$$.sed
  echo "/^#L\ /D" >> /tmp/lvmk$$.sed
  echo "/^#H\ /D" >> /tmp/lvmk$$.sed
  echo "/^#D\ /D" >> /tmp/lvmk$$.sed
  echo "s/^#P\ //g" >> /tmp/lvmk$$.sed
elif (test -f /stand/vmunix) then
  echo "  This appears to be an HP-UX 10 machine."
  echo "/^#P\ /D" >> /tmp/lvmk$$.sed
  echo "/^#V\ /D" >> /tmp/lvmk$$.sed
  echo "/^#B\ /D" >> /tmp/lvmk$$.sed
  echo "/^#L\ /D" >> /tmp/lvmk$$.sed
  echo "/^#D\ /D" >> /tmp/lvmk$$.sed
  echo "s/^#H\ //g" >> /tmp/lvmk$$.sed
elif (test -f /hp-ux) then
  echo "  This appears to be an HP-UX 9 machine."
  echo "/^#P\ /D" >> /tmp/lvmk$$.sed
  echo "/^#V\ /D" >> /tmp/lvmk$$.sed
  echo "/^#B\ /D" >> /tmp/lvmk$$.sed
  echo "/^#L\ /D" >> /tmp/lvmk$$.sed
  echo "/^#D\ /D" >> /tmp/lvmk$$.sed
  echo "s/^#H\ //g" >> /tmp/lvmk$$.sed
elif (test -f /osf_boot) then
  echo "  This appears to be a Digital Unix/OSF 1 machine."
  echo "/^#P\ /D" >> /tmp/lvmk$$.sed
  echo "/^#V\ /D" >> /tmp/lvmk$$.sed
  echo "/^#H\ /D" >> /tmp/lvmk$$.sed
  echo "/^#L\ /D" >> /tmp/lvmk$$.sed
  echo "/^#B\ /D" >> /tmp/lvmk$$.sed
  echo "s/^#D\ //g" >> /tmp/lvmk$$.sed
elif (test -f /vmunix) then
  echo "  This appears to be a Solaris 1 machine."
  echo "/^#P\ /D" >> /tmp/lvmk$$.sed
  echo "/^#V\ /D" >> /tmp/lvmk$$.sed
  echo "/^#H\ /D" >> /tmp/lvmk$$.sed
  echo "/^#L\ /D" >> /tmp/lvmk$$.sed
  echo "/^#D\ /D" >> /tmp/lvmk$$.sed
  echo "s/^#B\ //g" >> /tmp/lvmk$$.sed
elif [ "`uname`" = Linux ]; then
  echo "  This appears to be a Linux machine."
  echo "/^#P\ /D" >> /tmp/lvmk$$.sed
  echo "/^#B\ /D" >> /tmp/lvmk$$.sed
  echo "/^#H\ /D" >> /tmp/lvmk$$.sed
  echo "/^#V\ /D" >> /tmp/lvmk$$.sed
  echo "/^#D\ /D" >> /tmp/lvmk$$.sed
  echo "s/^#L\ //g" >> /tmp/lvmk$$.sed
elif (test -d /kernel) then
  echo "  This appears to be a Solaris 2 machine."
  echo "/^#P\ /D" >> /tmp/lvmk$$.sed
  echo "/^#B\ /D" >> /tmp/lvmk$$.sed
  echo "/^#H\ /D" >> /tmp/lvmk$$.sed
  echo "/^#L\ /D" >> /tmp/lvmk$$.sed
  echo "/^#D\ /D" >> /tmp/lvmk$$.sed
  echo "s/^#V\ //g" >> /tmp/lvmk$$.sed
else
  echo "  Unknown machine type - using defaults (gcc)."
  echo "/^#P\ /D" >> /tmp/lvmk$$.sed
  echo "/^#B\ /D" >> /tmp/lvmk$$.sed
  echo "/^#H\ /D" >> /tmp/lvmk$$.sed
  echo "/^#V\ /D" >> /tmp/lvmk$$.sed
  echo "/^#D\ /D" >> /tmp/lvmk$$.sed
  echo "s/^#L\ //g" >> /tmp/lvmk$$.sed
fi
echo "/^##/D" >> /tmp/lvmk$$.sed
if (test "$TYPE" = "CIN") then
  echo "s/^#C\ //g" >> /tmp/lvmk$$.sed
else
  echo "/^#C\ /D" >> /tmp/lvmk$$.sed
fi
if (test -z "$LVSBNAME") then
  echo "s/^#0\ //g" >> /tmp/lvmk$$.sed
  echo "/^#Q\ /,\$d" >> /tmp/lvmk$$.sed
else
  echo "/^#0\ /d" >> /tmp/lvmk$$.sed
  echo "/^#Q\ /d" >> /tmp/lvmk$$.sed
fi
echo "s,LVDIR,$LVDIR,g" >> /tmp/lvmk$$.sed
sed -f /tmp/lvmk$$.sed $LVDIR/cintools/Makefile.tmpl > $MAKEFILE
rm -f /tmp/lvmk$$.sed
echo Done.
exit 0
