#!/bin/sh
# ---------------------------------------
#   Copyright (c) 1997  Fumio Mizoguchi 
# ---------------------------------------

echo

 : ::::::::::::::::::: HELPER FUNCTIONS ::::::::::::::::::::
 : Find how to suppress newline after echo
 if ( echo "test line\c"; echo " ") | grep c >/dev/null 2>/dev/null ; then
    n='-n'
    c=''
 else
    n=''
    c='\c'
 fi

 pwd=`pwd`

 : Define some handy functions

ask_yes_or_no(){
    echo $n " $1 (yes or no) [$2] "$c
    while true ; do
       read ans 
        case $ans in
        yes | '' ) return 0;;
        no) return 1;;
        *) echo $n "Please input yes or no [$2] "$c;;
        esac
    done
}

ask_user_set(){
    echo $n " $1 "$c
    while true ; do
	read ask
        case $ask in
        '' ) echo $n " $1 "$c;;
        *) break;;
        esac
    done
}

ask_default(){
    echo $n " $1 [$2] "$c
    while true ; do
        read ask
        case $ask in
        '' | $2 ) return 0;;
        *) return 1;;
        esac
    done
}

: ::::::::::::::::::: ROBOT SETTINGS ::::::::::::::::::::

DEF=ROBOT_NUM
while true ; do
    if ask_default "What number of ROBOT?" "1"
        then robot_num=1; echo "$DEF = $robot_num" > config.tmp; break
        elif ( test $ask -eq 0 )
             then echo "\"0\" is NO!!"
        elif ask_yes_or_no "ROBOT is $ask" yes
              then robot_num=$ask
                   echo "$DEF = $robot_num" > config.tmp; break
              else echo;echo "SETTING AGAIN";echo;fi
done
echo



: ::::::::::::::::::: ROBOT SETTINGS ::::::::::::::::::::

check_conf(){
  ans=`awk 'BEGIN{
  true="true"}
  {
  if($1=="Machine"){
    if($3==machine) same="ok"
    else same="no"}
  if( $1 ~ /Port[12]/ && same=="ok" && $3==check) true="fault"
  if( $1=="TTY" && same=="ok" && $3==check) true="fault"
  }
  END{
  if(true=="true")print "ok"
  else print "no"}' machine=$1 check=$2 config.tmp`

  case $ans in
  ok ) return 0;;
  no ) return 1;;
  esac
}

robot_set(){
 while true ; do
    ask_user_set "What is machine name ,which control ROBOT$1"
        machine=$ask

    while true ; do
      ask_user_set "  Specify socket port number of Real ROBOT$1"
        if check_conf $machine $ask
          then real_port=$ask; break;
          else echo "Already used port number you specified!!"
        fi
    done

    while true ; do
      ask_user_set "  Specify tty of ROBOT$1"
        if check_conf $machine $ask
          then tty=$ask; break;
          else echo "Already used tty you specified!!"
        fi
    done

    if ask_yes_or_no "Please Input [$machine,$real_port,$tty] " yes
      then echo "Machine = $machine" >> config.tmp
	   echo "RealPort = $real_port" >> config.tmp;
           echo "TTY = $tty" >> config.tmp; echo; break;
      else echo "SETTING AGAIN"; fi
 done
}

java_set(){
 while true ; do

    while true ; do    
      ask_user_set "  Specify read socket port number of Java ROBOT$1"
	if check_conf $2 $ask
	  then read_port=$ask; break;
          else echo "Already used port number you specified!!"
        fi
    done

    while true ; do
      ask_user_set "  Specify write socket port number of Java ROBOT$1"
        if check_conf $2 $ask
          then write_port=$ask; break;
          else echo "Already used port number you specified!!"
        fi
    done

    if ask_yes_or_no "Please Input [Robot$1, $read_port,$write_port] " yes
      then echo "JavaMachine = $2" >> config.tmp
           echo "Port1 = $read_port" >> config.tmp;
           echo "Port2 = $write_port" >> config.tmp; echo; break;
      else echo "SETTING AGAIN"; fi
 done
}

i=1
while [ $i != `expr $robot_num + 1` ];do
  robot_set $i
  i=`expr $i + 1`
done


    ask_user_set "What is machine name, which work Vision System"
        vision_machine=$ask; 
    echo "VisionMachine = $vision_machine" >> config.tmp

    ask_user_set "What is machine name, which work Java System"
        java_machine=$ask; 
    echo "JavaVisionMachine = $java_machine" >> config.tmp

i=1
while [ $i != `expr $robot_num + 1` ];do
  java_set $i $java_machine
  i=`expr $i + 1`
done


echo
echo "Please specify the capacity of your robot"
echo

 while true ; do
  if ask_default "Permissible range of hand reaching(Min)?" "240"
    then min_take=240;
    else min_take=$ask;fi;
  if ask_default "Permissible range of hand reaching(Max)?" "400"
    then max_take=400;
    else max_take=$ask;fi;
  if ask_yes_or_no "Please Input [$min_take,$max_take] " yes
        then echo "MIN_TAKE = $min_take" >> config.tmp
             echo "MAX_TAKE = $max_take" >> config.tmp; echo; break;
        else echo "SETTING AGAIN"; fi
 done

 while true ; do
  if ask_default "Permissible range of box position(Min)?" "270"
    then min_in=270;
    else min_in=$ask;fi;
  if ask_default "Permissible range of box position(Max)?" "420"
    then max_in=420;
    else max_in=$ask;fi;
  if ask_yes_or_no "Please Input [$min_in,$max_in] " yes
        then echo "MIN_IN = $min_in" >> config.tmp
             echo "MAX_IN = $max_in" >> config.tmp; echo; break;
        else echo "SETTING AGAIN"; fi
 done

 while true ; do
  if ask_default "Permissible range of block transfer(Min)?" "390"
    then min_deli=390;
    else min_deli=$ask;fi;
  if ask_default "Permissible range of block transfer(Max)?" "550"
    then max_deli=550;
    else max_deli=$ask;fi;
  if ask_yes_or_no "Please Input [$min_deli,$max_deli] " yes
        then echo "MIN_DELI = $min_deli" >> config.tmp
             echo "MAX_DELI = $max_deli" >> config.tmp; echo; break;
        else echo "SETTING AGAIN"; fi
 done

: ::::::::::::::::::::::: MAKE FILE :::::::::::::::::::::::::::::::::::::: 

echo $n "SETTING NOW!!"$c

: ::::::::::::::::::::::: CALIB.KL1 FILE ::::::::::::::::::::::::::::::::: 

echo $n "."$c
i=1
while [ $i != `expr $robot_num + 1` ];do
  sed "s/\$Robot_num/$i/g" < SOURCE/calib.kl1 > calib$1.kl1
  sed "s/\$Vision/$vision_machine/g" < calib$1.kl1 > SOURCE/calib$i.kl1
  rm calib$1.kl1
  i=`expr $i + 1`
done


: :::::::::::::::::::::: MAKE USER_SET.KL1 ::::::::::::::::::::::::::::

echo $n "."$c
awk 'BEGIN{
print "" > "SOURCE/user_set.kl1"
print ":-module user_set." >> "SOURCE/user_set.kl1"
print "" >> "SOURCE/user_set.kl1"
n=0
}

{
if($1=="ROBOT_NUM"){
robot_num=$3
}

if($1=="JavaVisionMachine"){
print " vision_machine(Machine,Port):-" >> "SOURCE/user_set.kl1"
printf("    Machine = \"%s\",\n",$3) >> "SOURCE/user_set.kl1"
printf("    Port = 12401.\n") >> "SOURCE/user_set.kl1"
print "" >> "SOURCE/user_set.kl1"
print " input_machine(Machine,Port):-" >> "SOURCE/user_set.kl1"
printf("    Machine = \"%s\",\n",$3) >> "SOURCE/user_set.kl1"
printf("    Port = 12402.\n") >> "SOURCE/user_set.kl1"
print "" >> "SOURCE/user_set.kl1"}

if($1=="MIN_TAKE")min=$3
if($1=="MAX_TAKE"){
print " sihai_block(Range):-" >> "SOURCE/user_set.kl1"
printf("    Range = [%f,%f].\n",min,$3) >> "SOURCE/user_set.kl1"
print "" >> "SOURCE/user_set.kl1"}

if($1=="MIN_IN")min=$3
if($1=="MAX_IN"){
print " sihai_box(Range):-" >> "SOURCE/user_set.kl1"
printf("    Range = [%f,%f].\n",min,$3) >> "SOURCE/user_set.kl1"
print "" >> "SOURCE/user_set.kl1"}

if($1=="MIN_DELI")min=$3
if($1=="MAX_DELI"){
print " deliver_block(Range):-" >> "SOURCE/user_set.kl1"
printf("    Range = [%f,%f].\n",min,$3) >> "SOURCE/user_set.kl1"
print "" >> "SOURCE/user_set.kl1"}

if($1=="JavaMachine"){
    machine=$3
      ++n}
if($1=="Port1") port=$3
if($1=="Port2"){
   if(n==1){
      print " machine(R,Connect_Machine,Write_Port,Read_Port):-" >> "SOURCE/user_set.kl1"
      printf("      ( R = r1 -> Connect_Machine = \"%s\",\n",machine) >> "SOURCE/user_set.kl1"
      printf("                  Write_Port = %d,\n",port) >> "SOURCE/user_set.kl1"
      printf("                  Read_Port = %d",$3) >> "SOURCE/user_set.kl1"
  }
   if(n<=robot_num && n!=1){
      printf(";\n        R = r%d -> Connect_Machine = \"%s\",\n",n,machine) >> "SOURCE/user_set.kl1"
      printf("                  Write_Port = %d,\n",port) >> "SOURCE/user_set.kl1"
      printf("                  Read_Port = %d",$3) >> "SOURCE/user_set.kl1"
  }
   if(n==robot_num){print ")." >> "SOURCE/user_set.kl1"
      print "" >> "SOURCE/user_set.kl1"}
}
}' config.tmp


: :::::::::::::::::::::: MAKE MOVE.C ::::::::::::::::::::::::::::

write_file(){
      eval sed -e 's/$1/$2/g' < $3 > $4
}

search(){
ans=`awk 'BEGIN{
n=0
}
{
if($1=="Machine") --n
if( check==$1 && n==0 )
  print $3
}' n=$1 check=$2 config.tmp`
}

javasearch(){
ans=`awk 'BEGIN{
n=0
}
{
if($1=="JavaMachine") --n
if( check==$1 && n==0 )
  print $3
}' n=$1 check=$2 config.tmp`
}

i=1
while [ $i != `expr $robot_num + 1` ];do
 echo $n "."$c
 search $i "TTY"
 sed "s/\$Tty/$ans/g" <C_PROG/javaMove.c >javaMove$i.c
 echo $n "."$c
 search $i "RealPort"
 sed "s/\$RW_port/$ans/g" <javaMove$i.c >C_PROG/javaMove$i.c
 echo $n "."$c
 rm javaMove$i.c
 i=`expr $i + 1`
done
echo

: :::::::::::::::::::::: MAKE is.kl1 ::::::::::::::::::::::::::::
echo $n "."$c

sed "s/\$Vision/$java_machine/g" <SOURCE/originalIS.kl1> SOURCE/is.kl1


: :::::::::::::::::::::: MAKE KL1Camera.java::::::::::::::::::::::::::::
echo $n "."$c

sed "s/\$Vision/$vision_machine/g" <JavaRob/kl1C.java> JavaRob/KL1Camera.java

: :::::::::::::::::::::: MAKE ConnectKL1.java ::::::::::::::::::::::::::::

echo $n "."$c

i=1

javaport=""
robport=""
robmachine=""
while [ $i != `expr $robot_num + 1` ];do
  javasearch $i "Port1"
  port1=$ans
  javasearch $i "Port2"
  port2=$ans
  search $i "Machine"
  machine=$ans
  search $i "RealPort"
  port=$ans
	
  if [ $i = $robot_num ]
    then javaport=$javaport"{$port1,$port2}"
         robport=$robport"$port"
         robmachine=$robmachine"\"$machine\""; 
    else javaport=$javaport"{$port1,$port2},"
         robport=$robport"$port,"
         robmachine=$robmachine"\"$machine\","; 
  fi

  i=`expr $i + 1`
done

sed "s/\$Num/$robot_num/g" <JavaRob/conKL1.java> ConnectKL1.java
sed "s/\$KLICPort/$javaport/g" <ConnectKL1.java> JavaRob/ConnectKL1.java
sed "s/\$RobotPort/$robport/g" <JavaRob/ConnectKL1.java> ConnectKL1.java
sed "s/\$MachineName/$robmachine/g" <ConnectKL1.java> JavaRob/ConnectKL1.java

rm ConnectKL1.java


cat <<GAZONK

# All set!

GAZONK

