#!/bin/sh
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL 
# stta: (@#) 1.31051043346- [2004_01_29_102036]

USAGE="mktools.sh [lang] [webmin-etc]";
dn=/dev/null 
TTA_CMD="tarantella"

if [ -z "$WEBMIN_ETC" ] ; then 
   if  [ -n "$2" ] ; then 
         WEBMIN_ETC=$2
   fi 
   [ ! -d  "$WEBMIN_ETC" ] && WEBMIN_ETC="/etc/webmin"
fi 
if [ -z "$STTA_DATACONF" ] ; then 
    STTA_DATACONF=$WEBMIN_ETC"/stta/dataconf"
    export STTA_DATACONF
fi
if [ -z "$STTA" ] ; then 
    STTA=`cat $WEBMIN_ETC/miniserv.conf 2>$dn | egrep "^root=" | sed 's/root=//g'`/stta 
    export STTA
fi

[ ! -d "$STTA" ] && STTA=`pwd`
[ ! -r "$STTA/envprofile" ] && echo "STTA environment not set " && exit 2
if [ -z "$defaultLang" ] ; then 
    defaultLang=`cat $WEBMIN_ETC/config 2>$dn | egrep "^lang=" | sed 's/lang=//g'` 
    export defaultLang
fi
. $STTA/envprofile 
DIRTOOLS=$STTA/$STTA_SHELLS

if [ -n "$1" ] ; then 
   WEBMIN_LANG=$1
else 
   WEBMIN_LANG=$defaultLang
fi
if [ ! -d "$STTA_DATACONF" ] ; then 
    echo "STTA config not set "
    exit 2
fi
STTA_TOOLSDIR=`fgrep "stta_tools_dir" $STTA_DATACONF/shells 2>/dev/null | sed 's/stta_tools_dir=//g'`  
if [ -z "$STTA_TOOLSDIR" ] ; then 
    echo "STTA TOOLS Dir not set "
    exit 2
fi
TTABINDIR=`fgrep "tta_dir" $STTA_DATACONF/tta 2>/dev/null | sed 's/tta_dir=//g'`"/bin"
TTACMD=$TTABINDIR"/"$TTA_CMD
if [ ! -x "$TTACMD" ] ; then 
    echo "TTA bin file not set "
    exit 2
fi

N=""
C=""
if echo -n | grep n >$dn
then
  C="\c"
else
  N="-n"
fi
case `uname -a` in
*inux*) N="$N -e" ;;
esac

if [ -d $STTA/lang ] ; then 
   [ ! -r "$STTA/lang/shells/$WEBMIN_LANG" ] && WEBMIN_LANG=$defaultLang
   [ -r "$STTA/lang/shells/$WEBMIN_LANG" ] &&  {
        . $STTA/lang/shells/$WEBMIN_LANG 
   }
else 
    echo "LANG not set "
    echo "$USAGE"
    exit 2
fi

UNAME=`uname`
case  $UNAME in 
      Linux|inux|linux) CRONTAB=/var/spool/cron/tabs/root 
                       ;;
      *)
             CRONTAB=/usr/spool/cron/tabs/root
           ;;
esac
[ ! -f $CRONTAB ] && CRONTAB=/usr/spool/cron/crontabs/root

has_monitor=`fgrep "has_monitor" $STTA_DATACONF/main 2>/dev/null | sed 's/has_monitor=//g'`  
has_cronsess=`fgrep "has_cronsess" $STTA_DATACONF/main 2>/dev/null | sed 's/has_cronsess=//g'`  

if [ ! -d $STTA_TOOLSDIR ] ; then 
     mkdir $STTA_TOOLSDIR 
     chown root:bin $STTA_TOOLSDIR
     chmod 755 $STTA_TOOLSDIR
fi
if [ ! -d $STTA/$TOOLSDIR ] ; then 
     mkdir $STTA/$TOOLSDIR 
else 
     rm -r $STTA/$TOOLSDIR/* 2>$dn
fi

echo "$msg_mkfiles"
for file in ` echo $LIST_STTACMDS $LIST_TTACMDS $LIST_TCLS `
do
    [ "$file" = "openldap" ] && [ ! -d "/etc/openldap" ] && continue
    [ "$file" = "oldapadm" ] && [ ! -d "/etc/openldap" ] && continue
    cat $DIRTOOLS/$file.sh | sed "s,STTATOOLSDIR,$STTA_TOOLSDIR,g" |\
                       sed "s,ETCWEBMINDIR,$WEBMIN_ETC,g" >$STTA_TOOLSDIR/$file
    chmod 555  $STTA_TOOLSDIR/$file
    chown root:bin $STTA_TOOLSDIR/$file
    chmod 555 $STTA_TOOLSDIR/$file
    cp -p $STTA_TOOLSDIR/$file $STTA/$TOOLSDIR
done 
[ -x $STTA_DATACONF/envstta ] && cp -p $STTA_DATACONF/envstta $STTA_TOOLSDIR
echo $N "$msg_mkbrandinfo$C"
cat $STTA/$SttaBrandInfoFile | grep -v "stta/$TOOLSDIR/" >$STTA/$brandInfoFile
for file in ` echo $LIST_STTACMDS $LIST_TTACMDS $LIST_TCLS `
do
    brand_data=`grep "^# stta: (@#)" $STTA/$TOOLSDIR/$file 2>$dn`
    if [ -n "$brand_data" ] ; then
        brand_code=`echo $brand_data | awk '{ printf "%s", $4 }' `
        brand_date=`echo $brand_data | awk '{ printf "%s",$5 }' `
        file_size=`ls -l $STTA/$TOOLSDIR/$file | awk '{ printf "%s", $5 }' `
        echo "stta/$TOOLSDIR/$file::$brand_code::$file_size::$brand_date" >>$STTA/$brandInfoFile
        echo $N "$file $C";
    fi
done
echo "";

echo "$msg_mklibs"
if [ ! -d $STTA_TOOLSDIR/lang ] ; then 
     mkdir $STTA_TOOLSDIR/lang
     chown root:bin $STTA_TOOLSDIR/lang
     chmod 755 $STTA_TOOLSDIR/lang
else
     rm -r $STTA_TOOLSDIR/lang/*
fi
mkdir $STTA_TOOLSDIR/lang/shells
chown root:bin $STTA_TOOLSDIR/lang/shells
chmod 755 $STTA_TOOLSDIR/lang/shells
for file in ` echo $STTA/lang/shells/* ` 
do
   name=`basename $file`
   if [ -r $STTA/lang/$name ] ; then 
        cp -p $file $STTA_TOOLSDIR/lang/shells/$name 
        chown root:bin $STTA_TOOLSDIR/lang/shells/$name
        chmod 755 $STTA_TOOLSDIR/lang/shells/$name
   fi
done 
cp -pr $STTA_TOOLSDIR/lang $STTA/$TOOLSDIR 
for name in ` echo $LIST_DIRS ` 
do
    if [ ! -d $STTA_TOOLSDIR/$name ] ; then 
         mkdir $STTA_TOOLSDIR/$name
         chown root:bin $STTA_TOOLSDIR/$name
         chmod 755 $STTA_TOOLSDIR/$name
    else
         rm $STTA_TOOLSDIR/$name/* 2>$dn
    fi
    eval "LIST=\$LIST_$name"
    for file in ` echo $LIST ` 
    do
        cp -p $STTA/$file $STTA_TOOLSDIR/$name 
    done
    cp -pr $STTA_TOOLSDIR/$name $STTA/$TOOLSDIR 
done

if [ ! -d "$2" ]  ; then 
   npath=`echo $PATH | awk -F ":" '{ print $1}'`
   # echo $N "$msg_binpath$C"
   #read npath 
else 
  npath=$2
fi
[ "$npath" ] && [ -d "$npath" ] && {
   echo "$msg_mklinks"
   for file in ` echo $LIST_LNKCMDS `
   do
       rm $npath/$file 2>$dn
       ln -s $STTA_TOOLSDIR/$file $npath/$file
   done 
}

set_monline () {
   echo "#!/bin/sh" >$TTACMD
   echo "if [ -n \"\$1\" ] ">>$TTACMD
   echo "then ">>$TTACMD
   echo "    case \"\$1\" in ">>$TTACMD
   echo "        start) $STTA_TOOLSDIR/monitor auto \$1 ;; ">>$TTACMD
   echo "        stop) $STTA_TOOLSDIR/monitor auto \$1 ;; ">>$TTACMD
   echo "    esac ">>$TTACMD
   echo "fi ">>$TTACMD
   cat $TTAORG >>$TTACMD
   chown bin:bin $TTACMD
   chmod 755 $TTACMD
}

if [ $has_cronsess = "true" ] ; then
   line=`fgrep ttasess $CRONTAB`
   [ -z "$line" ] && echo "30 * * * * $STTA_TOOLSDIR/ttasess -r 1>/dev/null 2>/dev/console ">>$CRONTAB
fi
if [ $has_monitor =  "true" ] ; then
   TTAORG=$TTACMD".org"
   [ ! -f $TTAORG ] &&  cp -p $TTACMD $TTAORG && set_monline
   line=`fgrep monitor $CRONTAB`
   [ -z "$line" ] && echo "59 * * * * $STTA_TOOLSDIR/monitor task 1>/dev/null 2>/dev/console ">>$CRONTAB
fi
