#!/bin/bash
# This is /usr/sbin/xvmountconfig
# It will be called from xvmount.postinst
# Copyright 2003 V. Ossenkopf

set -e

# Routine to get all infos on an xvmount entry
getentrydata(){

MOUNTDEV=`echo $i |sed -e 's/^.*\*//'`
MOUNTDIR=`echo $i |sed -e 's/\*.*$//' -e 's/\/$//'`
DEFNAME=`echo ${MOUNTDIR} |awk ' {gsub(".*/","",$0)
	 printf("%s%s:\n",toupper(substr($0,1,1)),substr($0,2)); }'`

if [ ${AUTO} -eq 1 ]; then
  USEENTRY=1
  MNTNAME="${DEFNAME}"
else
  NUM=`expr $j + 1`
  echo ""
  echo "${NUM}. entry: Device ${MOUNTDEV} to be mounted at ${MOUNTDIR}:"
  echo -n "Do you want this drive to be mountable by xvmount? (y/n) [y] "
  read YN
  if [ "$YN" = "n" -o "$YN" = "N" ]; then
    USEENTRY=0
  else
    USEENTRY=1
    echo -n "Name of the entry in the menu [${DEFNAME}] "
    read NAME
    if [ -z "$NAME" ]; then NAME="${DEFNAME}" ; fi
    MNTNAME=`echo $NAME |sed -e 's/ /_/g'`
  fi
fi
}

# Parse the command line for allowed options
parse_cmdline () {
# defaults
  HELP=0
  FORCE=0
  AUTO=0
  DIR=${DEFAULTDIR}
# Now check arguments
  while [ $# -ne 0 ]; do
  case "$1" in
    --help|-h)
      HELP=1
      ;;
    --force)
      FORCE=1
      ;;
    --automatic)
      AUTO=1
      ;;
    --dir)
      DIR=$2
      shift
      ;;
    esac
    shift
  done
}

# This is the start of the main program
# Global variables

CONFIGFILE=xvmounttab
DEFAULTDIR=/etc
PROGRAMNAME=`basename $0`

parse_cmdline $*

# Usage message if called for help
if [ ${HELP} -eq 1 ]; then
	cat <<EOF

Usage: ${PROGRAMNAME} [ -h, --help ] [--force ] [--automatic]
       [--dir <directory>]

${PROGRAMNAME} creates a basic local xvmount configuration file. 
Special configuration options have to be introduced by hand
into ${CONFIGFILE} and are explained in the xvmount(1) man page.

Options: -h, --help      print this help and exit
         --force         overwrite existing xvmount configuration
         --automatic     guess configuration without user interaction
         --dir           create xvmountab in a directory different from /etc

EOF
        exit 0
fi

# If '--force' is supplied a new configuration file is written
# disregarding any old configuration file
if [ -f ${DIR}/${CONFIGFILE} ]; then
  if [ $FORCE -eq 1 ]; then
        echo "An xvmount configuration file is already present."
        if [ $AUTO -eq 1 ]; then
           YN="y"
        else
           echo "It will be preserved as ${CONFIGFILE}.bak"
	   mv ${DIR}/${CONFIGFILE} ${DIR}/${CONFIGFILE}.bak
	fi
  else
        echo "An xvmount configuration file is already present."
        echo "It will not be touched unless xvmountconfig is called with the"
        echo "--force option."
        exit 0
  fi
fi

if [ $AUTO -eq 1 ]
then
  echo ""
  echo "The xvmount configuration file is generated automatically from"
  echo "the list of user mountable devices in /etc/fstab"
else
  echo ""
  echo "You may now select the user mountable devices from /etc/fstab"
  echo "which are to be made available for mounting by xvmount."
fi

# Treat all device types
LIST="`egrep -e '([^[:space:]]+[[:space:]]+){3}.*,?user,?' /etc/fstab |\
sed 	-e '/^[[:space:]]*#/d' \
	-e 's/[[:space:]][[:space:]]*/ /g' \
	-e 's/^\([^ ]*\) \([^ ]*\).*/\2*\1/' |\
sort -s |uniq -W 1 --separator='*'`"

# Check for valid lines
if [ -z "${LIST}" ]
then
  echo "No user-mountable devices found in /etc/fstab."
  if [ -r ${DIR}/${CONFIGFILE}.bak ]
  then
     echo "Your old configuration file is restored."
     mv ${DIR}/${CONFIGFILE}.bak ${DIR}/${CONFIGFILE}
  fi
  exit 8
fi

# Get input - loop 
DOUBLE=1
while [ ${DOUBLE} = 1 ] ; do
i=0

# Loop over all devices
j=0
for i in ${LIST}
do

getentrydata

if [ "${USEENTRY}" = 1 ]
then
  ANAME[$j]=${MNTNAME}
  APOINT[$j]=${MOUNTDIR}
  j=`expr $j + 1`
fi

# Workaround for expr bug, thanks to Hamish Moffatt <hamish@debian.org>
NENTRY=`expr $j - 1 || true`
done

# Ask for final confirmation
if [ $AUTO -eq 1 ]
then
  YN="n"
else
  echo ""
  echo -n "Do you want to correct your input ? (y/n) [n] "
  read YN
fi
if [ "$YN" = "y" -o "$YN" = "Y" ]; then DOUBLE=1; else DOUBLE=0; fi
# End of the input loop
done 

# Output to the configuration file

cat >${DIR}/${CONFIGFILE} <<EOF
# Configuration file for xvmount (3.7)
# generated automatically by xvmountconfig (C) V. Ossenkopf
#
# Name  directory
EOF

j=0
while [ $j -le $NENTRY ]; do
  echo "${ANAME[$j]}"  "${APOINT[$j]}" >>${DIR}/${CONFIGFILE}
  j=`expr $j + 1`
done

echo ""
echo "Configuration file ${DIR}/${CONFIGFILE} successfully written."
# The End
