#!/bin/bash
#
#  Created 3 Aug 1996 by LD 
#          3 Aug 1996 : created and added different stuff
#          4 Aug 1996 : continued to add different stuff. 
#
#

GEN_OPT=general_opt.dump
NSS_OPT=nacctd_defs.dump
NSS_OPT1=nacctd_defs1.dump
OPT_OPT=optim_defs.dump
DLV_OPT=debug_lvl.dump
DBG_OPT=debug_opt.dump
MAIN_DUMP=main.dump
ROOT=root_path.dump
CONF_FILE=conf_file.dump
LOG_FILE=log_file.dump
CGI_OPT=cgi_defs.dump
CGI_OPT1=cgi_defs1.dump
CGI_OPT2=cgi_defs2.dump

CCT="-DCOUNT_CRASH_TIME"
CLP="-DCHECK_LOGOUT_PID"
NSS="-DNACCTD_SUPPORT"
EXK="-DEXPLICIT_KILLS"

NTS="-D_NACCTD_TIMESTAMP"
NPN="-D_NACCTD_PROTONO"
NSI="-D_NACCTD_SOURCEIP"
NSP="-D_NACCTD_SOURCEPORT"
NDI="-D_NACCTD_DESTIP"
NDP="-D_NACCTD_DESTPORT"
NPC="-D_NACCTD_PACKETCOUNT"
NS_="-D_NACCTD_SIZE"
NDD="-D_NACCTD_DEVICE"
NUN="-D_NACCTD_USERNAME"
NAP="-DNET_ACCT_PATH"

DDL="-DDEBUG_LEVEL"
DNF="-DDO_NOT_FORK"
ND_="-DNO_DEBUG"
LTS="-DLOG_TO_SCREEN"
LTY="-DLOG_TO_SYSLOG"

CGPRT="-DCGI_PORT"
CGADR="-DCGI_ADDR"
CGHSS="-DHIDE_SU_STAT"

GENOPT=GenOptions
FILOPT=GetPaths
NCTOPT=NacctdOptions
CGIOPT=CgiOptions
OPTOPT=GetOptOptions
DBGOPT=DebugOptions
GENALL=GenAll
VIEWMK=View
COMPIL=Compile
INSTAL=Install
CLEARC=ClearConfig
EXIT__=Exit
README=Readme


##########################################################################
function GenOptions()
{
dialog --title "Configuration Options" --checklist \
"Chose Configuration Options for Admind Package" \
23 76 17 \
CCT "Include crash intervals to the overall usage?" off \
CLP "Check Logout Pid?" on \
NSS "Traffic statistic support through nacctd?" on \
EXK "Issue explicit kills to those whose limit has expired? " on \
  2> ${GEN_OPT}
if [ "$?" -ne "0" ]; then 
  rm -f "$GEN_OPT"
  return 1
fi

}

function GenGenOptions()
{
  if [ -f ${GEN_OPT} ]
  then 
    cat >> Makefile.in << EOF

# General options, generated By Configure
EOF
    echo -n "CDEFS11= " >> Makefile.in
    for i in `cat $GEN_OPT`
    do
      eval echo -n \"\${`eval echo ${i}`} \"  >> Makefile.in
    done
    echo >> Makefile.in
  else 
    dialog --title "Confirm default options" --yesno \
      "`cat << EOF
      
  Warning: You have not specified 
  general options yet. 
  
  Continue with default creation?
EOF`" 20 60

    if [ "$?" -ne "0" ]; then
      return 1
    fi  
    # default settings are as follows:
    cat >> Makefile.in << EOF
    
# General options, generated by Configure as default:
EOF
    echo -n "CDEFS11= " >> Makefile.in

    echo  "$CLP $NSS $EXK" >> Makefile.in
  fi
}

################################################################################    

function NacctdOptions()
{

dialog --title "Nacctd support options" --checklist \
"Choose what fields your nacctd log entries contain" \
23 76 17 \
NTS "Do your nacctd logs contain time stamp field?(required)" on \
NPN "Do your nacctd logs contain protocol number field?" on \
NSI "Do your nacctd logs contain Source IP field?" on \
NSP "Do your nacctd logs contain Source Socket field?" on \
NDI "Do your nacctd logs contain Destination IP field?" on \
NDP "Do your nacctd logs contain Destination Socket field?" on \
NPC "Do your nacctd logs contain Packets Count field?" on \
NS_ "Do your nacctd logs contain Transfer size field(required)?" on \
NDD "Do your nacctd logs contain Device field?(required)" on \
NUN "Do your nacctd logs contain Associated User Name (required)?" on  \
            2> $NSS_OPT
if [ "$?" -ne "0" ]; then
  rm -f $NSS_OPT
  return 1
fi
dialog --title "Nacctd 'net-acct' log file" --inputbox "`cat << EOF

  You have to specify nacctd's log file 
full pathname.
  If unsure, just hit enter to proceed 
with default. 
  Default is /var/adm/net-acct.

EOF`" 20 70 2> $NSS_OPT1
if [ $? -ne 0 ]; then
  rm -f $NSS_OPT
  rm -f $NSS_OPT1
  return 1
fi

if [ -z "`cat $NSS_OPT1`" ]; then
  echo -n "/var/adm/net-acct" > $NSS_OPT1
fi
return 0

}

function GenNacctdOptions()
{
  if [ -z "`grep \\\\$NSS Makefile.in`" ]; then
    return 0;
  fi
     
  echo >> Makefile.in
  echo -n "CDEFS12= " >> Makefile.in
  if [ -f $NSS_OPT -a -f $NSS_OPT1 ]
  then 
    for i in `cat $NSS_OPT`; do
      eval echo -n \"\${`eval echo ${i}`} \"  >> Makefile.in
    done
    echo -n "${NAP}=\\\"`cat $NSS_OPT1`\\\"" >> Makefile.in
    echo >> Makefile.in
  else 
    dialog --title "Confirm default options" --yesno \
    "`cat << EOF
    
  Warning: You have not specified 
  Nacctd options yet. 
  
  Continue with default creation?  
EOF`" 20 60 
    if [ "$?" -ne "0" ]; then
      return 1
    fi  
    echo -n "$NTS $NPN $NSI $NSP $NDI $NDP $NPC $NS_ $NDD $NUN " >> Makefile.in
    echo "${NAP}=\\\"/var/adm/net-acct\\\"" >> Makefile.in
  fi
}

#########################################################################

function DebugOptions ()
{
  dialog --title "Debug level" --menu \
  "Choose debug level from menu:" 23 76 17 \
  "0" "Completely Dumb one" "1" "Only fatal errors are logged(recommended)" \
  "2" "Parser Hints are also logged" \
  "3" "For developers" "4" "For developers" "5" "Huge debugs" \
  2> $DLV_OPT
  if [ "$?" -ne "0" ]
  then
    rm -f $DLV_OPT
    return 1
  fi
  dialog --title "Debug Options" --checklist \
  "Set Debug options" 23 76 17 \
  DNF "Do not fork" off \
  ND_ "Eliminate levels 3-5 debug code" on \
  LTS "Log to screen ( to stderr ) instead of logfile" off \
  LTY "Duplicate debug level 1 messages to syslog?" off \
  2> $DBG_OPT
  if [ "$?" -ne "0" ]
  then 
    rm -f $DLV_OPT $DBG_OPT
    return 1
  fi
}

# this function also generates optimize options created by GetOptOptions  
function GenDebugOptions()
{
  echo -n "CDEFS13= " >> Makefile.in
  GenOptOptions
  if [ $? -ne 0 ] ; then
    return 1
  fi
  if [ -f $DLV_OPT ]
  then 
    echo -n "${DDL}=`cat $DLV_OPT` " >> Makefile.in
  else 
    dialog --title "Confirm default options" --yesno "`cat << EOF
    
    Warning: You have not specified 
    DebugOptions yet.
    
    Would you like to continue with
    default creation ?
EOF`" 20 60 
    if [ "$?" -ne "0" ]; then
      return 1
    fi    
    
    echo -n "${DDL}=1 " >> Makefile.in
  fi
  if [ -f $DBG_OPT ]
  then
    for i in `cat $DBG_OPT`; do
      eval echo -n \"\${`eval echo $i`} \" >> Makefile.in
    done
  else 
    echo -n $ND_ >> Makefile.in
  fi
  echo >> Makefile.in
}

#########################################################################
function GetOptOptions()
{
  dialog --title "Optimization Options" --menu \
"Choose Optimization Level\n(-O3 recommended)" 12 40 4 \
"  " "Do not optimize" \
"-O " "" "-O1" "" "-O2" "" "-O3" "" "-O4" "" \
"-O5" "" "-O6" "" "-O7" "" \
     2> $OPT_OPT
if [ "$?" -ne "0" ]
then 
  rm -f $OPT_OPT
  return 1
fi
 
}

function GenOptOptions ()
{
  if [ -f $OPT_OPT ]
  then
    echo -n "`cat $OPT_OPT` " >> Makefile.in
  else
    dialog --title "Confirm default generation" --yesno "`cat << EOF
    
Warning:You have not specified 
optimization options yet.

Would you like to proceed with default 
optimization options (-O3) ?

EOF`" 14 60
    if [ "$?" -eq "0" ]
    then
      echo -n "-O3 " >> Makefile.in
    else
      return 1
    fi
  fi
}

###########################################################
function GetPaths()
{
  
  dialog --title "Install Path" --inputbox "`cat << EOF
  
  Type in the path where the whole 
  installation should go to 
  (default is /usr/local/admind)
  
  For default just press <Enter>
  
EOF`" 15 70 2> $ROOT
  if [ $? -ne 0 ]
  then
    rm -f $ROOT
    return 1
  fi
  
  if [ -z "`cat $ROOT`" ]; then
    echo "/usr/local/admind" > $ROOT
  fi
  
  dialog --title "Configuration file" --inputbox "`cat << EOF
  
  Type in the file name of the
  admind configuration file
  without any path (e.g. default.conf )
  
  (default is admind.conf)
  
EOF`" 15 70 2> $CONF_FILE
  if [ $? -ne 0 ]
  then
    rm -f $ROOT $CONF_FILE
    return 1
  fi
  if [ -z "`cat $CONF_FILE`" ]; then
    echo "admind.conf" > $CONF_FILE
  fi
  
  dialog --title "Log file" --inputbox "`cat << EOF
  
  Type in the file name for  
  admind log file
  without any path ( e.g. default.log )
  
  (default is admind.log)
  
EOF`" 15 70 2> $LOG_FILE 
  if [ $? -ne 0 ]
  then 
    rm -f $ROOT $CONF_FILE $LOG_FILE
    return 1
  fi
  if [ -z "`cat $LOG_FILE`" ]; then
    echo "admind.log" > $LOG_FILE
  fi
  
  return 0
}

function GenPaths()
{
  if [ -f $ROOT -a -f $CONF_FILE -a -f $LOG_FILE ]
  then
    echo "ROOT_DIR=`cat $ROOT`" >> Makefile.in
    echo "BIN_DIR=\$(ROOT_DIR)/bin" >> Makefile.in
    echo "LIB_DIR=\$(ROOT_DIR)/lib" >> Makefile.in
    echo "LOG_DIR=\$(ROOT_DIR)/var" >> Makefile.in
    echo "CONF_FILE=\$(LIB_DIR)/`cat $CONF_FILE`" >> Makefile.in
    echo "LOG_FILE=\$(LOG_DIR)/`cat $LOG_FILE`" >> Makefile.in
  else 
    dialog --title "Confirm default generation" --yesno "`cat << EOF
    
    You have not specified root path
    and file names for log and 
    configuration files. 
    
    The defaults are:
    
    /usr/local/admind for root directory;
    admind.conf for configuration file;
    admind.log for log file.
    
    Would you like to continue with these defaults?
    
EOF`" 22 70 
    if  [ $? -ne 0 ]; then
      return 1
    else 
      echo "ROOT_DIR=/usr/local/admind" >> Makefile.in
      echo "BIN_DIR=\$(ROOT_DIR)/bin" >> Makefile.in
      echo "LIB_DIR=\$(ROOT_DIR)/lib" >> Makefile.in
      echo "LOG_DIR=\$(ROOT_DIR)/var" >> Makefile.in
      echo "CONF_FILE=\$(LIB_DIR)/admind.conf" >> Makefile.in
      echo "LOG_FILE=\$(LOG_DIR)/admind.log" >> Makefile.in
    fi
  fi
  return 0
}

#############################################################
function CgiOptions ()
{
  dialog --title "admind address" --inputbox "`cat << EOF
  For cgi utilities to work, you have to specify where should 
they seek for admind.  It should be either a dotted quad or
valid DNS name of a machine where admind is running on.

  Default is localhost (just hit Enter for default).

  WARNING: those parameters have no influence on admind. Ad-
mind's socket parameters (bind address and port ) are as 
specified in its config file. These settings are applied for 
Cgi utilites only.
EOF`" 20 70 2>$CGI_OPT
  if [ $? -ne 0 ]
  then
    rm -f $CGI_OPT
    return 1
  fi
  if [ -z "`cat $CGI_OPT`" ]; then
    echo -n "localhost" > $CGI_OPT
  fi
  dialog --title "admind listen port" --inputbox "`cat << EOF
  
  You have also to specify an admind's port number that 
cgi utils will assume it is listening on. Default is 1880 
(just hit Enter for default).

  Assuming you have read the previous warning. Make 
sure also you to enter a valid unsigned integer.

EOF`" 20 70 2> $CGI_OPT1
 if [ $? -ne 0 ]; then 
  rm -f $CGI_OPT $CGI_OPT1
  return 1
 fi
 if [ -z "`cat $CGI_OPT1`" ];then
   echo -n "1880" > $CGI_OPT1
 fi

 dialog --title "Other cgi options" --checklist "Other CGI options" \
 20 70 12 \
 CGHSS "Hide SU-marked users statistics?" on 2> $CGI_OPT2
 if [ $? -ne 0 ]; then
   rm -f $CGI_OPT $CGI_OPT1 $CGI_OPT2
   return 1
 fi
 
 return 0
}

function GenCgiOptions()
{
  if [ ! -f $CGI_OPT -o ! -f $CGI_OPT1 -o ! -f $CGI_OPT2 ]; then
   dialog --title "Confirm defaul generation" --yesno "`cat << EOF
   
   You have not specified Cgi Utilites options yet.
   
   Would you like to continue with default 
   generation?
   
EOF`" 20 70 
  if [ $? -ne 0 ]; then
    return 1;
  fi
  echo -n "CDEFS16= " >> Makefile.in
  echo -n "${CGADR}=\\\"localhost\\\" " >> Makefile.in
  echo -n "${CGPRT}=1880 " >> Makefile.in
  echo -n "${CGHSS}" >> Makefile.in
  echo >> Makefile.in
else 
  echo -n "CDEFS16= " >> Makefile.in
  echo -n "${CGADR}=\\\"`cat $CGI_OPT`\\\" " >> Makefile.in
  echo -n "${CGPRT}=`cat $CGI_OPT1` " >> Makefile.in
  for i in `cat $CGI_OPT2`
  do 
    eval echo -n \"\${`eval echo ${i}`} \" >> Makefile.in
  done
  echo >> Makefile.in
fi
return 0

}


#############################################################
function GetOptions()
{
  while /bin/true; do
    dialog --title "MainMenu" --menu "Choose Action" 18 70 10 \
    README "Read the file README.admind first!" \
    GENOPT "General admind finetuning options" \
    FILOPT "File and directories" \
    CGIOPT "CGI utilities options" \
    NCTOPT "Nacctd finetuning options" \
    OPTOPT "Optimization options" \
    DBGOPT "Debug Options" \
    GENALL "Generate Makefile.in with options specified included in" \
    VIEWMK "View current Makefile.in" \
    COMPIL "Go Compile it!" \
    INSTAL "Go and install binaries to directories specified!" \
    CLEARC "Clear all configs & remove objects (do make clean)" \
    EXIT__ "Exit Configure" \
       2> $MAIN_DUMP
    if [ "$?" -ne "0" ]
    then
      rm -f $MAIN_DUMP
      return 1
    fi
    action=`cat $MAIN_DUMP`
    eval \$$action
    if [ "$?" -eq "5" ]
    then
      rm -f $MAIN_DUMP
      return
    fi
  
    done
}

function Exit()
{ 
  return 5
}

function Readme()
{
  dialog --title "README.admind" --textbox README.admind 22 74
}
function View()
{ 
  if [ -f Makefile.in ]; then
    dialog --title "Makefile.in" --textbox Makefile.in 22 74
  else 
    dialog --title "Missing Makefile.in" --msgbox "`cat << EOF
    
    He!:) ( !     )
    You seem like not having generated a Makefile.in, so 
    I am unable to view it.
    
    Please use "Generate Makefile.in" option from main
    menu first .
    
EOF`" 15 70
    return 1
  fi  
    
}


function CleanConfigs()
{
  rm -f *.dump
}

function GenAll()
{
  > Makefile.in
  GenPaths && GenGenOptions && GenNacctdOptions && GenDebugOptions && \
  GenCgiOptions
  if [ "$?" -ne "0" ] ; then
    rm -f Makefile.in
    CleanConfigs
    return 1
  fi
  # this is for log and config files:
  
  echo "CDEFS14= -DCONF_FILE=\\\"\$(CONF_FILE)\\\" -DLOG_FILE=\\\"\$(LOG_FILE)\\\"" >> Makefile.in
  
  echo "CDEFS_IN= \$(CDEFS13) \$(CDEFS11) \$(CDEFS12) \$(CDEFS14) \$(CDEFS16)" >> Makefile.in
  CleanConfigs
}

function Compile()
{
  if [ -f Makefile.in ]; then
    clear
    make 
    echo Press a key
    read junk
  else 
    dialog --title "Message" --msgbox "`cat << EOF
    
    You have not generated a Makefile.in yet.
    Please use \"Generate Makefile.in\" option
    from main menu first.
    
EOF`" 20 60
    return 1
  fi 

}

function Install()
{
  if [ -f Makefile.in ]; then
    clear
    make install
    echo press a key
    read junk
  else 
    dialog --title "Message" --msgbox "`cat << EOF
    
    You have not generated a Makefile.in yet.
    Please use \"Generate Makefile.in\" option
    from main menu first.
    
EOF`" 20 60
    return 1
  fi 

}

function ClearConfig()
{
  CleanConfigs
  rm -f Makefile.in
  clear
  rm -f *o admind awreader aparser
}

#####################################################################
# rm -f Makefile.in
CleanConfigs
if 
  GetOptions
  [ "$?" -eq "0" ]
then
  echo Done...
else 
  CleanConfigs
  echo error...
fi
