#!/bin/sh
#
# RCS Version Control. Do not change, as this is updated
# by any invocation of rcs-tools:
# $Id: am_tool,v 1.43 1997/11/26 00:00:31 smurphy Exp $
# $Revision: 1.43 $
#
# File Name & location:
# /usr/local/bin/am_tool
#
# Short description:
# Answering Maschine tool. Handles all listening options
#
#>>> AIT kit Version 1.0 "new_file" V1.4, Fri the 21 Feb 1997
#>>> Build Host is "sirius" running "Linux 2.0.29" 
#>>> (c) J. Mertin <smurphy@linux.de>
#>>> Author: Joerg Mertin, mail: <smurphy@linux.de>
#
# *** Do not modify if you are unsure about what you're doing !
# *** Some changes may destroy your HARDWARE, so be WARNED !
#
# Functions to be written:
# am_ls: lists voice messages in chronological order (time)
#        * Done.
# am_rm: deletes specific files. Should be handled smoothly, in fact
#        a way of making it interactively or with a time-stamp
#        function would be great.
#        * Implemented, should be tested in regard to the voice-files.
# am_mv: Moves the voice message to a backup-directory. Should be handled
#        by a configuration file (Global), or on user-base.
#        * Done, used in the "do_what" function.
# am_play: Plays the voice message. 2 Options, over the integrated
#          soundcard or the Voice-modem speaker.
# 
# am_playall: Plays the voice messages, if an option is given, from
#             the last listening (timestamp), else all, or from the
#             time entered
#             * Easyest to do. For-statement.
# am_rec: Implementation of a Voice-recording unit. Works quite good.
#
# ---------------------------User-configurable-part------------------
#
# Voice directory
VOICE_DIR=/var/spool/voice
#
# Modem device, only trailing name, e.g. ttyS0
DEVICE=ttyS4
#
# Lock-file directory
LOCK_DIR=/var/lock
#
# Incoming directory containing the voice-files in adpcm3 format.
INCOMING_DIR=${VOICE_DIR}/incoming
#
# Set default directory for files to be listened at.
PLAY_DIR=${INCOMING_DIR}
#
# Message Directory, for playback
MSG_DIR=${VOICE_DIR}/messages
#
# Directory to keep the backups in
OLD_DIR=${VOICE_DIR}/old
#
# Time-Flag/stamp used to check files
FLAG="${VOICE_DIR}/.timestamp"
#
# Server all this should run on. Should be on the same subnet.
SERVER=stargate
#
# Voice file format. New format is from the form: *.rmd, old where voc-*
FORMAT="*.rmd"
#
# Modem type
MODEM=ZyXEL_1496
#
# Voice Encoding method 1,2,3 or 4, depeding on the modems
REC_CMP=3
#
#--------------------------------------------------------------------
#
#                   *** Nothing to be changed below !!! ***
#
# ---------------------------Config----------------------------------
#
# Path to search for binary/executable files.
PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#
# Programm version
VER="$Revision: 1.43 $"
#
# Belonging to AIT-Version
AIT_V=1.0
#
# Programm name
PROGNAME=`basename $0`
#
# How we where called with all arguments
CALL="`basename $0` $*"
#
# output format of the list comand
FORMAT_LS="-printf \"%f from %Ta, %Tb %Td at %TH:%TM, with size in Kb: %k \n\""
#
# output format of the del comand
FORMAT_DEL=" -printf \"Delete %f: Time: %TH:%TM, size: %k Kb\n\" "
#
# Information on the local host
HOSTNAME=`hostname -s`
#
# Local Directory
LOC_DIR=`pwd`
#
# Horrible Hack to keep track of the Directory we where in.
if [ -f "${HOME}/.am_toolrc" ] ; then
  PLAY_DIR="`cat ${HOME}/.am_toolrc`"
  LOC_DIR="`cat ${HOME}/.am_toolrc`"
else
  echo "${LOC_DIR}" | cat > ${HOME}/.am_toolrc
fi
#
# ---------------------------------Functions-------------#---------------------
#
#
# who: Version Control. Tells who did all this work :)
who() {
echo
echo ">>> AIT ${AIT_V} - Auto Install Tools <<<"
echo
echo "   \"$PROGNAME\" Version ${VER}, Thu the 24 Feb 1997"
echo "    by (c)  J. Mertin <smurphy@linux.de>"
echo "    Author: J. Mertin <smurphy@linux.de>"
echo
}
#
# -----------------------------------------------------------------------------
#
# Functions for the Display of the help-messages.
# 
option_a() {
echo "Options: -a        : applies to all files."
}
option_h() {
echo "         -h        : print this help text and exits"
}
option_l() {
echo "         -l        : local diretory is used as reference for"
echo "                     file operations"
}
option_v(){
echo "         -v        : print version"
}
option_n() {
echo "         -n <hours>: all voice messages newer than <hours>"
echo "                     are affected"
}
option_o() {
echo "         -o        : all is applied to the voice messages "
echo "                     situated in the \"old\" directory"
}
option_f() {
echo "         -f        : force the command. Deactivates interactive mode"
}
option_d() {
echo "         -d        : delete the actual voice message"
}
#
# -----------------------------------------------------------------------------
#
# Shows the Usage functionality.
Usage() {
#
echo
echo "Usage: ${PROGNAME} [-h] | [-v]"
#
#
if [ "${PROGNAME}" = "am_tool" ]; then
    echo "       ${PROGNAME} is a tool to listen to your voice"
    echo "       messages recorded by a voice-capable modem"
    echo "       configured as an answering maschine."
    echo
    echo "       The following commands exist:"
    echo "       am_ls:      lists the existing voice files"
    echo "       am_rm:      deletes interactively voice messages"
    echo "       am_mv:      moves interactively voice messages to"
    echo "                   ${OLD_DIR}"
    echo "       am_play:    plays interactively voice messages using the"
    echo "                   soundcard or the voice modem internal speaker"
    echo "       am_playall: play all files situated in the"
    echo "                   ${INCOMING_DIR}"
    echo "       am_rec:     Interactive message tool, message recording"
    echo
    echo "       For help on a specific command,"
    echo "       just type \"<command> -h\"."
elif [ "${PROGNAME}" = "am_ls" ]; then
    echo "       ${PROGNAME} [ -a | -n <hours> ] [ -o ]"
    echo
    echo "Description: \"${PROGNAME}\" lists all voice messages situated in"
    echo "             the \"`basename ${PLAY_DIR}`\" directory with formatted output"
    echo
    option_a
    option_h
    option_n
    option_o
    option_v
    echo
    echo "         Without any option, all voice messages will be listed"
elif [ "${PROGNAME}" = "am_rm" ]; then
    echo "       ${PROGNAME} [ [-f] -a | -n <hours> ] [ -o ]"
    echo
    echo "Description: \"${PROGNAME}\" deletes voice messages situated in"
    echo "             the \"`basename ${PLAY_DIR}`\" directory"
    echo
    option_a
    option_h
    option_f
    echo "                     Think twice before using this option !!!"
    option_n
    option_o
    option_v
    echo
    echo "         By default, interactive mode is set"
elif [ "${PROGNAME}" = "am_mv" ]; then
    echo "       ${PROGNAME} [ [-f] -a | -n <date> ]"
    echo
    echo "Description: \"${PROGNAME}\" moves voice messages situated in"
    echo "             the \"`basename ${PLAY_DIR}`\" directory to the \"old\" directory"
    echo
    option_a
    option_h
    option_f
    option_n
    option_v
    echo
    echo "         By default, interactive mode is set"
elif [ "${PROGNAME}" =  "am_play" ] || [ "${PROGNAME}" =  "am_playall" ]; then
    echo "       ${PROGNAME} [ -s | -m ] [ -a | -n <date> ] [ -o | -l ]"
    echo
    option_a
    option_h
    option_f
    option_l
    echo "         -m        : uses the modem-speaker as audio device"
    option_n
    option_o
    echo "         -s        : uses the soundcard as audio device"
    option_v
    echo
    echo "         By default, the soundcard audio device is used and all"
    echo "         files newer than ${FLAG} are played"
    echo
    echo "         If invoqued as \"am_playall\" all voice messages"
    echo "            are sent to the audio device"
    echo "         If invoqued in interactive mode, type h for supplementary"
    echo "            features."
elif [ "${PROGNAME}" = "am_rec" ]; then
    echo "       ${PROGNAME} [ -f <file> ]"
    echo
    echo "Description: \"${PROGNAME}\" Creates an adpcm3 coded voice file in"
    echo "              the local directory. This file can be used for the"
    echo "              Zyxel modem voice capability. Copy this file into the "
    echo "              /var/spool/voice/messages directory and update " 
    echo "              the Index file situated in the same location "
    echo "              for the vgetty to know what message to play"
    echo
    echo "Options:"
    echo "         -f <file> : name you want to give the message"
    option_h
    option_v
    echo
    echo "         If \"-f <file>\" is not specified, it will be asked"
    echo
fi
echo
}
#
# -----------------------------------------------------------------------------
#
Cleanrc(){
    # We are on a Client, invoque the program on the Server
    rm_flag
    echo "Removing stale file"
    rm -f ${HOME}/.am_toolrc
    echo
}
#
# interactive file Handling.
#
dowhat_msg() {
#
# What do we want to do ?
#
if [ "${FORCE}" != "true" ]; then
# We want interactive mode
while :;
  do
   # Tell what's going on. If we're are playing new or old messages
   echo -n "$LocMessage [h for help]: "
   read DO_What
   Action=${DO_What}
    if [ "${Action}" = "s" ] && [ ! ${NoTimeStamp} ]; then
	echo -n "Saving: "
	mv -f ${VOICE_FILE} ${OLD_DIR}
	echo "done."
	return 0
    elif [ "${Action}" = "d" ]; then
	echo -n "Deleting `basename ${VOICE_FILE}`: "
	rm -f ${VOICE_FILE}
	echo done.
	return 0
    elif [ "${Action}" = "p" ]; then
	echo
	listen_msg
	echo
    elif [ "${Action}" = "q" ]; then
	echo "User quit requested. Exiting..."
	Cleanrc
	exit 0
    elif [ "${Action}" = "h" ]; then
	echo
        echo ">>> \"${PROGNAME}\" Version ${VER}, Thu the 24 Feb 1997"
	echo ">>> The following commands are supported:"
	echo
	if [ "${SoundCard}" != "disabled" ]; then
	  echo "    A: sets soundcard as playback device"
	fi
	echo "    a: show message information for all voice messages"
	echo "    c: show information on the current voice message"
	echo "    d: delete current voice message"
	echo "    h: shows this help"
	if [ "${ModemPlay}" != "disabled" ]; then
	  echo "    m: sets modem as playback device"
	fi
	echo "    n: skip current voice message"
	echo "    p: playback current voice message"
	echo "    q: exits"
	echo "    r: rename file"
	if [ ! ${NoTimeStamp} ]; then
	  echo "    s: saves the current voice message to ${OLD_DIR}"
	fi
	echo "    u: update timestamp"
	echo "    v: version information"
	echo
	echo " The sound card is always set as default playback"
	echo " device if ${PROGNAME} is invoqued without the -m flag"
	echo " or if the modem is locked."
	echo
    elif [ "${Action}" = "r" ]; then
	mv_inter
	return 0
    elif [ "${Action}" = "v" ]; then
	who
    elif [ "${Action}" = "n" ]; then
	echo
	echo "Skipping current voice message"
	return 0
	echo
    elif [ "${Action}" = "m" ] && [ "${ModemPlay}" != "disabled" ]; then
	echo
	echo -n "Enabling modem as playback device: "
	modem_chk
	echo
    elif [ "${Action}" = "c" ]; then
	echo
	file_list
	echo
    elif [ "${Action}" = "A" ] && [ "${SoundCard}" != "disabled" ]; then
	echo
	echo "Enabling audio card as playback device"
	Modem=disabled ;
	echo
    elif [ "${Action}" = "a" ]; then
	echo
        TMP_FLAG=${F_FLAG}
	files_list
        F_FLAG=${TMP_FLAT}
	echo
    elif [ "${Action}" = "u" ]; then
	echo
	echo -n "Updating timestamp information: "
	touch ${FLAG}
	echo "done."
	echo
    else
	echo "*** Unknown or wrong argument, use \"h\" for help"
    fi
done
fi
}
#
# -----------------------------------------------------------------------------
#
# Separate functions needed to be run in one instance on the Server-maschine.
#
# Sets the mixer level.
#
mixer_set() {
setmixer pcm 80
}
# 
# Listen to a voice-file through the soundcard
#
listen_msg() {
#
# Trap Ctrl-C
trap "" SIGINT
if [ "${Modem}" = "enabled" ]; then
   if [ -f  ${LOCK_DIR}/LCK..${DEVICE} ];then
     echo "modem is locked... "
     echo "Try again later or enable soundcard as playback device"
     return 1
   fi
   echo -n "Listening to `basename ${VOICE_FILE}`. [<Strg>-C to interrupt]: "
   # Effective Modem playback.
   ${PLAY} ${VOICE_FILE}
    vm beep
   # removing lock-file
elif [ "${SoundCard}" != "disabled" ]; then
   echo -n "Listening to `basename ${VOICE_FILE}`. [<Strg>-C to interrupt]: "
    rmdtopvf ${VOICE_FILE} | pvfspeed -s 8000 | pvftolin | bplay -b 8 &> /dev/null
    echo -ne "\adone."
fi
}
#
# lists a specific file as we would need it, formatted output.
#
file_list() {
  find ${VOICE_FILE} ${F_FLAG} -printf "%f from %Ta, %Tb %Td at %TH:%TM, with size in Kb: %k\n" | sort -k 8,9
}
#
# lists voice-files on the Server system.
#
files_list() {
  find ${PLAY_DIR} -name "${FORMAT}" -type f ${F_FLAG} -printf "%f from %Ta, %Tb %Td at %TH:%TM, with size in Kb: %k\n" | sort -k 8,9
}
#
# Delete specified files on the Server system:
#
del_files() {
  find ${PLAY_DIR} -name "${FORMAT}" -type f ${F_FLAG} -printf "\nDelete %f: Time: %TH:%TM, size: %k Kb: " ${Interactive} rm -f {} \;
echo
}
#
# Saves specified files on the Server system in the "old" Directory:
#
mv_files() {
#
find ${PLAY_DIR} -name "${FORMAT}" -type f ${F_FLAG} -printf "\nSave %f: Time: %TH:%TM, size: %k Kb: " ${Interactive} mv -f {} ${OLD_DIR} \;
echo
}
#
modem_chk() {
 # Check if zplay or vm is used
if [ -f  ${LOCK_DIR}/LCK..${DEVICE} ];then
     echo "failed !"
     echo "Modem is locked... Falling back to soundcard as playback device."
     return 1
fi
if [ ! -z $(type -path vm) ]; then
   PLAY="vm play " ;
   VM=true
   Modem=enabled ;
   echo "succeded."
else
   Modem=disabled ;
   echo "failed !"
   echo "No known tool to enable modem play-back detected" ;
   echo "Disabling modem play-back, audio card activated." ;
fi
}
#
audio_chk() {
# Checks if an audio device exists.
# Horrible hack, but I dont know of a better way to find
#   out about all different soundcard drivers.
#
cat /dev/sndstat &> /dev/null
errlvl=$?
#
echo
if [ $errlvl -gt 0 ] 
  then
    echo "*** No soundcard found. Disabled."
    SoundCard=disabled
    if [ "${PROGNAME}" = "am_rec" ]; 
    then
      echo "*** Fatal Error: A soundcard is required for recording"
      echo "***              capabilities."
      echo "*** Exiting !"
      Cleanrc
      echo
      exit 1
    fi
else
    echo "Soundcard detected. Good !"
    SoundCard=enabled
    mixer_set
fi
if [ -z $(type -path zplay) ] && [ -z $(type -path vm) ]
  then
    echo "*** No modem playback tool found. Disabled."
    ModemPlay=disabled
else
    echo "Modem playback tool detected. Good !"
    ModemPlay=enabled
fi
if [ "${SoundCard}" = "disabled" ] && [ "${ModemPlay}" = "disabled" ]
   then
    echo "*** Fatal Error: No playback device found."
    echo
    Cleanrc
    exit 1
fi
if [ "${SoundCard}" = "disabled" ] && [ "${ModemPlay}" = "enabled" ]
   then
    echo -n "Enabling modem as playback device: "
    modem_chk
fi
}
#
# Moves a file, asking for the New name.
#
mv_inter() {
  FILE_NAME=`basename ${VOICE_FILE}`
  echo
  echo -n "Rename ${FILE_NAME} to: "
  read NewName
  New_File=${NewName}
  echo -n "Directory [default to ${PLAY_DIR}]: "
  read NewDir
  New_Dir=${NewDir}
  if [ ! ${New_Dir} ]; 
  then 
    New_Dir=${PLAY_DIR};
  fi
  mv -f ${PLAY_DIR}/${FILE_NAME} ${New_Dir}/${New_File}
  echo "File moved to ${New_Dir}/${New_File}"
} # mv_inter
#
rec_file() {
#
trap "" SIGINT
# Set some defaults.
PLAY_DIR=${MSG_DIR}
unset F_FLAG
#
while :;
do
  if [ ! "${Recording}" ]; then
    echo
    echo -n "Last action was \"$LocMessage\" [h for help]: "
      read DO_What
      Action=${DO_What}
  fi
  # Here we go.
  if [ "${Action}" = "R" ] || [ "${Recording}" ]; then
    if [ ! ${RecName} ]; then
      echo -n "Give in a name for the new voice file: "
	read RecName
    fi
    Rec_Name=${MSG_DIR}/${RecName};
    touch ${Rec_Name} &> /dev/null
    errlvl=$?
    if [ $errlvl -gt 0 ]; then
      echo
      echo "*** Error. You do not have write access to the"
      echo "*** ${MSG_DIR} directory. Exiting !"
      echo
      Cleanrc
      exit 1
    fi
    echo "Type <Strg>-C to stop recording"
    echo -n "Be ready, recording starts in 2 seconds: "
    sleep 2; echo "Recording"
    cat /dev/dsp | lintopvf | pvfamp -A 5 | pvfspeed -s 9600 | pvftormd ${MODEM} ${REC_CMP} > ${Rec_Name}
    echo "File ${Rec_Name} created"
    LocMessage="recording"
    unset Recording
    VOICE_FILE=${Rec_Name}
    #
    # Next
  elif [ "${Action}" = "q" ]; then
    echo
    echo "User quit requested. Exiting..."
    Cleanrc
    exit 0
  elif [ "${Action}" = "l" ]; then
    echo
    files_list
    LocMessage="playback"
  elif [ "${Action}" = "c" ]; then
    echo
    file_list
    LocMessage="list current"
  elif [ "${Action}" = "a" ]; then
    echo
    TMP_FILE=${VOICE_FILE}
    echo "Active voice message(s):"
    for message in `cat ${MSG_DIR}/Index` ; do
      VOICE_FILE=${MSG_DIR}/$message
      file_list
    done
    VOICE_FILE=${TMP_FILE}
    LocMessage="listing active"
  elif [ "${Action}" = "h" ]; then
    echo
    echo
    echo ">>> \"${PROGNAME}\" Version ${VER}, Thu the 24 Feb 1997"
    echo ">>> The following commands are supported:"
    echo
    echo "    a: show active message(s) file(s)"
    echo "    c: show information on current message file"
    echo "    d: delete current message file"
    echo "    h: shows this help"
    echo "    l: list all files in ${MSG_DIR}"
    echo "    n: next message file in active list"
    echo "    p: playback current message file"
    echo "    q: exits"
    echo "    R: record voice file"
    echo "    r: remove entry from \"Index\" file"
    echo "    S: show \"Index\" file"
    echo "    s: add entry to \"Index\" file"
    echo "    v: version information"
    echo
    echo " ${PROGNAME} create an adpcm level 3 voice file that can"
    echo " be used with the Zyxel modem."
    echo " Active messages are those listed in the " 
    echo " file \"${MSG_DIR}/Index\""
    LocMessage="help"
  elif [ "${Action}" = "n" ]; then
    echo
    LocMessage="next message"
    echo "Skipping current message file"
    echo
    return 0
    echo
  elif [ "${Action}" = "s" ]; then
    echo -n "Adding current entry to Index: "
    FileName=`basename ${VOICE_FILE}`
    echo $FileName | cat >> ${MSG_DIR}/Index
    LocMessage="add to Index"
    echo "Done."
  elif [ "${Action}" = "S" ]; then
    echo "Entries in \"Index\" file:"
    echo "========================"
    echo
    cat ${MSG_DIR}/Index
    LocMessage="show Index"
  elif [ "${Action}" = "r" ]; then
    touch ${MSG_DIR}/Index &> /dev/null ;
    errlvl=$?
    if [ $errlvl -gt 0 ]; then
      echo
      echo "*** Error. You do not have write access to the index file"
      echo "*** Please make sure you have write access."
      LocMessage="remove from Index failed"
    else
      FileName=`basename ${VOICE_FILE}`
      echo -n "Removing entry \"$FileName\" from Index file: "
      sed -e "s/$FileName//g" <  $MSG_DIR/Index > /tmp/Index_new
      mv -f /tmp/Index_new ${MSG_DIR}/Index
      echo "Done."
      LocMessage="remove from Index"
    fi
  elif [ "${Action}" = "d" ]; then
    echo -n "Delete \"${VOICE_FILE}\" ? [y/n]: "
    read yesno
    if [ "$yesno" = "y" ] || [ "$yesno" = "Y" ]; then
      echo -n "Deleting ${VOICE_FILE}: "
      rm -f ${VOICE_FILE} &> /dev/null
      errlvl=$?
      if [ $errlvl -gt 0 ]; then
	echo "Failed."
	echo "*** You do not have write acces for `basename ${VOICE_FILE}`"
	  echo
      else
	echo "Done."
	echo
	return 0
      fi
      LocMessage="delete succede"
    else
      echo "Delete command aborted."
      LocMessage="delete aborted"
    fi
  elif [ "${Action}" = "v" ]; then
    who
  elif [ "${Action}" = "p" ]; then
    listen_msg
    #      elif [ "${Action}" = "" ]; then
  fi
  #
      done
} # rec_file
#
# Removes .flag file if it still exists.
rm_flag() {
if [ -f ${VOICE_DIR}/.flag ] && [ "$PROGNAME" = "am_play" -o "$PROGNAME" = "am_playall" ]
  then
    echo -n "VM-Received flag exists. Do you want to remove it [y/n]: "
    read yesno
    if [ "$yesno" = "y" ] || [ "$yesno" = "Y" ]; 
    then
      echo -n "Removing VM-Flag: "
      rm -f ${VOICE_DIR}/.flag &> /dev/null
      errlvl=$?
      if [ $errlvl -gt 0 ]; then
        echo "Failed."
        echo "*** You do not have write acces for \".flag\""
          echo
      else
        echo "Done."
	killall vgetty &> /dev/null
	if [ $errlvl -gt 0 ]; then
	  echo "*** Failed to reset vgetty"
	  echo
	fi
      fi
    else
      echo "OK. Exiting"
    fi
fi
#
} # rm_flag
# 
# ------------------------------Run----------------------------
#
# here we go, are we on our sevfer or not ?
if [ "${SERVER}" = "${HOSTNAME}" ]
then # We are on the Servermaschine
  #
  # Setting some defaults first.
  LocMessage="programm start"
  Interactive="-ok"
  #
  # Testing what Params where given.
  #
  while :; do
    case "$1" in
      -a) F_FLAG=" " ;
	  ALL=set ;
	  shift ;;
      -f) if [ "${PROGNAME}" = "am_rec" ]; then
	RecName=${2}
	Recording=true
      else
	FORCE=true ; 
	Interactive="-exec";
      fi
      shift ;;
      -l) PLAY_DIR=${LOC_DIR} ;
	  shift ;;
      -h) Usage >&2
	  Cleanrc
	  exit 0 ;;
      --help) Usage >&2
	      Cleanrc
	      exit 0 ;;
      -m) echo; echo -n "Setting modem as playback device: "
	  modem_chk; echo ;
	  shift ;;
      -n) let N_TIME=" ( ${2} * 60 ) " ;
				     F_FLAG=" -cmin -${N_TIME} " ;
				     TIME_FLAG=true ;
				     shift 2 ;;
      -o) PLAY_DIR=${OLD_DIR} ;
	  LocMessage="Old voice messages selected"
	  NoTimeStamp=true ;
	  shift ;;
      -v) who >&2
	  Cleanrc
	  exit 0 ;;
      -*) who >&2
	  echo "Unknown Option: \"$1\""
	  Usage >&2
	  Cleanrc
	  exit 1 ;;
      *) break
    esac
  done
  #
  # --------------------------------------------------------------------------
  #
  # Set some Variables:
  if [ ! ${TIME_FLAG} ] && [ ! ${NoTimeStamp} ] && [ ! ${ALL} ] ; then
    F_FLAG="-cnewer ${FLAG}"
  fi
  #
  case "${PROGNAME}" in
    # Different options
    am_ls)             files_list
	   echo "End of voice-messages list. Exiting..."
	   Cleanrc
	   exit 0 ;;
    am_rm)             del_files
	   echo "No more voice-messages to delete. Exiting..."
	   Cleanrc
	   exit 0 ;;
    am_mv)             mv_files
	   Cleanrc
	   echo "No more voice-messages to move. Exiting..."
	   exit 0 ;;
    am_play)            # first, check if any Audio-device exists.
	     audio_chk
	     for files in `find ${PLAY_DIR} -name "${FORMAT}" -type f ${F_FLAG}` ; do
	       VOICE_FILE=${files} ;
	       echo;  file_list;  echo
	       dowhat_msg
	     done
	     echo "No more voice-messages to play. Exiting..."
	     Cleanrc
	     exit 0 ;;
    am_playall)         # first, check if any Audio-device exists.
		audio_chk
		for files in ${PLAY_DIR}/* ; do
		  VOICE_FILE=${files} ;
		  listen_msg
		done
		echo "No more voice-messages to play. Exiting..."
		Cleanrc
		exit 0 ;;
    am_rec)            # first, check if any Audio-device exists.
	    audio_chk
	    if [ ! ${Rec_Name} ]; then
	      for messages in ${MSG_DIR}/* ; do
		message=`basename $messages`
		if [ "$message" != "Index" ]; then
		  VOICE_FILE=${MSG_DIR}/$message
		  file_list
		  rec_file
		  LocMessage="list active"
		fi
	      done
	    else
	      VOICE_FILE=${MSG_DIR}/$Rec_Name
	      Recording=true
	      rec_file
	    fi
	    echo "No more voice-messages to play. Exiting..."
	    Cleanrc
	    exit 0 ;;
  esac
  # Clean rcfile.
  Cleanrc
else
  rsh ${SERVER} "$CALL"
fi
#
# End of /usr/local/bin/am_tool
#
