#!/bin/sh
##############################################################################
##############################################################################
#
# mycc  version 1.04
# Copyright(c) 1995.  All Right Reserved.
#
##############################################################################
##############################################################################
#
# This program is written in Bourne Shell to compile pmfilter.c and its output
# file is PMfilter.
#
# History:
# 09/12/94 KMY -Created the program.
#
##############################################################################
##############################################################################

############################
# strings(): assign values to all messages to be displayed in the screen.
############################
# strings
strings() {
 comp_done="Compling has been done successfully."
 output_file="The output execution file is "   

 unix_type1="Choose the type of your UNIX system:" 
 unix_type2=" 1.SCO UNIX ver 3.x"
 unix_type3=" 2.Hewlett Packard HP_UX"
 unix_type4=" 3.SunOS 4.X"
 unix_type5=" 4.SunOS 5.X"
 unix_type6=" 5.AT&T UNIX_SV release 4"
 unix_type7=" 6.DEC 3000 OSF1"
 unix_type8=" 7.IBM AIX"
 unix_type9=" 8.UNIXWare ver 2.x "
 unix_type10=" 9.SCO UNIX Open Server 5.x"
 unix_type11=" 10.Others"
 choose_unix_type="Please enter a number:"

 type_quit="Type quit to exit the program."
 invalid_input="ERROR: Invalid Input!"

 comp_type1=" 1.BSD (Berkeley Software Distribution)"
 comp_type2=" 2.SVR4 (System V Release 4)"
 comp_type3=" 3.SVR3 (System V Release 3)"
 comp_type4=" 4.HP (Hewlett Packard)" 
 comp_type5=" 5.AIX (IBM Corp.)"
 comp_type6=" 6.DEC/OSF1"
 comp_type7=" 7.Others"
 
 comp_fail="ERROR: Error encountered during compiling."
 manu_comp="Please compile it manually according to User's Guide."

 type_source_dir="Type the dircetory containing $program_source and errormsg.h =>"
 not_exist="doesn't exist!"
 press_to_cont="                   [Press <Enter> to Continue]"
 err="ERROR:"
 cant_find="Can't find" 
 Sin="in"
 modifying="Editing ${program_source}, please wait..."
 compiling="Compiling ${program_source}, please wait..."

 quit="Quit the program."  
 cant_read="Can't read"
 cant_r_write="Can't read or write"
 cant_xr_write="Can't read, wirte or execute in"
 add_read_perm="Please add read permission to"
 add_r_write_perm="Please add read and write permission to"
 add_xr_write_perm="Please add read, write and execution permission to the directory:"
 
 Scopy="copy"
 Sto="to"
 for_safe="for safety."


}
############################
# check_unix_type(): determine which UNIX type the host is. 
############################
# check_unix_type
check_unix_type() {
  case "$ux_os" in
  scosysv )
           compile_type=SVR3
           ;;
  HP_UX | HP-UX  )
           ux_os="HP_UX"
           compile_type=HP
           ;;
  SunOS  )
           release=`uname -a | cut -f3 -d" " | cut -c1`
           if [ "$release" = 4 ]
           then
             ux_os=Sun4
             compile_type=BSD
           else
             ux_os=Sun5
             compile_type=SVR4
           fi
           ;;
  Sun4 )
                 compile_type=BSD 
                 ;;
  Sun5 )
                 compile_type=SVR4 
                 ;;
  UNIX_SV | SCO_SV )
           ux_os=UNIX_SV
           compile_type=SVR4
           ;;
  OSF1   )
           compile_type=OSF1
           ;;
  AIX   )
           compile_type=AIX
           ;;
  *       )
           ux_os=unknown
           compile_type=unknown
           ;;

  esac
}
#####################################
# ask_unix_type(): ask the unix type of the station 
#####################################
# ask_unix_type
 ask_unix_type() {
   while true
   do
     is_break=yes
     clear
     echo " "
     echo "$type_quit"
     echo " "
     echo "$unix_type1"
     echo "$unix_type2"
     echo "$unix_type3"
     echo "$unix_type4"
     echo "$unix_type5"
     echo "$unix_type6"
     echo "$unix_type7"
     echo "$unix_type8"
     echo "$unix_type9"
     echo "$unix_type10"
     echo "$unix_type11"
     echo "$choose_unix_type"
     read number
     is_quit "$number"
     case "$number" in
       1 )
              ux_os=scosysv
              ;;
       2 )
              ux_os=HP_UX
              ;;
       3 )
              ux_os=Sun4
              ;;
       4 )
              ux_os=Sun5
              ;;
       5 | 8 | 9 )
              ux_os=UNIX_SV
              ;;
       6 )
              ux_os=OSF1
              ;;
       7 )
              ux_os=AIX
              ;;
       10 )
              ux_os=unknown
              ;;
       * ) 
          is_break=no
          echo "$invalid_input"
          ;;
     esac
     if [ "$is_break" = "yes" ]
     then
       check_unix_type
       break
     else
       echo " "
       echo "$press_to_cont"
       read dumb
       is_quit "$dumb"
     fi
   done

}
#####################################
# ask_compile_type(): ask the unix type used in pmfilter.c
#####################################
# ask_compile_type
 ask_compile_type() {
  while true
  do
    is_break=yes
    clear
    echo " "
    echo "$type_quit"
    echo " "
    echo "$unix_type1"
    echo "$comp_type1"
    echo "$comp_type2"
    echo "$comp_type3"
    echo "$comp_type4"
    echo "$comp_type5"
    echo "$comp_type6"
    echo "$comp_type7"
    echo "$choose_unix_type"
    read  number 
    is_quit "$number"
    case "$number" in

     1 )
         compile_type=BSD 
          ;;
     2 )
         compile_type=SVR4 
          ;;
     3 )
         compile_type=SVR3 
          ;;
     4 )
         compile_type=HP 
          ;;
     5 )
         compile_type=AIX 
          ;;
     6 )
         compile_type=OSF1 
          ;;
     7 )
         compile_type=unknown 
          ;;
      * ) 
         is_break=no
         echo "$invalid_input"
          ;;
     
    esac
    if [ "$is_break" = "yes" ]
    then
       break
     else
       echo " "
       echo "$press_to_cont"
       read dumb
       is_quit "$dumb"
    fi
  done
}

#####################################
# cant_compile(): print the error message of compiling unseccessfully.
#####################################
# cant_compile
 cant_compile() {
    echo " "
    echo "$comp_fail"
    echo "$manu_comp"
    echo " "
}
#####################################
# process_comp(): compile the pmfilter.c 
#####################################
#process_comp
process_comp() {
  echo " "
  echo "$compiling"
  is_comp_ok=no
  cnt=1 
  while true
  do
    lib=`echo $comp_list | cut -f$cnt -d":"`
    cc -o $program_exec $program_source $lib 2> /dev/null 
    if [ $? = 0 ]
    then
       is_comp_ok=yes
       chmod +x $program_exec 
       break
    else 
      cc -o $program_exec $lib $program_source 2> /dev/null 
      if [ $? = 0 ]
      then
         is_comp_ok=yes
         chmod +x $program_exec 
         break
      fi
    fi
    cnt=`expr $cnt "+" 1`
    if [ $cnt -gt 3 ]
    then
       break
    fi
  done
}
#####################################
# create_comp_list
#####################################
#create_comp_list
create_comp_list() {
  case "$ux_os" in
    scosysv )
            comp_list=" -lsocket : -lsocket -lnsl : "
            ;;
    HP_UX | AIX | Sun4 | OSF1 )
            comp_list=" : -lsocket : -lsocket -lnsl "
             ;;
    UNIX_SV | Sun5  | SCO_SV )
            comp_list=" -lsocket -lnsl : -lsocket : "
             ;;
    * )
             case "$compile_type" in
               BSD | HP | AIX | OSF1 ) 
                   comp_list=" : -lsocket : -lsocket -lnsl "
                   ;;
               SVR3 )
                   comp_list=" -lsocket : -lsocket -lnsl: "
                   ;;
               SVR4 )
                   comp_list=" -lsocket -lnsl : -lsocket : "
                   ;;
               * ) 
                   comp_list=" : -lsocket : -lsocket -lnsl "
                   ;;
             esac
            ;;
  esac

}

#####################################
#clear_off(): clear off the junk left and then exit
#####################################
#clear_off
clear_off() {
   cd $curr_dir
   rm -f $temp
   rm -f $pm_out
   exit 1
}

#####################################
# slecet_ux_type(): select the unix type from pmfilter.c
#####################################
#select_ux_type
select_ux_type() {
  uxt="$1"
  val="$2"
  delimter=`cat $pm_source | grep -n "#define $uxt" | cut -d: -f1`
  delimter=`echo $delimter | cut -d" " -f1`
  if [ X"$delimter" = X ]
  then
    echo " "
    echo "$err $cant_find '#define $uxt ' $Sin $pm_source"
    clear_off
  else
    cat > $temp << EOF_EOF
${delimter}c
#define $uxt $val     
.
w
q
EOF_EOF

     cat $temp | ed $pm_source > /dev/null
fi
}
#####################################
#modify_pm(): modify the pmfilter.c to select the correct UNIX type
#####################################
#modify_pm
modify_pm() {

  echo " "
  echo "$Scopy $pm_source $Sto $pm_source_old $for_safe"
  echo " "
  cp $pm_source $pm_source_old
  echo "$modifying"  


  trap "" 1 2 3 15
 
  select_ux_type $compile_type 1

  ux_list="SVR3 SVR4 BSD HP AIX OSF1"
  for tmp_ux in $ux_list
  do
    if [ "$tmp_ux" != "$compile_type" ]
    then
      select_ux_type $tmp_ux 0
    fi
  done

  trap "clear_off" 1 2 3 15

}
#####################################
#check_rwx_perm(): check the read, write, execution permission of LPsource
#                  directory, pmfilter.c and errormsg.h
#####################################
#check_rwx_perm
check_rwx_perm() {

      if [ ! -r $source_dir -o ! -w $source_dir -o ! -x $source_dir ]
      then
          echo " "
          echo "$err $cant_xr_write $source_dir !"
          echo "$add_xr_write_perm"
          echo "$source_dir" 
          exit 1
      fi

      cd $source_dir
      source_dir=`pwd`
      cd $curr_dir
      pm_source="${source_dir}/$program_source"
      pm_source_old="${source_dir}/${program_source}.$$"
      err_source="${source_dir}/errormsg.h"
      pm_out="${source_dir}/$program_exec"
      temp="${source_dir}/temp"

      if [ ! -w $pm_source -o ! -r $pm_source ]
      then
          echo " "
          echo "$err $cant_r_write ${pm_source}!"
          echo "$add_r_write_perm ${pm_source}."
          exit 1
      fi

      if [  ! -r $err_source ]
      then
          echo " "
          echo "$err $cant_read ${err_source}!"
          echo "$add_read_perm ${err_source}."
          exit 1
      fi
}
#####################################
#ask_source_dir(): ask user to input the directory containing pmfilter.c and 
#                  errormsg.h.
#####################################
#ask_source_dir
ask_source_dir() {
  while true
  do
    is_break=yes
    clear
    echo " "
    echo "$type_quit"
    echo " "
    echo "$type_source_dir"
    read  source_dir 
    is_quit "$source_dir"
    if [ ! -d "$source_dir" ]
    then
         is_break=no
         echo " "
         echo "$err $source_dir $not_exist"
    else
      pm_source="${source_dir}/$program_source"
      err_source="${source_dir}/errormsg.h"
      if [ ! -f "$pm_source" ]
      then
         is_break=no
         echo " "
         echo "$err $pm_source $not_exist"
      else
        if [ ! -f "$err_source" ]
        then
          is_break=no
          echo " "
          echo "$err $err_source $not_exist"
        fi
      fi
    fi
     
    if [ "$is_break" = "yes" ]
    then
       break
     else
       echo " "
       echo "$press_to_cont"
       read dumb
       is_quit "$dumb"
    fi
  done
}
#######################################
#is_quit(): check if the input is "quit" or "QUIT".
#######################################
#is_quit
is_quit() {
  case "$1" in
    [Qq][Uu][Ii][Tt] )
                    echo "$quit"
                    clear_off
                    ;;
    * )
        ;;
  esac

}
##############################################################################
##############################################################################
#
# Main
#
##############################################################################
##############################################################################
# main
set -u
trap "clear_off" 1 2 3 15 

PATH="/bin:/usr/bin:/usr/ucb:/usr/etc:$PATH"
export PATH

curr_dir=`pwd`

program_exec="PSfilter"
program_source="psfilter.c"

strings

if [ $# = 2 ]
then
  ux_os="$1" 
  source_dir="$2"
else
  ux_os=`uname`
  source_dir="${curr_dir}/LPsource"
fi

pm_source="${source_dir}/$program_source"
pm_out="${source_dir}/$program_exec"
err_source="${source_dir}/errormsg.h"
temp="${source_dir}/temp"

if [ ! -f $pm_source -o ! -f $err_source ]
then
  source_dir=unknown
  ask_source_dir
fi
check_rwx_perm

check_unix_type

if [ "$ux_os" = "unknown" ]
then
  ask_unix_type
fi

if [ "$ux_os" = "unknown" ]
then
  ask_compile_type
fi

if [ "$compile_type" = "unknown" ]
then
  cant_compile
  clear_off
fi

comp_list=""
create_comp_list

modify_pm


cd $source_dir
is_comp_ok=no
process_comp
cd $curr_dir

if [ "$is_comp_ok" = "no" ]
then
  cant_compile
  clear_off
else
  echo " "
  echo "$comp_done"
  echo "$output_file $pm_out"
  echo " "
  rm -f $temp
  exit 0
fi

