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

set -e

# Routine to get all infos on an xvmount entry

getentrydata(){

echo ""
echo "${NUMBER}. ${TYPE}:"
echo -n "Do you want this drive to be mountable by xvmount? (y/n) [n] "
read YN
if [ "$YN" = "y" -o "$YN" = "Y" ]; then
  DEFAULT="${TYPENAME}${MNUM}"
  echo -n "Name of the entry in the menu [${DEFAULT}] "
  read NAME
  if [ -z "$NAME" ]; then NAME=$DEFAULT ; fi
  NAME=`echo $NAME |sed -e 's/ /_/g'`
  TYPENAME=`echo $NAME |sed -e 's/[0-9]* *$//'`
  MNUM=`echo $NAME |sed -e 's/  *$//' -e 's/^.*[^0-9]//'`
  ANAME[$i]=$NAME

  OK=0
  while [ "${OK}" = 0 ]; do
    DEFAULT="/dev/${DTYPE}${DNUM}"
    echo -n "Device name of ${NUMBER}. ${TYPE} [${DEFAULT}] "
    read DEV
    if [ -z "$DEV" ]; then DEV=$DEFAULT ; fi
    DTYPE=`echo $DEV |sed -e 's/^\/dev\///' -e 's/[0-9]* *$//'`
    DNUM=`echo $DEV |sed -e 's/  *$//' -e 's/^.*[^0-9]//'`
    ADEV[$i]=$DEV
    if [ -b ${DEV} ]; then
      OK=1
    else
      echo "${DEV} is not a block device. Please use another device."
      OK=0
    fi
  done

  OK=0
  while [ "${OK}" = 0 ]; do
    DEFAULT="${MDIR}${MNUM}"
    echo -n "Mount point for the device [${DEFAULT}] "
    read MPOINT
    if [ -z "$MPOINT" ]; then MPOINT=$DEFAULT ; fi
    MDIR=`echo $MPOINT |sed -e 's/[0-9]* *$//'`
    MNUM=`echo $MPOINT |sed -e 's/  *$//' -e 's/^.*[^0-9]//'`
    APOINT[$i]=$MPOINT
    if [ -e ${MPOINT} ]; then
      if [ -d ${MPOINT} ]; then
        if ls -a "${MPOINT}" |grep -e '[^.]' >/dev/null; then
          echo "Directory ${MPOINT} exists but is not empty."
          echo -n "Use as mount point anyway ? (y/n) [n] "
          read YN
          if [ "$YN" = "y" -o "$YN" = "Y" ]; then OK=1; else OK=0; fi
        else
          OK=1
        fi
      else
        echo "${MPOINT} is not a directory. Please use another mount point."
        OK=0
      fi
    else
      if mkdir ${MPOINT}; then
        OK=1
      else
        echo "Could not create directory ${MPOINT}, Please use another one."
        OK=0
      fi
    fi
  done

  DEFAULT="${MOPT}"
  echo -n "Special non-default mount options (ro,exec,suid,dev) [${DEFAULT}] "
  read OPTION
  if [ -z "$OPTION" ]; then OPTION=$DEFAULT ; fi
  OPTION=`echo ${OPTION} |sed -e 's/  */,/g' -e 's/,,*/,/g' -e 's/^,//' -e 's/,$//'`
  if [ -z "$OPTION" ]; then
  	AOPT[$i]='defaults'
  else
  	AOPT[$i]=$OPTION
  	MOPT=$OPTION
  fi
  i=`expr $i + 1`

fi
}

# Routine to get the infos for all devices of one type

deviceloop(){

YN="y"
while [ "${YN}" = "y" -o "${YN}" = "Y" ]; do
  getentrydata
  DNUM=`expr '0'$DNUM + 1`
  MNUM=`expr '0'$MNUM + 1`
  NUMBER=`expr $NUMBER + 1`
echo -n "I there another ${TYPE}? (y/n) [n] "
read YN
done
}

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

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

# Usage message if called for help

if [ "$1" = "-h"  -o "$1" = "--help" ]; then
	cat <<EOF

Usage: ${PROGRAMNAME} [ -h, --help ] [--force ]

${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

EOF
        exit 0
fi

# If '--force' is supplied as $1 a new configuration file is written
# disregarding any old configuration file

if [ "$1" = "--force" ]; then
  FORCE=1
else
  FORCE=0
fi

if [ -f $CONFIGFILE ]; then
  if [ $FORCE -eq 1 ]; then
        echo "An xvmount configuration file is already present."
        echo -n "Preserve that file as ${CONFIGFILE}.bak? (y/n) [y] "
        read YN
	if [ "$YN" != 'n' -a "$YN" != 'N' ]
	   then mv $CONFIGFILE ${CONFIGFILE}.bak
	fi
  else
        echo "An xvmount configuration file is already present."
        echo "It will not be touched unless xvmountconfig is called"
        echo "with the --force option."
        exit 0
  fi
fi

echo ""
echo "You may now select all disks and drives that may be mounted"
echo "by unpriviledged users via xvmount."

# Get input - loop if consistency check fails

DOUBLE=1
while [ ${DOUBLE} = 1 ] ; do
i=0

# Treat all device types

NUMBER=1
TYPE='floppy drive'
TYPENAME='Floppy'
DTYPE='fd'
DNUM='0'
MDIR='/floppy'
MNUM=''
MOPT=''

deviceloop

NUMBER=1
TYPE='CD-ROM drive'
TYPENAME='CD-ROM'
DTYPE='cdrom'
DNUM=''
MDIR='/cdrom'
MNUM=''
MOPT='ro'

deviceloop

NUMBER=1
TYPE='DOS partition'
TYPENAME='DOS-disk'
DTYPE='hda'
DNUM='1'
MDIR='/dos'
MNUM=''
MOPT=''

deviceloop

NUMBER=1
TYPE='other device'
TYPENAME='Other'
DTYPE='hda'
DNUM='1'
MDIR='/mnt'
MNUM=''
MOPT=''

deviceloop

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

# Ask for final confirmation

echo ""
echo -n "Do you want to correct your input ? (y/n) [n] "
read YN
if [ "$YN" = "y" -o "$YN" = "Y" ]; then DOUBLE=1; else DOUBLE=0; fi

# Consistency check

i=0
while [ $i -lt $NENTRY ]; do
  j=`expr $i + 1`
  while [ $j -le $NENTRY ]; do
    if [ ${ADEV[$i]} = ${ADEV[$j]} ]; then
      echo "The device "${ADEV[$i]}" is used more than once."
      echo "Please, repeat the selection."
      DOUBLE=1
    fi
    if [ ${APOINT[$i]} = ${APOINT[$j]} ]; then
      echo "The mount point "${APOINT[$i]}" is used more than once."
      echo "Please, repeat the selection."
      DOUBLE=1
    fi
    j=`expr $j + 1`
  done
  i=`expr $i + 1`
done

# End of the input loop
done 

# Output to the configuration file

cat >$CONFIGFILE <<EOF
# Configuration file for xvmount
# generated automatically by xvmountconfig (C) V. Ossenkopf
#
# Name  device  directory  type  options
EOF

i=0
while [ $i -le $NENTRY ]; do
  echo ${ANAME[$i]}":  "${ADEV[$i]}"  "${APOINT[$i]}"  auto  "${AOPT[$i]} >>$CONFIGFILE
  i=`expr $i + 1`
done

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