#!/bin/sh
##############################################################################
#
#  Copyright (c) 2008, Doremi Labs, Inc.
#    All rights reserved.
#
#  Redistribution in source or binary forms, with or without
#  modification, are NOT permitted.
#
#    Doremi Labs, Inc.
#    306 East Alameda Avenue
#    Burbank, CA 91502
#    Tel : (818) 562 1101
#    Fax : (818) 562 1109
#
##############################################################################

HOT_FIX="X11vnc-1.9 watchdog"
LOGFILE="/doremi/log/update.log"
DONE=0

remount_rootfs_rw()
{
  test -n "${rootfsisro}" && mount -o remount,rw /
}

remount_rootfs_ro()
{
  sync
  test -n "${rootfsisro}" && mount -o remount,ro /
}

check_load_vnc()
{
  CFILE="$1"

  if ! test -r ${CFILE}; then
    return 0;
  fi
  # test load vnc instruction
  if ! grep -s Load ${CFILE} | grep -qs vnc; then
    remount_rootfs_rw
    # check if section exist
    if grep -qs 'Section "Module"' ${CFILE}; then
      sed -i -e 's/^Section "Module"$/Section "Module"\n        Load    "vnc"/g' ${CFILE}
    else
      echo >> ${CFILE}
      echo 'Section "Module"' >> ${CFILE}
      echo '        Load    "vnc"' >>  ${CFILE}
      echo 'EndSection' >> ${CFILE}
    fi
    remount_rootfs_ro
    DONE=1
  fi
  if ! grep -s Load ${CFILE} | grep -qs vnc; then
    echo '['`date`']:' "${HOT_FIX}: Error: failed to insert vnc module." >> ${LOGFILE}
    exit -1
  fi
  return 0
}

check_load_extmod()
{
  CFILE="$1"

  if ! test -r ${CFILE}; then
    return 0;
  fi
  # test load extmod instruction
  if ! grep -s Load ${CFILE} | grep -qs extmod; then
    remount_rootfs_rw
    # check if section exist
    if grep -qs 'Section "Module"' ${CFILE}; then
      sed -i -e 's/^Section "Module"$/Section "Module"\n        Load    "extmod"/g' ${CFILE}
    else
      echo >> ${CFILE}
      echo 'Section "Module"' >> ${CFILE}
      echo '        Load    "extmod"' >>  ${CFILE}
      echo 'EndSection' >> ${CFILE}
    fi
    remount_rootfs_ro
    DONE=1
  fi
  if ! grep -s Load ${CFILE} | grep -qs extmod; then
    echo '['`date`']:' "${HOT_FIX}: Error: failed to insert extmod module." >> ${LOGFILE}
    exit -1
  fi
  return 0
}

check_password_vnc()
{
  CFILE="$1"

  if ! test -r ${CFILE}; then
    return 0;
  fi
  # check option password file
  if ! grep -s Option ${CFILE} | grep -qs PasswordFile; then
    remount_rootfs_rw
    # check if section exist
    if grep -qs 'Section "Screen"' ${CFILE}; then
      sed -i -e 's_^Section "Screen"$_Section "Screen"\n        Option     "PasswordFile"    "/doremi/etc/vnc/passwd"_g' ${CFILE}
    else
      echo >> ${CFILE}
      echo 'Section "Screen"' >> ${CFILE}
      echo '        Option     "PasswordFile"    "/doremi/etc/vnc/passwd"' >>  ${CFILE}
      echo 'EndSection' >> ${CFILE}
    fi
    remount_rootfs_ro
    DONE=1
  fi
  if ! grep -s Option ${CFILE} | grep -qs PasswordFile; then
    echo '['`date`']:' "${HOT_FIX}: Error: failed to insert vnc password." >> ${LOGFILE}
    exit -1
  fi
  return 0
}

check_path_vnc()
{
  CFILE="$1"

  if ! test -r ${CFILE}; then
    return 0;
  fi
  # check proper path
  if ! grep -s ModulePath ${CFILE} | grep -qs '"/usr/X11R6/lib/modules/extensions"'; then
    remount_rootfs_rw
    sed -i -e 's_^Section "Files"$_Section "Files"\n    ModulePath      "/usr/X11R6/lib/modules/extensions"_g' ${CFILE}
    remount_rootfs_ro
    DONE=1
  fi
  if ! grep -s ModulePath ${CFILE} | grep -qs '"/usr/X11R6/lib/modules"'; then
    remount_rootfs_rw
    sed -i -e 's_^Section "Files"$_Section "Files"\n    ModulePath      "/usr/X11R6/lib/modules"_g' ${CFILE}
    remount_rootfs_ro
    DONE=1
  fi
  return 0
}

if mount | grep 'on / ' | grep -v rootfs | grep -q '(ro[),]'; then
  rootfsisro=true
fi

# XF86Config setups
if test -r /etc/X11/XF86Config-4; then
  DCFILE=/etc/X11/XF86Config-4
elif test -r /etc/X11/xorg.conf; then
  DCFILE=/etc/X11/xorg.conf
else
  echo '['`date`']:' "${HOT_FIX}: Error: configuration file not found" >> ${LOGFILE}
  exit -1
fi

# Xorg configuration file
check_load_vnc     /etc/X11/xorg.conf
check_password_vnc /etc/X11/xorg.conf

# Xfree86 configuration file
check_load_vnc     /etc/X11/XF86Config-4
check_password_vnc /etc/X11/XF86Config-4
check_path_vnc     /etc/X11/XF86Config-4

if [ ${DONE} -gt 0 ]; then
  echo '['`date`']:' ${HOT_FIX}: patched. >> ${LOGFILE}
fi

exit 0
