#!/bin/sh

############################################
# Author:  jstile@stilen.com
# Title:   OOo_setup.sh
# Purpose: Automated the per-user install 
#          of Open Office to a nfs share 
# Updated: 2004-07-25 - Updated to 1.1.2
# Updated: 2004-10-25 - Updated to 1.1.3
# Updated: 2005-05-13 - Updated to 1.1.4
############################################
# Administration setup:
#-------------------------------------------
# 1. First run the 'net' install
#    tar -zxvpf OOo_1.1.4_LinuxIntel_install.tar.gz
#    umask 0022
#    ./OOo_1.1.4_LinuxIntel_install/setup -net
#    rm -rf OOo_1.1.4_LinuxIntel_install
#    rm -rf OOo_1.1.4_LinuxIntel_install.tar.gz
#-------------------------------------------
# 2. Now copy these 3 files into the directory /opt/OpenOffice.org1.1.4/
#    cd /opt/OpenOffice.org1.1.4/ 
#    wget http://www.stilen.com/scripts/openoffice/OOo_printer.txt
#    wget http://www.stilen.com/scripts/openoffice/OOo_response.txt
#    wget http://www.stilen.com/scripts/openoffice/OOo_setup.sh
#    chmod +755 OOo_setup.sh
#-------------------------------------------
# 3. To prevent the initial registration window: 
#  Edit OpenOffice.org1.1.4/share/registry/schema/org/openoffice/Office/Common.xcs
#    <prop oor:name="RequestDialog" oor:type="xs:int">
#   		  <value>1</value>
#    </prop>
#    <prop oor:name="ShowMenuItem" oor:type="xs:boolean"> 
# 		  <value>true</value>
#    </prop>
# To:
#    <prop oor:name="RequestDialog" oor:type="xs:int">
#   		  <value>0</value>
#    </prop>
#    <prop oor:name="ShowMenuItem" oor:type="xs:boolean"> 
# 		  <value>false</value>
#    </prop>
#
# If you can use patches, this is my diff
#    913c913
#    <					    <value>1</value>
#    ---
#    >					    <value>0</value>
#    916c916
#    <					    <value>true</value>
#    ---
#    >					    <value>false</value>
#
#-------------------------------------------
# 4. Transfer installation to the NFS server, for a networked environment
#      mv /opt/Openoffice.org1.1.4 /net/tools/OpenOffice.org1.1.4
#-------------------------------------------
# 5. Finally, each user should run this script (OOo_setup.sh) to setup Openoffice.
############################################

############################################
# Set the variales to make the script work
#
OpenOfficeHome="OpenOffice.org1.1.4"
#
# If they already have OpenOffice setup, tell them how to run it.
#
if [ -x "$HOME/$OpenOfficeHome/soffice" ]; then
  echo "You already have OpenOffice installed."
  echo "Try running 'soffice'"
  exit
fi
SetupDirectory="/opt/$OpenOfficeHome"
PrinterTemplate="/opt/$OpenOfficeHome/OOo_printer.txt"
AnserFileTemplate="/opt/$OpenOfficeHome/OOo_response.txt"
#If you have an NIS environmnet:
#    Me=`ypmatch -k $USER passwd | awk '{print $1}'`
#    FirstName=`ypmatch -k $Me passwd | awk -F: '{print $5}' | awk '{print $1}'`
#    LastName=`ypmatch -k $Me passwd | awk -F: '{print $5}' | awk '{print $2}'`
#If you use /etc/shadow and /etc/passwd
Me=`grep ^$USER /etc/passwd  |awk -F: '{print $1}'`
FirstName=`grep ^$USER /etc/passwd  |awk -F: '{print $5}' |sed 's/\(.*\)\ .*/\1/'`
LastName=`grep ^$USER /etc/passwd  |awk -F: '{print $5}' |sed 's/.*\ \(.*\)/\1/'`

MyEMail="$Me@stilen.com"
MyStreet="Happy Trails"
MyZip="93924"
MyCity="Carmel Valley, CA"
MyCompanyname="my_company.com"

#
# Create a custom setup response file for this user
#
cat $AnserFileTemplate |                    \
sed -e "s/First_Name/$FirstName/g"          \
    -e "s/Last_Name/$LastName/g"            \
    -e "s/Me/$Me/g"                         \
    -e "s/OpenOfficeHome/$OpenOfficeHome/g" \
    -e "s/MyEMail/$MyEMail/g"               \
    -e "s/MyStreet/$MyStreet/g"             \
    -e "s/MyZip/$MyZip/g"                   \
    -e "s/MyCity/$MyCity/g"                 \
    -e "s/MyCompanyname/$MyCompanyname/g"   \
    >  /usr/tmp/openoffice_response_file.txt
#
# Change to the users home 
#
pushd $HOME > /dev/null

#
# Run the automated setup
#
if [ -x $SetupDirectory/setup -a  -f /usr/tmp/openoffice_response_file.txt ]; then
    $SetupDirectory/setup -r /usr/tmp/openoffice_response_file.txt
else
   echo -e "ERROR: Can't find $SetupDirectory/setup\nSetup Failed\n";
fi

#
# clean up the temp response file
#
if [ -f  /usr/tmp/openoffice_response_file.txt ]; then
  rm /usr/tmp/openoffice_response_file.txt 
fi

#
# If the PRINTER variable is set, setup a printer
#
if [ -n $PRINTER ]; then
  echo "Adding a printer named ${PRINTER} to your OpenOffice config."
  echo "To change printer: run ~/${OpenOfficeHome}/spadmin"
  cat $PrinterTemplate | \
  sed -e "s/My_Printer/$PRINTER/g" \
  > ${OpenOfficeHome}/user/psprint/psprint.conf
fi

#
# If they do not have a ~/bin directory create it
#
if [ ! -d $HOME/bin ]; then
  echo "Creating a ~/bin directory, since you don't have one."
  mkdir $HOME/bin
fi

#
# If they do have a ~/bin directory, 
# 
if [ -d $HOME/bin -a  -f $HOME/$OpenOfficeHome/soffice ]; then
    # 
    # Change to the ~/bin dir
    #
    cd $HOME/bin
    #
    #  If they have a link to the networked soffice
    #
    if [ -f $HOME/$OpenOfficeHome/soffice ]; then
      #
      # If they have a ~/bin/soffice move it
      #
      if [ -f ~/bin/soffice ]; then 
        mv  $HOME/bin/soffice $HOME/bin/soffice.old
      fi
      #
      # Copy the link to soffice to the bin dir
      #
      echo "Making link ~/bin/soffice"
      cp -l $HOME/$OpenOfficeHome/soffice $HOME/bin/
    fi
fi 

#
# Launch the program for them
#
$HOME/$OpenOfficeHome/soffice  &

#
# Go back to what ever directory they were in before running the setup
#
popd > /dev/null
 
#
# Let them know that we are done
#
echo "Setup Completed. To run, type 'soffice'"

