#! /bin/sh
############################################################################
# LinPac startup script (model 0.14)                                       #
############################################################################

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

DIRNAME=LinPac
PREFIX=/usr/local
VERSION=0.16pre3
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/; s/#PORT#/$BBSPORT/" <$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 $VERSION > $HOMEDIR/plugins/version

  echo Instalation done.
  echo Press ENTER to run LinPac
  read ENTER

else ############ LinPac alreasy installed, check the version ##############
  
  OLDVER=`cat $HOMEDIR/plugins/version 2> /dev/null`
  if [ ! "$OLDVER" = "$VERSION" ]; then
    echo "LinPac version was updated to $VERSION"
    echo "Some macros may have been updated in this version. Would you like to update"
    echo "the macros in your home directory? (your current macros will be backed up"
    echo "to the macro.old directory). If you answer NO to the following question LinPac"
    echo "won't ask you next time."
    echo -n "Continue updating [Y/n]? "
    read ANSWER
    ANSWER=`echo $ANSWER | tr [a-z] [A-Z]`
    if [ "$ANSWER" = "Y" -o "$ANSWER" = "YES" ]; then
      cd $HOMEDIR
      rm -rf macro.old
      mv macro macro.old
      mkdir macro
      cp macro.old/init.mac macro

      #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
            cp $I $HOMEDIR/macro/
          fi
        fi
      done
    fi
      
    echo $VERSION > $HOMEDIR/plugins/version
  fi  
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
      export SHAREDIR
      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 $?
  