#!/bin/sh

#
# set_makefile: set the currently active project (the symlink to Makefile)
#

usage_message() {
  echo "$0: set latex_make makefile"
  echo ""
  echo "Usage:"
  echo "       set_makefile [<PROJECT_NAME>]"
  echo "       set_makefile <-c|--create> <PROJECT_NAME>"
  echo ""
  echo "  Where <PROJECT_NAME> is the name of your project."
  echo "  This means that your makefile is <PROJECT_NAME>.mak"
  echo "  and your main tex file is <PROJECT_NAME>.tex"
  echo ""
  echo "  if <PROJECT_NAME> is not given, and only one *.mak file,"
  echo "  exists in the current directory then it will be used as "
  echo ""
  echo "  With the option '-c' (or --create) a new project file will "
  echo "  be created. The PROJECT_NAME argument is mandatory. "
}

error_usage_message() {
  echo "$0: ERROR: $1"
  usage_message
}

# I don't assume PROJECT_NAME is non-empty. Only the value of 
# NEW_MAKEFILE is used in the rest of the script

create_project=0

case "$1" in
  -h|--help)
    usage_message;
    exit 
    ;;
  -c|--create)
    PROJECT_NAME="$2"
    create_project=1
    ;;
# TODO:
# -s|--select)
# add here some code to choose makefle interactively
  *)
    PROJECT_NAME="$1"
    # Continue
esac

NEW_MAKEFILE="${PROJECT_NAME}.mak"
TEX_FILE="${PROJECT_NAME}.tex"
MAKEFILE="Makefile"


if test "${create_project}" = "1"; then
  if test -r ${NEW_MAKEFILE}; then
    echo "A makefile for project ${PROJECT_NAME} already exists."
    echo "No need to create a new one. ignoring 'create'"
  else
    if test "x${SET_MAKEFILE_IGNORE_TEX_CHECK}" = "x"; then 
      if test ! -r ${TEX_FILE}; then
              error_usage_message \
          "tex file ${TEX_FILE} for new project ${PROJECT_NAME} does not exists
I guess that this is a typo, and will not attempt to create a project.
If this is is not a typo, set SET_MAKEFILE_IGNORE_TEX_CHECK"
        exit 1  
      fi
    fi
    echo "Creating Project makefile ${NEW_MAKEFILE}"
    latex_make vars |sed -e "s/^DOCUMENT=.*/DOCUMENT=${PROJECT_NAME}/" \
      > ${NEW_MAKEFILE}
  fi
fi
  
if test "${NEW_MAKEFILE}" = ".mak"; then
  # the PROJECT_NAME argument is not defined:
  current_makefiles="`ls *.mak 2>/dev/null`"
  first_makefile="`ls *.mak 2>/dev/null| head -n 1`"
  if [ -z "${current_makefiles}" ]; then
    error_usage_message "new makefile ${NEW_MAKEFILE} does not exist"
    exit 1
  fi
  if [ "${first_makefile}" = "${current_makefiles}" ]; then
    # There is only one makefile in the current directory
    echo "Only one makefile in the current directory (${first_makefile})"
    NEW_MAKEFILE="${first_makefile}"
  else
    # More than one makefile
    echo "ERROR: More than one makefile in current directory."
    echo "       You'll have to tell me which of them you want:"
    echo ""
    for makefile in ${current_makefiles}; do
      proj="`basename ${makefile} .mak`"
      echo "   $0 ${proj}"
    done
    echo ""
    echo "or use \"$0 --help\" for the full help message"
    exit 1
  fi
fi

if test ! -r "${NEW_MAKEFILE}"; then
	echo "$0: Error: new makefile ${NEW_MAKEFILE} does not exist or is unreadable"
    echo "     You have probably entered the wrong project name."
    echo "     Bailing out"
    exit 1
fi

if [ ! -f "${NEW_MAKEFILE}" ]; then
  echo "$0: Warning: new makefile ${NEW_MAKEFILE} is not a regular file"
fi

# Makefile should be a symlink!
if [ -e "${MAKEFILE}" ]; then
  # Makefile should be a symlink!
  if [ ! -L "${MAKEFILE}" ]; then
    echo "$0: Error: ${MAKEFILE} is not a symbolic link."
    echo "     I don\'t want to replace it with a link to ${NEW_MAKEFILE}"
    echo "     Bailing out"
    exit 1
  fi
  
  # if it is a symlink, it can be safely removed 
  rm -f "${MAKEFILE}" 
fi

# If we got to this point, ${MAKEFILE} does not exist (perhaps deleted, and
# can be safely created

# added -v to make the user aware of what happened.
ln -sv "${NEW_MAKEFILE}" "${MAKEFILE}"
