#!/bin/sh
#
# Install Script for Mayko mXmap V1.0
Version=1.0.12
#
##########################################################################
#
# Some Installation Directions for Mayko mXmap V1.0
# -------------------------------------------------
# 
#  |---------------------------------------------------------------|
#  |       Copyright (c) 1998, Mayr & Kockelhoff Inc.              |
#  |       All Rights Reserved                                     |
#  |                                                               |
#  | Note: BY INSTALLING THIS SOFTWARE YOU ACCEPT                  |
#  |       ALL THE TERMS AND CONDITIONS OF THE LICENSE AGREEMENT.  |
#  |                                                               |
#  |    Please refer to file LICENSE that comes with the mXmap     |
#  |    distribution.                                              |
#  |    If the LICENSE file is not available, please request a     |
#  |    copy via email from 'mxmap@mayko.com' or see               |
#  |    'www.mayko.com' prior to the install.                      |
#  |                                                               |
#  |---------------------------------------------------------------|
#
# The install script 'install-mxmap' helps to install the Mayko mXmap 
# package.  It could be used to install from the distribution CD or
# the downloaded mXmap and Map packages from 'ftp.mayko.com'
# See 'www.mayko.com' too for further instructions and news.
#
# You can install the Mayko mXmap package as 'root' or as 'normal user'.
# The default install directories are:
#    login as root  install directory = /usr/local/mxmap
#       or as user  install directory = $HOME
#
# Installing from CD:
# -------------------
# Mount the mXmap distribution CD on your maschine. In case you do not
# have a CDROM, you could install as well via NFS.
#
# Installing downloaded mXmap package (TGZ):
# ------------------------------------------
# You should get a copy of the mXmap package "mxmap-*.tar.gz" and
# of the map archive "mxmaps-1.0.tar.gz". Create a directory
# e.g 'mkdir /tmp/mx-temp' and 'cd' into it. Unpack mxmap-*.tar.gz using
# tar and gzip. e.g 'tar -xvzf mxmap-*.tar.gz'.
# Further you move the map archive 'mxmaps-1.0.tar.gz' into the same directory.
# Note, there is no need to unpack the map archive! This will be done
# later by the install script.
#
# Installation in general:
# ------------------------
# 'cd' into the directory, where you find the script 'install-mxmap'. 
# e.g. /cdrom or /tmp/mx-temp package.
# Then just simply type 'sh install-mxmap'. (very important when installing
# from cdrom.)
#
# The install script will copy all necessary binaries and ini file.
# Is the installation done in /usr/local/mxmap, the script will
# create symbolic links for mxmap and gpsd in /usr/bin.
#
# If your local space is limited, you could omit copying the map data
# from the CD to your harddisk. Instead of a map copy, a symbolic link
# will be established. 
# Please note that you need to have the CDROM mounted prior to 
# running Mayko mXmap.
# 
# Prepare user mXmap enviroment (after the root install):
# -------------------------------------------------------
# Mayko mXmap requires a specific enviroment for every user
# who is using mXmap.
# It is important for every single user to have his own 
# preference file '~/mxmap.ini' and '~/maps/map.db'.
#
# Simply launch the install program of the local mXmap install
# directory '/usr/local/mxmap/install-mxmap' once per user.
#
# This will copy the necessary files into the user's directory
# and establish a link for every single map found,
#
# Start Mayko mXmap:
# ------------------
#
# Login with your user account and launch mXmap by typing: 
#	'mxmap'.
# Should be no mxmap link established try: 
#	'/usr/local/mxmap/start-mxmap'
# or	'/usr/local/mxmap/mxmap'
#
# Note: If you did not have superuser priviledges and installed
#	mXmap as user, you'll find the mXmap binary in your HOME
#	directory. You need to type then:
#	'./mxmap'
#
# Enjoy!
#
# Problems:
# ---------
# In case you run into trouble, please let us know and send us
# an e-mail at xmap@mayko.com.
#

#-------------------------------------------------------------------------
# src      id      dest            bin     ini     map.db  .des    maps
#-------------------------------------------------------------------------
# CD       root   /usr/local/mxmap c       c       c       c       l/c
#          user   home             -       c       c       c       l/c
# TGZ      root   /usr/local/mxmap c       c       c       c       c
#          user   home             -       c       c       c       c
# MXMAPDIR root   home             -       c(u)    c       c       l
#          user   home             -       c(u)    c       c       l
#                             c = copy, l = link, c(u) = copy user sample
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------

Other_Platforms ()
{
cat <<EOF

	Mayko mXmap is available for other platforms too.
	Please check the CDROM directory /binaries,
	or see "ftp.mayko.com/pub/mxmap" the latest status.

	To install Mayko mXmap for other platforms, select  
	option 1) of the menu above. 
	After the install is finished, you simply replace the files 
        "mxmap" and "mxmap-static" (if available), by copying 'your' 
        platform mXmap version into /usr/local/mxmap.

EOF
}

#-------------------------------------------------------------------------

echoawk ()
{
  echo $* | awk '{ printf "%s", $0 }'
}

echon ()
{
  echo -n "$*"
}

echoc ()
{
  echo "${*}\c"
}

##########################################################################
yesno()
{
  msg="$1"
  def="$2"
  while true ; do
    echo " "
    $echonl "$msg"
    read answer
    if [ "$answer" ] ; then
      case "$answer" in
        y|Y|yes|YES)
          return 0
          ;;
        n|N|no|NO)
          return 1
          ;;
        q|Q|quit|QUIT)
          exit 99
          ;;
        *)
          echo " "
          echo "ERROR: Invalid response, expected \"yes\" or \"no\"."
          continue
          ;;
      esac
    else
      return $def
    fi
  done
}
 
DisplayLicense ()
{
  if [ -z "$1" ] ; then
    return
  fi

  if [ ! -f "$1" ] ; then
    echo " "
    echo "ERROR: Cannot find license file ... aborting"
    echo " "
    exit 1
  fi

  if [ -z "$PAGER" ] || [ "`type $PAGER`" = "$PAGER not found" ] ; then
    if [ "`type more`" != "more not found" ] ; then
      command=more
    elif [ "`type pg`" != "pg not found" ] ; then
      command=pg
    else
      command=cat
    fi
  else
    command="$PAGER"
  fi

  echo " "
  $command "$1"

  answer=
  while [ -z "$answer" ] ; do
    echo " "
    echo " "
    echo "To accept the terms and conditions of this agreement enter \"accept\"."
    echo "To decline the terms and conditions of this agreement enter \"decline\"."
    echo " "
    $echonl "Do you accept the terms and conditions of this license agreement? "
    read answer
    ucanswer=`echo "${answer}" | tr '[a-z]' '[A-Z]'`
    case "$ucanswer" in
    ACCEPT)
      ;;
    DECLINE)
      echo " "
      echo "License not accepted ... aborting installation"
      echo " "
      exit 1
      ;;
    *)
      echo " "
      echo "ERROR: Invalid response, expected \"accept\" or \"decline\" ... try again"
      answer=
      ;;
    esac
  done
}

##############################################################
#
# Initialization
#

ScriptName=`basename $0`
CurrentDirectory=`pwd`
ScriptDirectory=`dirname $0`
case "${ScriptDirectory}" in
  /*) ;;
  .) ScriptDirectory="$CurrentDirectory" ;;
  *) ScriptDirectory="$CurrentDirectory"/"$ScriptDirectory" ;;
esac

if [ "`type id`" != "id not found" ] ; then
  Installerid=`id -u -n`
fi

if [ "`type uname`" != "uname not found" ] ; then
  OSname=`uname -s`
  if [ "$OSname" = "AIX" ] ; then

    OSrelease=`uname -a | ( read name host minor major foo ; echo $major.$minor )`
  else
    OSrelease=`uname -r`
  fi
else
  OSname=unknown
  OSrelease=unknown
fi

if [ `echo "x\c"` = "x" ] ; then
  echonl=echoc
else
  echonl=echon
fi

#
# Get the filenames
#

GetFileNames ()
{
#for i in ${ScriptDirectory}/* ${ScriptDirectory}/../COMMON/* ; do
for i in $1/* ; do
  if [ -f "$i" ] ; then
    case $i in
      */license*|*/LICENSE*)
        License="$i"
        ;;
      */install-mxmap|*/INSTALL-MXMAP)
        Instscript="$i"
        ;;
      */install-mapdata|*/INSTALL-MAPDATA)
        Mapiscript="$i"
        ;;
      */mxmap|*/MXMAP)
        Xmapbin="$i"
        ;;
      */mxmap-static|*/MXMAP-STATIC)
        Xmapsbin="$i"
        ;;
      */mxmap.ini|*/MXMAP.INI)
        Xmapini="$i"
        ;;
      */mxmap.ini.user|*/MXMAP.INI.USER)
        Xmapini-user="$i"
        ;;
      */mxmap.hlp*|*/MXMAP.HLP*)
        Xmaphlp="$i"
        ;;
      */start-mxmap*|*/START-MXMAP*)
        Xmapscript="$i"
        ;;
      */gpsd|*/GPSD)
        Gpsd="$i"
        ;;
      */gpsd.ini*|*/GPSD.INI*)
        Gpsdini="$i"
        ;;
      */mxmaps*|*/MXMAPS*)
        Mapstgz="$i"
        ;;
      */map.db)
        Mapdb="$i"
        ;;
    esac
  fi
done
}

GetFileNames $ScriptDirectory


#
# check if install from cdrom or mxmap dir
#
Source=tgz

if [ -d ${ScriptDirectory}/binaries ] ; then
        Bindir="${ScriptDirectory}/binaries"
	if [ -d ${ScriptDirectory}/maps ] ; then
        	Mapdir="${ScriptDirectory}/maps"
		Source="cdrom"
	fi
elif [ "$ScriptDirectory" = "/usr/local/mxmap" ]; then
        	Mapdir="${ScriptDirectory}/maps"
		Source="xmapdir"
fi

echo Source=$Source-

#
# prepare install destinations
#
  
if [ $Installerid != "root" ] || [ $Source = "xmapdir" ] ; then
#if [ $Installerid != "root" ] ; then
    Installdir_xmap=$HOME
    Installdir_map=$Installdir_xmap/maps
    Installdir_gpsd=$Installdir_xmap
    Installdir_gpsdini=$Installdir_xmap
else
    Installdir_xmap=/usr/local/mxmap
    Installdir_map=$Installdir_xmap/maps
    Installdir_gpsd=/usr/local/mxmap
    Installdir_gpsdini=/usr/local/mxmap
fi

#
# notify user about install conditions
#
    echo "Mayko mXmap install will be done as user '$Installerid'"
    echo "    - mXmap will be installed in directory '$Installdir_xmap'"
if [ $Installerid != "root" ] ; then
  if [ $Source != "xmapdir" ] ; then
    echo "    - Login as 'root' to install someplace else!"
  fi
fi

if [ -f "$Xmapbin" ] ; then
  if [ $Source != "xmapdir" ] ; then
  echo ""
  echo "    ! To install maps, please copy archive mxmaps* in directory '`pwd`'"
  echo "      and restart install script"
  fi
fi

if yesno "Continue installation? [y] " 0 ; then
      :
else
      exit 1
fi

if [ -f "$Xmapbin" ] ; then
	DisplayLicense "$License"
	echo ""
elif [ "$Source" = "cdrom" ] ; then
	DisplayLicense "$License"
	echo ""
fi

#
# --- prepare cdrom install
#	
selection ()
{
  msg="$1"
  def="$2"
  while true ; do
    echo " "
    $echonl "$msg"
    read answer
    if [ "$answer" ] ; then
      case "$answer" in
        y|Y|yes|YES)
          return 0
          ;;
        n|N|no|NO)
          return 1
          ;;
        *)
          echo " "
          echo "ERROR: Invalid response, expected $def"
          continue
          ;;
      esac
    else
      return $answer
    fi
  done
}
 
CdromPrepare ()
{
clear
cat <<EOF

        Mayko mXmap CDROM Install
        ------------------------

	1) Linux mXmap motif static  [libc5] SuSE, RedHat 4.x and similiar 
	2) Linux mXmap motif dynamic [libc5]

	3) Linux mXmap motif static  [glibc] RedHat 5.x and above
	4) Linux mXmap motif dynamic [glibc] 

        5) other versions

	q) for quit

EOF
#     selection "Your call : " "1 2 3 q"

TYPE=
MODE=

while :
do
	echo -n "        your call : "
    	read answer

	case "$answer" in
	1)
		TYPE=linux-x86-libc5
		MODE=static	
		;;
	2)
		TYPE=linux-x86-libc5
		MODE=dynamic	
		;;
	3)
		TYPE=linux-x86-glibc
		MODE=static	
		;;
	4)
		TYPE=linux-x86-glibc
		MODE=dynamic	
		;;
	5)
		TYPE=other
		Other_Platforms
		TYPE=
		;;
	q)
		exit
		;;
	*)
		echo "select number between 1 and 5"
		;;
	esac

	if [ "$TYPE" ] ; then
		break
	fi

done

echo "CDROM install selected type : $TYPE - $MODE"
GetFileNames $ScriptDirectory/etc
GetFileNames $ScriptDirectory/binaries/$TYPE

}

if [ "$Source" = "cdrom" ] ; then
	CdromPrepare
fi

#---- test dumps
if [ "$1" = "debug" ]; then
echo id=$Installerid
echo Installdir_xmap - Installdir_gpsd - Installdir_map -
echo $Installdir_xmap - $Installdir_gpsd - $Installdir_map -
echo $OSname - $OSrelease
echo $ScriptName - $CurrentDirectory - $ScriptDirectory

echo license - xmapbin - xmapini - xmaphlp - gpsd - gpsdini -
echo $License - $Xmapbin - $Xmapini - $Xmaphlp - $Gpsd - $Gpsdini -
echo - Mapdir - Mapstgz - Mapdb -
echo - $Mapdir - $Mapstgz - $Mapdb -
fi

installdir ()
{
  if [ ! -d $1 ] ; then
    if yesno "dir '$1' does not exist. create? [y] " 0 ; then
       echo "created directory $1"
       install -m 755 -d $1
       if [ ! -d $1 ] ; then
          echo "error creating directory '$1', installation aborted!"
	  exit 1
       fi
    else
       echo "installation aborted!"
       exit 1
    fi
  fi
}

installlink ()
{
    path=$1
    binary=$2
    linkdir=/usr/bin
    linkbin=$3

    if [ "$linkbin" = "" ] ; then
	linkbin=$binary    
    fi

    if [ $Installerid != "root" ] ; then
	return
    fi

    if yesno "  create link '$linkdir/$linkbin of $binary' ? [y] " 0 ; then
        ln -sf $path/$binary $linkdir/$linkbin
    fi
}

#
# validation
#
# --- print warning if inconsistencies are found
#	e.g. xmap but no ini and help file

#
# Xmap install
#
XmapFullinstall ()
{

installdir $Installdir_xmap

if [ -f "$Xmapbin" ] ; then
    MB_VERSION="mxmap"
    if [ -f $Installdir_xmap/mxmap ] ; then
        echo "rename existing $Installdir_xmap/mxmap -> mxmap.old" 	
        mv $Installdir_xmap/mxmap $Installdir_xmap/mxmap.old
    fi
    echo "install $Xmapbin $Installdir_xmap" 	
    install -m 755 $Xmapbin $Installdir_xmap
fi

if [ -f "$Xmapsbin" ] ; then
    MB_VERSION="mxmap-static"
    if [ -f $Installdir_xmap/mxmap-static ] ; then
        echo "rename existing $Installdir_xmap/mxmap-static -> mxmap-static.old" 	
        mv $Installdir_xmap/mxmap-static $Installdir_xmap/mxmap-static.old
    fi
    echo "install $Xmapsbin $Installdir_xmap" 	
    install -m 755 $Xmapsbin $Installdir_xmap
fi

#
# --- establish some necessary links
#
    installlink $Installdir_xmap start-mxmap mxmap

if [ $Installerid = "root" ] ; then
    if [ "$MODE" = "dynamic" ] ; then
        MB_VERSION="mxmap"
    elif [ "$MODE" = "static" ] ; then
        MB_VERSION="mxmap-static"
    fi

    CWD=`pwd`
    cd ${Installdir_xmap}
    if [ -f mxmap_link ] ; then  rm -f mxmap_link; fi
    ln -sf $MB_VERSION mxmap_link
    if [ -f link_to_mxmap ] ; then  rm -f link_to_mxmap; fi
    ln -sf $MB_VERSION link_to_mxmap
    cd $CWD
fi

if [ -f "$Xmapini" ] ; then
    if [ -f $Installdir_xmap/mxmap.ini ] ; then
        echo "rename existing $Installdir_xmap/mxmap.ini -> mxmap.ini.old" 	
        mv $Installdir_xmap/mxmap.ini $Installdir_xmap/mxmap.ini.old
    fi

#    if [ $Installerid != "root" && -f ${Xmapini}-user ] ; then
    if [ $Installerid != "root" ] ; then
        echo "install ${Xmapini}-user $Installdir_xmap/mxmap.ini" 	
        install -m 644 ${Xmapini}-user $Installdir_xmap/mxmap.ini
    else
        echo "install $Xmapini $Installdir_xmap" 	
        install -m 644 $Xmapini $Installdir_xmap
    fi
fi

if [ -f "$Xmapini-user" ] ; then
    if [ -f $Installdir_xmap/mxmap.ini-user ] ; then
        echo "rename existing $Installdir_xmap/mxmap.ini-user -> mxmap.ini-user.old" 	
        mv $Installdir_xmap/mxmap.ini-user $Installdir_xmap/mxmap.ini-user.old
    fi

        echo "install $Xmapini-user $Installdir_xmap" 	
        install -m 644 $Xmapini-user $Installdir_xmap
fi

if [ -f "$Xmaphlp" ] ; then
    if [ -f $Installdir_xmap/mxmap.hlp ] ; then
        echo "rename existing $Installdir_xmap/mxmap.hlp -> mxmap.hlp.old" 
        mv -f $Installdir_xmap/mxmap.hlp $Installdir_xmap/mxmap.hlp.old
    fi
    echo "install $Xmaphlp $Installdir_xmap" 
    install -m 644 $Xmaphlp $Installdir_xmap
fi

if [ -f "$Xmapscript" ] ; then
    if [ -f $Installdir_xmap/start-mxmap ] ; then
        echo "rename existing $Installdir_xmap/start-mxmap -> start-mxmap.old" 	
        mv $Installdir_xmap/start-mxmap $Installdir_xmap/start-mxmap.old
    fi
    echo "install $Xmapscript $Installdir_xmap" 	
    install -m 755 $Xmapscript $Installdir_xmap
fi

#
# license, readme install
#
    install -m 755 $Instscript $Installdir_xmap
    install -m 755 $Mapiscript $Installdir_xmap
    install -m 644 $License $Installdir_xmap
    install -m 644 README* $Installdir_xmap
    
#
# gpsd install
#
if [ -f "$Gpsd" ] ; then
    if [ -f $Installdir_gpsd/gpsd ] ; then
        echo "rename existing $Installdir_gpsd/gpsd -> gpsd.old" 
        mv $Installdir_gpsd/gpsd $Installdir_gpsd/gpsd.old
    fi
    echo "install $Gpsd $Installdir_gpsd" 
    install -m 755 $Gpsd $Installdir_gpsd
    installlink $Installdir_gpsd gpsd
fi

if [ -f "$Gpsdini" ] ; then
    echo gpsd-0.93 does not use any gpsd.ini
fi
}

XmapUserInstall ()
{

if [ -f ${HOME}/mxmap.ini ] ; then
 
  if yesno "File '${HOME}/mxmap.ini' exist. Install anyway? [n] " 1 ; then
        echo "rename existing $HOME/mxmap.ini -> mxmap.ini.old" 	
        mv $HOME/mxmap.ini $HOME/mxmap.ini.old
  else
      exit 1
  fi

fi
installdir $Installdir_xmap
echo "install $Xmapini-user $Installdir_xmap/mxmap.ini" 	
install -m 644 $Xmapini-user $Installdir_xmap/mxmap.ini

}

#
# maps install
#

MaptgzInstall ()
{
  mapa="$1"
  if yesno "maps archive '$mapa' found. install? [y] " 0 ; then
    if [ -d "$Installdir_xmap/maps" ] ; then
	echo "Hoops, map directory '$Installdir_xmap/maps' already exists!!!"
	echo "Install of '$mapa' is canceled"
	return
    fi
    echo "install Map archive $mapa" 
    installdir $Installdir_xmap
    CWD=`pwd`
    cd $Installdir_xmap
    tar -xvzf $mapa
    cd $CWD
    if [ -f "$Installdir_xmap/maps/install*" ] ; then
	rm $Installdir_xmap/maps/install*
    fi
  fi
}

MapdirInstall ()
{
  mdir="$1"
  if yesno "install maps in '$mdir' to '$Installdir_map'? [y] " 0 ; then
    if [ -d "$Installdir_map" ] ; then
	echo "Hoops, map directory '$Installdir_map' already exists!!!"
	echo "Install of '$mdir' is canceled"
	return
    fi
    installdir $Installdir_map
    echo "install Maps of '$mdir'" 
#    install -m 644 -d * $Installdir_map
#    cp -rpv * $Installdir_map
    cp -rv * $Installdir_map
    if [ -f "$Installdir_map/install*" ] ; then
	rm $Installdir_map/install*
    fi
  fi
}

MapdirLink ()
{
  mlink="$1"
  if [ -d $Installdir_xmap/maps ] ; then
    echo "Link not created, directory '$Installdir_xmap/maps' already exist"
  elif [ -f $Installdir_xmap/maps ] ; then
    echo "Link not created, file '$Installdir_xmap/maps' already exist"
  else
    echo "create link '$Mapdir' -> '$Installdir_xmap'"
    ln -s $Mapdir $Installdir_xmap/maps
  fi
}


MapFullinstall ()
{
#old if [ -d "$Mapdir" ] ; then
#if [ "$Source" = "cdrom" ] ; then

clear
cat <<EOF

	Map CDROM Install
	-----------------

        Select the set of maps you like to have copied 
        to the local drive.

	a) built links to all maps on cdrom   <  0.1Mb
	b) several raster maps                ~210.0Mb
	c) US majorroads vector maps          ~250.0Mb
	d) both (b+c)                         ~460.0Mb

	q) for quit

Note: Creating links to the distribution CD
      requires the CD mounted prior to 'Mayko mXmap' start!!

EOF

RMAPS=link
VMAPS=link

while :
do
	echo -n "        your selection : "
	read answer

	case "$answer" in
	a)
		RMAPS=link
		VMAPS=link
		break
		;;
	b)
		RMAPS=copy
		break
		;;
	c)
		VMAPS=copy
		break
		;;
	d)
		RMAPS=copy
		VMAPS=copy
		break
		;;
	q)
		exit
		;;
	*)
		echo "select a letter from the menu above"
		;;
	esac
done

	MapCLinstall $RMAPS $VMAPS $Mapdir
}

#
# copy map.db + *.des file
# copy or link rmaps + vmaps as selected
#
MapCLinstall ()
{
f_rmaps=$1
f_vmaps=$2

if [ -f $Installdir_map/map.db ] ; then

  if yesno "Directory '${Installdir_map}' exists. Install anyway? [n] " 1 ; then
      echo "rename existing $Installdir_map/map.db -> map.db.old" 	
      mv $Installdir_map/map.db $Installdir_map/map.db.old
  else
      exit 1
  fi
fi

echo " ... prepare Mayko mXmap map enviroment"
if [ ! -d ${Installdir_map} ] ; then
     mkdir ${Installdir_map}
fi


CWD=`pwd`
cd ${Mapdir}

for i in * ; do

#    echo "copy $i - ${Installdir_map}/${i}-"

    if [ -L ${Installdir_map}/${i} ] ; then
       rm ${Installdir_map}/${i}
    fi

      case $i in
        map.db)
		echo "copy $i -> ${Installdir_map}/${i}"
		cp $Mapdir/map.db ${Installdir_map}
		;;
        *.des)
		echo "copy $i -> ${Installdir_map}/${i}"
		cp $i ${Installdir_map}
		;;
        mxmap.obj)
		echo "copy $i -> ${Installdir_map}/${i}"
		cp $i ${Installdir_map}
		;;
        vmap)					# install vector maps
		if [ "$f_vmaps" = "copy" ] ; then
		    echo "copydir $i -> ${Installdir_map}/${i}"
		    cp -rv $i ${Installdir_map}
		else
		    LinkCheck ${Installdir_map}/${i}
		    echo "link ${Mapdir}/${i} -> ${Installdir_map}/${i}"
		    ln -sf ${Mapdir}/${i} ${Installdir_map}
		fi
		;;
        *)					# install raster maps
		if [ "$f_rmaps" = "copy" ] ; then
		    echo "copy $i -> ${Installdir_map}/${i}"
		    cp  $i ${Installdir_map}
		else
		    LinkCheck ${Installdir_map}/${i}
		    echo "link ${Mapdir}/${i} -> ${Installdir_map}/${i}"
		    ln -sf ${Mapdir}/${i} ${Installdir_map}
		fi
		;;
      esac

done
cd $CWD

}

LinkCheck ()
{
link=$1

    if [  -d ${link} ] ; then
	echo "Error: Could not build link. Directory '${link}' already exist"
	exit
#
# --- overwriting existing files works !!!!!
#    elif [  -f ${link} ] ; then
#	echo "Error: Could not build link. File '${link}' already exist"
#	exit
    fi
}
#
# copy map.db + *.des file and maps
#
MapCopyInstall ()
{
if [ -f $Installdir_map/map.db ] ; then

  if yesno "Directory '${Installdir_map}' exists. Copy anyway? [n] " 1 ; then
      echo "rename existing $Installdir_map/map.db -> map.db.old" 	
      mv $Installdir_map/map.db $Installdir_map/map.db.old
  else
      exit 1
  fi
fi

echo " ... prepare Mayko mxmap map enviroment"
if [ ! -d ${Installdir_map} ] ; then
     mkdir ${Installdir_map}
fi


CWD=`pwd`
cd ${Mapdir}

for i in * ; do

    echo "copy - $i - ${Installdir_map}/${i}-"

    if [ -L ${Installdir_map}/${i} ] ; then
       rm ${Installdir_map}/${i}
    fi

      case $i in
        map.db)
		cp $Mapdir/map.db ${Installdir_map}
		;;
        *.des)
		cp $i ${Installdir_map}
		;;
        mxmap.obj)
		cp $i ${Installdir_map}
		;;
        vmap)
		cp -rv $i ${Installdir_map}
		;;
        *)
		cp $i ${Installdir_map}
		;;
      esac

done
cd $CWD

}

#
# copy map.db + *.des file, do link on maps
#
MapLinkInstall ()
{
if [ -f $Installdir_map/map.db ] ; then

  if yesno "Directory '${Installdir_map}' exists. Install? [n] " 1 ; then
      echo "rename existing $Installdir_map/map.db -> map.db.old" 	
      mv $Installdir_map/map.db $Installdir_map/map.db.old
  else
      exit 1
  fi
fi

echo " ... prepare Mayko mxmap map enviroment for user"
if [ ! -d ${Installdir_map} ] ; then
     mkdir ${Installdir_map}
fi


CWD=`pwd`
cd ${Mapdir}

for i in * ; do

    echo "- $i - ${Installdir_map}/${i}-"

    if [ -L ${Installdir_map}/${i} ] ; then
       rm ${Installdir_map}/${i}
    fi

      case $i in
        map.db)
		cp $Mapdir/map.db ${Installdir_map}
		;;
        *.des)
		cp $i ${Installdir_map}
		;;
        mxmap.obj)
		cp $i ${Installdir_map}
		;;
        *)
		ln -sf $i ${Installdir_map}
		;;
      esac

done
cd $CWD

}

#
#------
#

if [ "$Source" = "xmapdir" ] ; then	# user install from /usr/local/mxmap
	XmapUserInstall
#	MapLinkInstall
	MapCLinstall link link $Mapdir
else 					# install from CD or tgz
	XmapFullinstall

        if [ -f "$Mapstgz" ] ; then	# maps.tgz archive found
	    MaptgzInstall $Mapstgz
        elif [ -f "$Mapdb" ] ; then	# unpacked maps.tgz archive
	    MapdirInstall $ScriptDirectory
        elif [ "$Source" = "cdrom" ] ; then 
	    MapFullinstall		# must be a map directory
	else
	    echo " "
	    echo " No maps found to install!!!!!!"
	    echo " "
        fi
fi


# ---- eos

