#! /bin/sh
############################################################################
# LinPac startup script                                                    #
############################################################################

if [ x$HOME = x ]; then
  echo 'Cannot determine your home directory - $HOME is not set'
  exit 1
fi

DIRNAME=LinPac
PREFIX=/usr/local
HOMEDIR=$HOME/$DIRNAME
SHAREDIR=$PREFIX/share/linpac

if [ ! -d $HOMEDIR ]; then
  echo 'Hello dear user. You seem to run LinPac for the first time.'
  echo 'LinPac has to create a directory in your home directory for storing'
  echo 'your personal configuration.'
  echo
  echo 'For creating your personal configuration please answer following questions:'
  echo
  echo Your callsign:
  read CALLSIGN
  CALL=`echo $CALLSIGN | tr [a-z] [A-Z]`
  echo
  echo Enter your home BBS callsign:
  read BBSCALLS
  BBSCALL=`echo $BBSCALLS | tr [a-z] [A-Z]`
  echo Enter the name of port for connecting $BBSCALL
  read BBSPORT
  echo Enter the digipeaters used to connect $BBSCALL or press enter 
  echo when no digipeaters are used:
  read DIGIPTS
  DIGIS=`echo $DIGIPTS | tr [a-z] [A-Z]`
  echo Enter the full hierarchical address of $BBSCALL
  echo '(e.g. #MOR.CZE.EU)'
  read HIADDR
  HADDR=$BBSCALL.`echo $HIADDR | tr [a-z] [A-Z]`
  echo
  echo Thank you, $CALL
  BBSADDR="$BBSPORT:$BBSCALL $DIGIS"
  
  echo Please wait a moment for creating your personal configuration

  mkdir $HOMEDIR

  #create subdirectories
  mkdir $HOMEDIR/bin
  mkdir $HOMEDIR/macro
  mkdir $HOMEDIR/save
  mkdir $HOMEDIR/user
  mkdir $HOMEDIR/log
  mkdir $HOMEDIR/mail
  mkdir $HOMEDIR/mail/$BBSCALL
  mkdir $HOMEDIR/plugins
  
  if [ ! -d /var/ax25/mail/$BBSCALL ]; then
    mkdir -p /var/ax25/mail/$BBSCALL
  fi

  PWDBEFORE=`pwd`

  #copy 'mail'
  cd $SHAREDIR/mail
  for I in *; do
    if [ ! -x $I ]; then
      cp $I $HOMEDIR/mail/$I
    fi
  done

  #copy 'bin'
  cd $SHAREDIR/bin
  for I in *; do
    if [ $I = commands ]; then
      cp $I $HOMEDIR/bin/$I
    else
      ln -s `pwd`/$I $HOMEDIR/bin/$I
    fi
  done

  #copy 'macro'
  cd $SHAREDIR/macro
  for I in *; do
    if test -d $I; then
      mkdir $HOMEDIR/macro/$I
      cd $I
      for J in *; do
        cp $J $HOMEDIR/macro/$I
      done
      cd ..
    else
      if [ $I = init.mac ]; then
        sed "s/#CALL#/$CALL/; s/#BBS#/$BBSADDR/; s/#ADDR#/$HADDR/" <$I >$HOMEDIR/macro/$I
      else
        cp $I $HOMEDIR/macro/
      fi
    fi
  done

  #copy root
  cd $SHAREDIR
  cp station.data $HOMEDIR
  for I in *.ctt; do
    cp $I $HOMEDIR/$I
  done

  cd $PWDBEFORE
  
  echo Instalation done.
  echo Press ENTER to run LinPac
  read ENTER
fi

#PATCH991003# Setup new installed plugins for current user
if ! test -d $HOMEDIR/plugins ; then
  mkdir $HOMEDIR/plugins
fi
cd $SHAREDIR/plugins
for PLUGIN in * ; do
  cd $SHAREDIR/plugins
  if test -x $PLUGIN ; then
    if ! test -r $HOMEDIR/plugins/$PLUGIN ; then
      cd $HOMEDIR
      eval $SHAREDIR/plugins/$PLUGIN
      date +%y%m%d > $HOMEDIR/plugins/$PLUGIN
    fi
  fi
done

cd $HOMEDIR
export PATH=$PATH:$SHAREDIR/mail:$SHAREDIR/tools
$SHAREDIR/linpac $*
exit $?
  