#!/bin/sh
#
# Install/update map data for Mayko mXmap V1.0
Version=1.0.1
#
##########################################################################
#
# This script will install/add/update map data for Mayko mXmap
#
#   examples                            source          destination
#
usage()
{
cat <<EOF
usage():
 install-mapdata [-s|-u| [srcdir [destdir]] ]

 install-mapdata -u                     ;# /usr/local/mxmap/maps $HOME/maps
 install-mapdata -s [srcdir]            ;# $srcdir               /u/l/mxmap/maps
 install-mapdata                        ;# currentdir            $HOME/maps
 install-mapdata srcdir [destdir]       ;# $srcdir               $destdir

       -s       system wide map install 
                  files copied/linked to /usr/local/mxmap/maps
       -u       user map update 
                  link maps from /usr/local/mxmap/maps and integrate in 
                  in user home map directory 
                  ! maps will not be copied. a symbolic is established.

EOF
}

#---------------------------------------------------------------------------
#
# a) User map update from system wide install 
#
# 	 	'install-mapdata -u'
#
#	map update from	/usr/local/mxmap/maps. links to maps will be established
#
# b) System wide install
#
# 	 	'install-mapdata -s [sourcedir]'
#
#	 maps install from source to /usr/local/mxmap/maps
#
# 	!!! if mapdata installed in '/usr/local/mxmap/maps',
#	    don't forget to advise users to run 'install-mapdata -u'
#	    to modfiy the users 'map.db'
#
# c) User install
#
# 	 	'install-mapdata [sourcedir]'
#
#	maps will be installed from source to ~user/maps
#
# d) Enjoy
#
#
##########################################################################

#-------------------------------------------------------------------------
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
}
 



#
# Get the map filenames
#

GetFileNames ()
{
for i in * ; do
  if [ -f "$i" ] ; then
    case $i in
      *.xpm|*.xpm.gz)
        MAP="$i"
	Install_map XPM $i
        ;;
      *.tif)
	Install_map TIFF $i
        ;;
      *.tfw)
        ;;
      *.bsb)
	Install_map_bsb BSB $i
        ;;
      *.kap)
        ;;
      *.des)
        ;;
      map.db)
        ;;
      mxmap.obj)
        ;;
      install*)
        ;;
      update*)
        ;;
      *)
	echo invalid $i
        ;;
    esac
  elif [ -d "$i" ] ; then
	:
#	echo "directory are not supported '$i'"
  else
	echo "unknown data found '$i'"
  fi
done
}

Install_map ()
{
	MAPTYPE=$1
	MAPFILE=$2

#        MAPNAME=`echo "$MAPFILE" | awk -F . '{print $1}'`
        MAPNAME=`echo "$MAPFILE" | awk -F ".gz" '{print $1}' \
                                 | awk -F ".xpm" '{print $1}' \
                                 | awk -F ".tif" '{print $1}'`
	MAPDES=$MAPNAME.des
	DBNAME=$MAPNAME

#
# --- Is there a description file for this map?
#
	mdes=`grep ^$MAPNAME *.des 2>/dev/null \
		| head -1 \
		| awk -F : '{print $1}'`

#echo " mapfile: $MAPFILE mapname: '$MAPNAME' mapdes : -$mdes-"

	if [ "$mdes" != "" ] ; then
#	    echo "new mapdes name is: '$mdes'"
	    MAPDES=$mdes

# ---     use DBNAME from des file
	    DBNAME=`head -1 $mdes`

# --old-  is there a mapd.db on cdrom?	
#	    if [ -f $MAPSOURCE/map.db ] ; then
#		mdb_entry=`grep ^$mdes $MAPSOURCE/map.db`
#		if [ "$mdb_entry" != "" ] ; then
#	    	    echo "new mapname is: '$newname'"
#		    newname=`echo "$mdb_entry" \
#			     | awk '{print $2 " " $3 " " $4 " " $5}'`
#		    if [ "$newname" != "" ] ; then
#	    	    	echo "new mapname is: '$newname'"
#	    	        DBNAME=$newname
#		    fi
#		fi
#	    fi

	fi
 
	if [ -f $MAPDEST/$MAPFILE ] ; then
	    echo "mapfile already exists '$MAPDEST/$MAPFILE'"
	    return
	fi
	if [ -f $MAPDEST/$MAPDES ] ; then
	    echo "map description already exists '$MAPDEST/$MAPDES'"
	    return
	fi

#
# --- create map description file (*.des)
#
	if [ -f $MAPSOURCE/$MAPDES ] ; then
	    cp $MAPSOURCE/$MAPDES $MAPDEST
	else
	    Create_des $MAPFILE $MAPTYPE $MAPNAME "$DBNAME"
	fi

#
# --- do map.db entry
#
	Create_mapdb_entry $MAPNAME.des "$DBNAME"

#
# --- copy/link map
#
	if [ "$MMODE" = "link" ] ; then
	    ln -s $MAPSOURCE/$MAPFILE $MAPDEST
	else
	    cp $MAPFILE $MAPDEST
	fi

	echo "map installed '$MAPFILE' ($MAPTYPE) using name '$MAPNAME'"

}

Install_map_bsb ()
{
	MAPTYPE=$1
	MAPFILE=$2

	MAPDES=$2
        MAPNAME=`echo "$MAPFILE" | awk -F ".bsb" '{print $1}'`

	DBNAME=$MAPNAME

#
# --- Is there a description file for this map?
#
	mdes=`grep ^CHT $MAPDES 2>/dev/null \
		| awk -F = '{print $2}' \
		| awk -F , '{print $1}'`

	if [ "$mdes" != "" ] ; then
	    DBNAME=$mdes
	fi

#
# --- check if files already exist at destination
# 
	if [ -f $MAPDEST/$MAPDES ] ; then
	    echo "map description already exists '$MAPDEST/$MAPDES'"
	    return
	fi

for n in $MAPNAME* ; do
  if [ -f "$n" ] ; then
    case $n in
      *.bsb)
        ;;
      *.kap)
	if [ -f $MAPDEST/$n ] ; then
	    echo "mapfile already exists '$MAPDEST/$n'"
	    return
	fi
	;;
      *)
        ;;
    esac
  fi
done

#
# --- create map description file (*.des)
#
	if [ -f $MAPSOURCE/$MAPDES ] ; then
	    cp $MAPSOURCE/$MAPDES $MAPDEST
#	else
#	    Create_des $MAPFILE $MAPTYPE $MAPNAME "$DBNAME"
	fi

#
# --- do map.db entry
#
	Create_mapdb_entry $MAPNAME.bsb "(BSB) $DBNAME"

#
# --- copy/link map
#

for n in $MAPNAME* ; do
  if [ -f "$n" ] ; then
    case $n in
      *.kap)
	if [ "$MMODE" = "link" ] ; then
	    ln -s $MAPSOURCE/$n $MAPDEST
	else
	    cp $n $MAPDEST
	fi
	;;
    esac
  fi
done

	echo "map installed '$MAPFILE' ($MAPTYPE) using name '$MAPNAME'"

}

Create_des ()
{
	filename=$1
	type=$2
	desname=$3
	dbname=$4
	desfile=$MAPDEST/$desname.des

	echo "${dbname}" >$desfile
	if [ "$type" = "XPM" ] ; then
	    echo "${dbname}" >$desfile
	    echo "${desname}" >>$desfile
	    echo "LL, 0, 0" >>$desfile
	else
	    echo "${filename}" >>$desfile
	    echo "DRG" >>$desfile
	fi

#--- we don't have calibration data, so use default
	echo "000000N0000000W, 0, 0" >>$desfile
	echo "000000N0000000W, 0, 0" >>$desfile
}

Create_mapdb_entry ()
{
	map=$1
	dbname=$2

	echo "${map} ${dbname}" >>$MAPDB
}

Imageusage_simple ()
{
	dspace=`du -s --kilobyte ./ | awk '{print $1}'`
	echo " "
	echo "      required diskspace (in kbytes): $dspace"
	echo " "
	
}

Imageusage ()
{
#	dspace=`du -s --kilobyte *.xpm* *.tif | awk '{print $1}'`

total=0

for i in * ; do
  if [ -f "$i" ] ; then
    case $i in
      *.xpm|*.xpm.gz|*.tif|*.tfw|*.bsb|*.kap)
	fsize=`du --kilobyte $i | awk '{print $1}'`
        total="$[ $total + $fsize ]"
        ;;
      *)
        ;;
    esac
  fi
done


	echo " "
	echo -n "     required diskspace (.xpm .tif .bsb): $total"
	echo " kbytes"
	echo " "

}

#########################################################################
#
# --- the script starts here
#

#
# --- 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 [ `echo "x\c"` = "x" ] ; then
  echonl=echoc
else
  echonl=echon
fi

#
# --- set defaults
#

MXMAPDIR=/usr/local/mxmap
MAPSOURCE=`pwd`
MAPDEST=$HOME/maps
INSTALLMODE=

#
#---- check commandline parameter
#
if [ $# -gt 2 ] ; then usage; exit 1; fi

while test $# -ne 0; do

#param=$*
#echo "no=$# :pl: $param ::"

    case "$1" in

'-s')
	INSTALLMODE=system
	MAPDEST=$MXMAPDIR/maps
        ;;

'-u')
	INSTALLMODE=user
	MAPSOURCE=$MXMAPDIR/maps
	if [ $# -gt 1 ] ; then usage; exit 1; fi
        ;;

'-v')
        VERBOSE="$VERBOSE v"
        ;;

'-h'|'-help')
	usage
        exit 1
        ;;

-*)
        echo "error: unknown parameter"
        usage
        exit 1
        ;;

*)
	MAPSOURCE=$1
	if [ $# -gt 1 ] ; then 
		MAPDEST=$2
		shift
	fi
        ;;

    esac
    shift
done
 

#
# --- prepare install destinations
#

    if [ ! -d $MAPSOURCE ]; then
	echo "Error: directory not found: '$MAPSOURCE'"
	exit 13
    fi
    if [ ! -d $MAPDEST ]; then
	echo "Error: directory not found: '$MAPDEST'"
	exit 13
    fi
    oldir=`pwd`
    cd $MAPSOURCE
    MAPSOURCE=`pwd`
    cd $MAPDEST
    MAPDEST=`pwd`
    cd $oldir


    MAPDB=$MAPDEST/map.db
#
# --- correct bug from old mapdata distribution
#
    if [ ! -d $MAPDB ]; then
	chmod u+w $MAPDB
    fi

#
# --- notify user about install conditions
#

cat <<EOF

        Mayko mXmap Map updater
        -----------------------
EOF

#  if [ ! "$Coption" ] ; then
#    echo " "
#    echo "!!!! You have not given a path on the command line."
#    echo "     To select a map directory please type '$ScriptName /map/dir'"
#  fi

    echo " "
    echo "                            Installer ID: '$Installerid'"
    echo "             Maps will be installed from: '$MAPSOURCE'"
    echo "                            To directory: '$MAPDEST'"


    cd $MAPSOURCE

#
# --- copy or link
#
#	!!! if map source is '/usr/local/mxmap' ==> create links
#

MMODE=     

if [ "$MAPSOURCE" != "/usr/local/mxmap/maps" ]; then

    Imageusage

cat <<EOF

        c) copy maps 
        l) create symbolic links for maps
              Note: Creating links requires the CD mounted 
                    prior to 'Mayko mXmap' start!!

        q) quit 

EOF

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

        case "$answer" in
        c)
                MMODE=copy     
                ;;
        l)
                MMODE=link     
                ;;
        q)
                exit
                ;;
        *)
                echo "select a letter from the menu above"
                ;;
        esac

        if [ "$MMODE" ] ; then
                break
        fi
done

else
	MMODE=link     
	if yesno "continue? [y] " 0 ; then
	      :
	else
	      exit 1
	fi
fi

#
# --- check enviroment
#

#    if [ ! -d $MXMAPDIR ] ; then
#	echo "ERROR: could not find '$MXMAPDIR'"
#	echo "you need to install Mayko mXmap first"
#	exit 99
#    fi


if [ ! -d ${MAPDEST} ] ; then

	if yesno "Map directory not found '$MAPDEST: create? [y] " 0 ; then
	      :
	else
	      exit 1
	fi
	
	mkdir ${MAPDEST}
fi


#
# --- install maps
#
    GetFileNames 

# --- the end my friend

