#!/bin/bash
#
# The purpose of this script is to create a user specifc fstab file
# for Cygwin release 1.7.0 and later from the user mount points of
# the current user, stored in the registry by an earlier installation
# of Cygwin (pre-1.7.0).
#
# The registry mount points are stored in a file /etc/fstab.d/<username>
#

export PATH="/bin:$PATH"

# Uncomment for testing
# SYSCONFDIR="${PWD}"
SYSCONFDIR="${SYSCONFDIR:=/etc}"

FSTABDIR="${SYSCONFDIR}/fstab.d"
FSTAB="${FSTABDIR}/${USER}"
FSTAB="${FSTAB/\\//}"

print_flags ()
{
  (( $1 & 0x0002 )) && echo -n "binary" || echo -n "text"
  (( $1 & 0x0010 )) && echo -n ",exec"
  (( $1 & 0x0040 )) && echo -n ",cygexec"
  (( $1 & 0x0100 )) && echo -n ",notexec"
}

# Check for ${FSTABDIR} directory

if [ -e "${FSTABDIR}" -a ! -d "${FSTABDIR}" ]
then 
  # No mercy.  Try to remove.
  rm -f "${FSTABDIR}"
  if [ -e "${FSTABDIR}" -a ! -d "${FSTABDIR}" ]
  then 
    echo
    echo "${FSTABDIR} is existant but not a directory."
    echo "Please fix that manually."
    echo
    exit 1
  fi
fi

# Create it if necessary

if [ ! -e "${FSTABDIR}" ]
then
  mkdir -m 1777 "${FSTABDIR}"
  if [ ! -e "${FSTABDIR}" ]
  then
    echo
    echo "Creating ${FSTABDIR} directory failed."
    echo "Please fix that manually."
    echo
    exit 1
  fi
fi

if [ ! -e "${FSTAB}" ]
then
  # First check if we really have to create a user-specifc fstab
  # The rules are the same as below when actually creating the file
  create=no
  key='\HKCU\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2'
  for subkey in $(regtool -q list "$key")
  do
    if [[ "$subkey" =~ /.* ]]
    then
      [ "$subkey" = "/" ] && continue
      [ "$subkey" = "/usr/bin" ] && continue
      [ "$subkey" = "/usr/lib" ] && continue
      flags=$(regtool -q get "$key\\$subkey\flags")
      if (( ( $flags & 0x0800 ) == 0 ))
      then
	create=yes
	break
      fi
    fi
  done
  if [ "${create}" = "no" ]
  then
    cygd=""
    prefix=$(regtool -q get "$key\cygdrive prefix")
    flags=$(regtool -q get "$key\cygdrive flags")
    [ -z "$flags" ] && flags=2
    if [ -n "$prefix" \
	 -a \( "$prefix" != "/cygdrive" -o "$(( $flags & ~0x28 ))" -ne 2 \) ]
    then
    echo
    echo "Creating ${FSTABDIR} directory failed."
    echo "Please fix that manually."
    echo
      create=yes
    fi
  fi
  # No reason to create a user fstab file, just exit
  if [ "${create}" = "no" ]
  then
    exit 0
  fi

  mkdir -p -m 1777 "${FSTAB%/*}"
  cat > "${FSTAB}" << EOF
# For a description of the file format, see the Users Guide
# http://cygwin.com/cygwin-ug-net/using.html#mount-table

EOF

  key='\HKCU\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2'
  for subkey in $(regtool -q list "$key")
  do
    if [[ "$subkey" =~ /.* ]]
    then
      # Never write out /, /usr/bin, and /usr/lib
      # These are generated automatically by the Cygwin DLL (since 1.7.0-48)
      # and we don't want to give the user ideas.
      [ "$subkey" = "/" ] && continue
      [ "$subkey" = "/usr/bin" ] && continue
      [ "$subkey" = "/usr/lib" ] && continue
      nat=$(regtool -q get "$key\\$subkey\native")
      nat="${nat//\\//}"
      nat="${nat// /\\040}"
      psx="${subkey// /\\040}"
      flags=$(regtool -q get "$key\\$subkey\flags")
      # Skip managed mounts
      if (( ( $flags & 0x0800 ) == 0 ))
      then
	echo -n "${nat} ${psx} some_fs "
	print_flags $flags
	echo " 0 0"
      fi
    fi >> "${FSTAB}"
  done

  cygd=""
  prefix=$(regtool -q get "$key\cygdrive prefix")
  flags=$(regtool -q get "$key\cygdrive flags")
  [ -z "$flags" ] && flags=2
  # Don't take system and cygdrive flags into account when testing
  if [ -n "$prefix" \
       -a \( "$prefix" != "/cygdrive" -o "$(( $flags & ~0x28 ))" -ne 2 \) ]
  then
    cygd="1"
    psx="${prefix// /\\040}"
    echo -n "none ${psx} cygdrive "
    print_flags $flags
    echo ",posix=0 0 0"
  fi >> "${FSTAB}"
fi
