#! /bin/sh
#
# Configure shell script for mailsplit
#
# @(#)Configure    2.6    88/08/20       M F Wyle
#
# "Yes, you may rip this off to use in other distribution packages."
#      -Larry Wall-
#
# Thanks Larry I did!  -Mitch
#
# If your sh doesn't like these # comments, nuke them.  You must
# also get rid of the # comments in mailsplit and joinr.
#
# $Id: Configure,v 1.5 90/02/07 12:05:00 yamanaka Rel $
#
package=mailsplit

: sanity checks
PATH='.:/bin:/usr/bin:/usr/local/bin:/usr/ucb:/usr/local:/usr/lbin:/etc:/usr/new:/usr/new/bin:/usr/nbin:/usr/ucb/bin:/usr/old'
export PATH || (echo "OOPS, this isn't sh.  Desperation time.  I will feed
myself to sh."; sh $0; kill $$)

if test ! -t 0; then
    echo "Say 'sh Configure', not 'sh <Configure'"
    exit 1
fi

(alias) >/dev/null 2>&1 && \
    echo "(I see you are using the Korn shell.  Some ksh's blow up on
Configure," && \
    echo "especially on exotic machines.  If yours does, try the Bourne shell
instead.)"

echo "creating a work directory named UU"
if test ! -d UU; then
  mkdir UU
fi
cd UU

: We must find out about Eunice early
eunicefix=':'
if test -f /etc/unixtovms; then
    eunicefix=/etc/unixtovms
fi
if test -f /etc/unixtovms.exe; then
    eunicefix=/etc/unixtovms.exe
fi

: some greps do not return status, grrr.
echo "grimblepritz" >grimble
if grep blurfldyick grimble >/dev/null 2>&1 ; then
    contains=contains
elif grep grimblepritz grimble >/dev/null 2>&1 ; then
    contains=grep
else
    contains=contains
fi
rm -f grimble
: the following should work in any shell
case "$contains" in
contains*)
    echo " "
    echo "AGH!  Grep doesn't return a status.  Attempting remedial action."
    cat >contains <<'EOSS'
grep "$1" "$2" >.greptmp && cat .greptmp && test -s .greptmp
EOSS
chmod +x contains
esac

: first determine how to suppress newline on echo command
echo "Checking echo to see how to suppress newlines..."
(echo "hi there\c" ; echo " ") >.echotmp
if $contains c .echotmp >/dev/null 2>&1 ; then
    echo "...using -n."
    n='-n'
    c=''
else
    cat <<'EOM'
...using \c
EOM
    n=''
    c='\c'
fi
echo $n "Type carriage return to continue.  Your cursor should be here-->$c"
read ans
rm -f .echotmp

: now set up to do reads with possible shell escape and default assignment
cat <<EOSC >myread
case "\$fastread" in
yes) ans=''; echo " " ;;
*) ans='!';;
esac
while expr "X\$ans" : "X!" >/dev/null; do
    read ans
    case "\$ans" in
    !)
        sh
        echo " "
        echo $n "\$rp $c"
        ;;
    !*)
        set \`expr "X\$ans" : "X!\(.*\)\$"\`
        sh -c "\$*"
        echo " "
        echo $n "\$rp $c"
        ;;
    esac
done
rp='Your answer:'
case "\$ans" in
'') ans="\$dflt";;
esac
EOSC
: set up shell script to do ~ expansion
cat >filexp <<EOSS
$startsh
: expand filename
case "\$1" in
 ~/*|~)
    echo \$1 | $sed "s|~|\${HOME-\$LOGDIR}|"
    ;;
 ~*)
    if $test -f /bin/csh; then
        /bin/csh -f -c "glob \$1"
        echo ""
    else
        name=\`$expr x\$1 : '..\([^/]*\)'\`
        dir=\`$sed -n -e "/^\${name}:/{s/^[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\).*"'\$'"/\1/" -e p -e q -e '}' </etc/passwd\`
        if $test ! -d "\$dir"; then
            me=\`basename \$0\`
            echo "\$me: can't locate home directory for: \$name" >&2
            exit 1
        fi
        case "\$1" in
        */*)
            echo \$dir/\`$expr x\$1 : '..[^/]*/\(.*\)'\`
            ;;
        *)
            echo \$dir
            ;;
        esac
    fi
    ;;
*)
    echo \$1
    ;;
esac
EOSS
chmod +x filexp
$eunicefix filexp



: general instructions
cat <<EOH
 
This installation shell script will examine your system and ask you
questions to determine how the $package package should be installed.
If you get stuck on a question, you may use a ! shell escape to start a
subshell or execute a command.  Many of the questions will have default
answers in square brackets--typing carriage return will give you the
default.

EOH
rp="[Type carriage return to continue]"
echo $n "$rp $c"
. myread
 

: find out where common programs are
echo " "
echo "Locating mailsplit component utility programs..."
cat <<EOSC >loc
$startsh
case \$# in
0) exit 1;;
esac
thing=\$1
shift
dflt=\$1
shift
for dir in \$*; do
    case "\$thing" in
    .)
        if test -d \$dir/\$thing; then
            echo \$dir
            exit 0
        fi
        ;;
    *)
        if test -f \$dir/\$thing; then
            echo \$dir/\$thing
            exit 0
        elif test -f \$dir/\$thing.exe; then
            : on Eunice apparently
            echo \$dir/\$thing
            exit 0
        fi
        ;;
    esac
done
echo \$dflt
exit 1
EOSC
chmod +x loc
$eunicefix loc
loclist="
basename
cat
tar
rm
sed
mv
awk
head
pwd
split
compress
Mail
touch
uuencode
uudecode
uncompress
"
pth='. /bin /usr/bin /usr/local/bin /usr/ucb /usr/local /usr/lbin /etc /usr/new /usr/new/bin /usr/nbin /usr/ucb/bin /usr/old'

for file in $loclist; do
    xxx=`loc $file $file $pth`
    eval $file=$xxx
    eval _$file=$xxx
    case "$xxx" in
    /*)
        echo $file is in $xxx.
        ;;
    *)
        echo "I don't know where $file is.  I hope it's in everyone's PATH."
        ;;
    esac
done
case "$test" in
test)
    echo "Hopefully test is built into your sh."
    ;;
/bin/test)
    if sh -c "PATH= test true" >/dev/null 2>&1; then
        echo "Using the test built into your sh."
        test=test
    fi
    ;;
*)
    test=test
    ;;
esac
case "$echo" in
echo)
    echo "Hopefully echo is built into your sh."
    ;;
/bin/echo)
    echo " "
    echo "Checking compatibility between /bin/echo and builtin echo (if
any)..."    $echo $n "hi there$c" >foo1
    echo $n "hi there$c" >foo2
    if cmp foo1 foo2 >/dev/null 2>&1; then
        echo "They are compatible.  In fact, they may be identical."
    else
        case "$n" in
        '-n') n='' c='\c' ans='\c' ;;
        *) n='-n' c='' ans='-n' ;;
        esac
        cat <<FOO
They are not compatible!  You are probably running ksh on a non-USG system.
I'll have to use /bin/echo instead of the builtin, since Bourne shell doesn't
have echo built in and we may have to run some Bourne shell scripts.  That
means I'll have to use $ans to suppress newlines now.  Life is ridiculous.

FOO
        rp="Your cursor should be here-->"
        $echo $n "$rp$c"
        . myread
    fi
    $rm -f foo1 foo2
    ;;
*)
    : cross your fingers
    echo=echo
    ;;
esac
rmlist="$rmlist loc"

: see if sh knows # comments
echo " "
echo "Checking your sh to see if it knows about # comments..."
if sh -c '#' >/dev/null 2>&1 ; then
    echo "Your sh handles # comments correctly."
    shsharp=true
    spitshell=cat
    echo " "
    echo "Okay, let's see if #! works on this system..."
    echo "#!/bin/echo hi" > try
    $eunicefix try
    chmod +x try
    try > today
    if $contains hi today >/dev/null 2>&1; then
        echo "It does."
        sharpbang='#!'
    else
        echo "#! /bin/echo hi" > try
        $eunicefix try
        chmod +x try
        try > today
        if test -s today; then
            echo "It does."
            sharpbang='#! '
        else
            echo "It doesn't."
            sharpbang=': use '
        fi
    fi
else
    echo "Your sh doesn't grok # comments--I will strip them later on."
    shsharp=false
    echo "exec grep -v '^#'" >spitshell
    chmod +x spitshell
    $eunicefix spitshell
    spitshell=`pwd`/spitshell
    echo "I presume that if # doesn't work, #! won't work either!"
    sharpbang=': use '
fi


: figure out how to guarantee sh startup
echo " "
echo "Checking out how to guarantee sh startup..."
startsh=$sharpbang'/bin/sh'
echo "Let's see if '$startsh' works..."
cat >try <<EOSS
$startsh
set abc
test "$?abc" != 1
EOSS

chmod +x try
$eunicefix try
if try; then
    echo "Yup, it does."
else
    echo "Nope.  You may have to fix up the shell scripts to make sure sh runs
them."
fi
rm -f try today

: determine where manual pages go
case "$mansrc" in
'')
    dflt=`loc . /usr/man/man1 /usr/man/mann /usr/man/manl /usr/man/local/man1 /usr/man/u_man/man1 /usr/man/man1`
    ;;
*)  dflt="$mansrc"
    ;;
esac
cont=true
while $test "$cont" ; do
    echo " "
    rp="Where do the manual pages (source) go? [$dflt]"
    $echo $n "$rp $c"
    . myread
    mansrc=`filexp "$ans"`
    if test -d $mansrc; then
        cont=''
    else
        dflt=n
        rp="Directory $mansrc doesn't exist.  Use that name anyway? [$dflt]"
        $echo $n "$rp $c"
        . myread
        dflt=''
        case "$ans" in
        y*) cont='';;
        esac
    fi
done
case "$mansrc" in
*l)
    manext=l
    ;;
*n)
    manext=n
    ;;
*C)
    manext=C
    ;;
*)
    manext=1
    ;;
esac


: determine where public executables go
case "$bin" in
'')
    dflt=`loc . /bin /usr/local/bin /usr/lbin /usr/local /usr/bin`
    ;;
*)  dflt="$bin"
    ;;
esac
cont=true
while $test "$cont" ; do
    echo " "
    rp="Where do you want to put the public executables? [$dflt]"
    $echo $n "$rp $c"
    . myread
    bin="$ans"
    bin=`filexp $bin`
    if test -d $bin; then
        cont=''
    else
        dflt=n
        rp="Directory $bin doesn't exist.  Use that name anyway? [$dflt]"
        $echo $n "$rp $c"
        . myread
        dflt=''
        case "$ans" in
        y*) cont='';;
        esac
    fi
done
:
: Variables and tuning parameters set here:
:
echo " "
dflt="/tmp"
rp="In which directory should temporary files be created? [$dflt]"
echo $n "$rp $c"
. myread
TD="$ans"

:
: If uuencode not found, try btoa
:
case $uuencode in
  uuencode)
    echo " "
    echo "Hmmm...  I couldn't find uuencode anywhere; I'll look for btoa."
    file="btoa"
    xxx=`loc $file $file $pth`
    eval $file=$xxx
    eval _$file=$xxx
    case "$xxx" in
      /*) echo "$file is in $xxx  I'll use $xxx instead of uuencode."
          uuencode=$xxx ;;
      *)  echo "btoa not found either."
          echo " "
          dflt="uuencode"
	  rp="Neither uuencode nor btoa found; which shall I use [$dflt]?"
          echo $n "$rp $c"
	  . myread
          uuencode="$ans" ;;
    esac ;;
esac

UUOPT="" 
case $uudecode in
  uudecode)
    echo " "
    echo "Hmmm...  I couldn't find uudecode anywhere; I'll look for btoa -a."
    file="btoa"
    xxx=`loc $file $file $pth`
    eval $file=$xxx
    eval _$file=$xxx
    case "$xxx" in
      /*) echo "$file is in $xxx  I'll use $xxx instead of uudecode."
          uuencode=$xxx 
	  UUOPT="-a" ;;
      *)  echo "btoa not found either."
          echo " "
          dflt="uudecode"
	  rp="Neither uudecode nor btoa found; which shall I use [$dflt]?"
          echo $n "$rp $c"
	  . myread
          uudecode="$ans" ;;
    esac ;;
esac
:
: If compress not found, try compact
:
compact="false"
case $compress in
  compress)
    echo " "
    echo "Hmmm...  I couldn't find compress anywhere; I'll look for compact."
    file="compact"
    xxx=`loc $file $file $pth`
    eval $file=$xxx
    eval _$file=$xxx
    case "$xxx" in
      /*) echo "$file is in $xxx  I'll use $xxx instead of compress."
          compress=$xxx
          compact="true"  ;;
      *)  echo "compact not found either."
          echo " "
	  file="pack"
	  xxx=`lock $file $file $pth`
	  eval $file=$xxx
	  eval _$file=$xxx
	  case "$xxx" in
	    /*) echo "$file is in $xxx  I'll use $xxx instead of compress."
                compress=$xxx
                compact="others"  ;;
            *)  echo "pack not found either."
                echo " "
	        dflt="compress"
	rp="No compress, no compact and no pack found; which shall I use [$dflt]?"
        	echo $n "$rp $c"
	        . myread
        	compact="false"
	        compress="$ans" ;;
	  esac;;
    esac ;;
esac
:
: Likewise for uncompress and uncompact:
:
case $uncompress in
  uncompress)
    echo " "
    echo "I couldn't find uncompress anywhere; I'll look for uncompact."
    file="uncompact"
    xxx=`loc $file $file $pth`
    eval $file=$xxx
    eval _$file=$xxx
    case "$xxx" in
      /*) echo "$file is in $xxx  I'll use $xxx instead of uncompress."
          uncompress=$xxx ;;
      *)  echo "uncompact not found either."
          echo " "
	  file="unpack"
	  xxx=`loc $file $file $pth`
	  eval $file=$xxx
	  eval _$file=$xxx
	  case "$xxx" in
	    /*) echo "$file is in $xxx  I'll use $xxx instead of uncompress."
        	uncompress=$xxx ;;
	    *)  echo "unpack not found either."
        	echo " "
	        dflt="uncompress"
rp="No uncompress, no uncompact and no unpack found; which should I use [$dflt]?"
	        echo $n "$rp $c"
        	. myread
	        uncompress="$ans" ;;
	 esac ;;
    esac ;;
esac

:
: Set default bits for compress, file name extension, and command line flags
:
case $compact in
  false)
    echo " "
    dflt="16"
    rp="How many bits should $compress use (ok to guess) [$dflt]"
    echo $n "$rp $c"
    . myread
    CB="$ans"
    CF="\"-b $CB\""
    CE="Z" ;;
  true)
    CE="C"
    CF="" ;;
  others)
    CE="z"
    CF="" ;;
esac
:
: Check if Mail was found, if not use mail:
:
MS="Subj"
case $Mail in
  Mail)
    MS="Mist"
    echo " "
    echo "I couldn't find Mail anywhere; I'll try to use mailx..."
    file="mailx"
    xxx=`loc $file $file $pth`
    eval $file=$xxx
    eval _$file=$xxx
    case "$xxx" in
      /*) echo "$file is in $xxx  I'll use $xxx."
          Mail=$xxx ;;
       *) echo " "
	  echo "I couldn't find mailx anywhere; I'll try to use mail..."
	  file="mail"
	  xxx=`loc $file $file $pth`
	  eval $file=$xxx
	  eval _$file=$xxx
	  case "$xxx" in
	    /*) echo "$file is in $xxx  I'll use $xxx."
        	Mail=$xxx ;;
	     *) echo "mail not found either."
        	echo " "
	        dflt="mail"
		rp="Neither mail nor Mail was found; which should I use [$dflt]?"
	        echo $n "$fp $c"
        	. myread
	        Mail="$ans" ;;
	 esac ;;
     esac ;;
esac

# Strip dirnames here for compatibilities. 
# 	Added by yamanaka@iias.fujitsu.co.jp

for file in $loclist; do
    xxx=`loc $file $file $pth`
    eval $file=`basename $xxx`
done


echo " "
dflt="5"
rp="What should the default debugging level be (ok to guess) [$dflt]"
echo $n "$rp $c" 
. myread 
DB="$ans"

dflt="0"

cat << FOO

Some mailers (a certain X.400 system with "MTA congestion problems"
will remain nameless) have trouble dealing with sudden, large bursts
of messages.  You might want to set the default delay to 5 or 10
seconds if your mailer has such problems.  Otherwise leave it at 0, the
default.

FOO

rp="What is the default delay (in seconds) between sending pieces [$dflt]"
echo $n "$rp $c"
. myread
DE="$ans"


echo " "
dflt=''
echo "If you didn't make any mistakes, then just type a carriage return here."
rp="If you did, type ^C and start over."
$echo $n "$rp $c"
. myread
echo " "

:
: here goes!!
:

cp ../joinr.SH ./
cp ../mailsplit.SH ./
. joinr.SH
. mailsplit.SH

echo " "
dflt="n"
rp="Would you like to install joinr and mailsplit in $bin now (y/n) "
echo $n "$rp $c"
. myread
case $ans in
  y|Y) cp ./joinr $bin/joinr
       cp ./mailsplit $bin/mailsplit ;;
esac

echo " " 
dflt="n"
rp="Would you like to install the man pages $mansrc now (y/n) "
echo $n "$rp $c" 
. myread 
case $ans in
  y|Y) cp "../joinr.1" "$mansrc/joinr.$manext"
       cp "../mailsplit.1" "$mansrc/mailsplit.$manext" ;; 
esac 

cd ..

echo " "
echo "saving joinr and mailsplit in `pwd`"
mv UU/joinr ./joinr
mv UU/mailsplit ./mailsplit

echo " "
echo "removing my work directory UU."
$rm -rf UU
