#! /bin/sh

BASE="$1"
BASEURL="$2"
LANGUAGE="$3"
LANGS=""
RESULT=""

# Find what translations are available.
for j in ${BASE}.??.xhtml; do 
  LANGS="`expr $j : ${BASE}.\\\\\(.*\\\\\).xhtml` ${LANGS}"
done

# Create the string to be included in the webpage.
BASENAME=`basename ${BASE}` 

if [ "`echo ${LANGS} | grep en`" != "" ]; then
  if [ "${LANGUAGE}" != "en" ]; then
    RESULT="${RESULT}<a href=\"${BASENAME}.en.html\">"
  fi
  RESULT="${RESULT}<img src=\"${BASEURL}/images/en.jpg\" \
border=\"0\" width=\"32\" height=\"26\" \
alt=\"[English]\">"
  if [ "${LANGUAGE}" != "en" ]; then
    RESULT="${RESULT}</a>"
  fi
  RESULT="${RESULT} | "
fi
if [ "`echo ${LANGS} | grep fr`" != "" ]; then
  if [ "${LANGUAGE}" != "fr" ]; then
    RESULT="${RESULT}<a href=\"${BASENAME}.fr.html\">"
  fi
  RESULT="${RESULT}<img src=\"${BASEURL}/images/fr.jpg\" \
border=\"0\" width=\"32\" height=\"26\" \
alt=\"[Fran&ccedil;ais]\">"
  if [ "${LANGUAGE}" != "fr" ]; then
    RESULT="${RESULT}</a>"
  fi
fi

# Output the string.
echo "${RESULT}"

# Create or update $BASE.lang file as needed
if [ "${LANGUAGE}" != "" ]; then
    LANGSTRING="`$0 ${BASE}`"
else
    LANGSTRING="${RESULT}"
fi
if [ ! -f "${BASE}.lang" ]; then
    echo "${LANGSTRING}" > ${BASE}.lang
elif [ "${LANGSTRING}" != "`cat ${BASE}.lang`" ]; then
    echo "${LANGSTRING}" > ${BASE}.lang
fi
