#!/bin/sh
# ---------------------------------------
#   Copyright (c) 1996  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


 : ::::::::::::::::::: VISION DATA PATH SETTINGS ::::::::::::::::::::

DEF=BLOCK_DATA_PATH
while true ; do
 if ask_default "Which directory in VISION DATA?" "$pwd/DATA"
    then echo "$DEF = $pwd/DATA" >> config.tmp; break
    elif test -d $ask
       then if ask_yes_or_no "$ask in VISION DATA?" yes
               then  echo "$DEF = $ask" >> config.tmp; break
               else echo "OK SETTING AGAIN"; echo; fi
       else echo "BAD DIRECTORY!! 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 read socket port number of ROBOT$1 "
        if check_conf $machine $ask
          then write_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 ROBOT$1"
        if ( check_conf $machine $ask && test $write_port -ne $ask )
           then read_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,$write_port,$read_port,$tty] " yes
      then echo "Machine = $machine" >> config.tmp
           echo "Port1 = $write_port" >> config.tmp
           echo "Port2 = $read_port" >> config.tmp;
           echo "TTY = $tty" >> 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

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)?" "400"
    then min_deli=400;
    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 > SOURCE/calib$i.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=="BLOCK_DATA_PATH"){
print " block_data(PATH):-" >> "SOURCE/user_set.kl1"
printf("    PATH = \"%s/data\".\n",$3) >> "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=="Machine"){
    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`
}

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

cat <<GAZONK

# All set!

GAZONK