#!/bin/sh

echon() {
  echo "$@" | tr -d '\012';
}

user=`id | awk '{print $1}' | awk -F"(" '{print $2}'|sed 's/)//'`

if [ "$user" != "root" ];then
 echo "Error: You have to be superuser (root) to run this script.";exit 1;
fi

if [ $# != 4 ];then
  echo ""
  echo "Usage: $0 <start_core_script> <special_user> <install dir> <data dir>"
  echo ""
  exit 1
fi

curdir=`dirname "$0"`
absolute=`expr "$0" : '/.*'`
if [ $absolute = 0 ];then
  curdir=`pwd`/"$curdir"
fi
exit_status=0

start_core_script=$1
special_user=$2
install_dir=$3
data_dir=$4

mwos=`uname`
boot_file=""

case $mwos in
	  AIX ) if [ -f /etc/rc.mwcore_services ];then
                   echo "Warning: '/etc/rc.mwcore_services' file exists. Overwriting '/etc/rc.mwcore_services'."
		   /bin/rm -f /etc/rc.mwcore_services 2> /dev/null
                fi
                 boot_file=/etc/rc.mwcore_services
                /usr/sbin/rmitab mwcore 2> /dev/null
                /usr/sbin/mkitab -i srcmstr "mwcore:2:wait:/etc/rc.mwcore_services start"
                ;;
        SunOS ) boot_file=/etc/init.d/mwcore_services
		top_dir=/etc
		filelist='rc0.d/K70 rc1.d/K70 rc2.d/S99'
                ;;
	Linux ) if grep -qi 'Red Hat' /etc/issue; then
		  boot_file=/etc/rc.d/init.d/mwcore_services
		else
		  boot_file=/etc/rc.d/mwcore_services
		fi
		top_dir=/etc/rc.d
		filelist='rc0.d/K70 rc1.d/K70 rc3.d/S99 rc5.d/S99'
                ;;
	 HP-UX ) boot_file=/sbin/init.d/mwcore_services
		 top_dir=/sbin
		 filelist='rc1.d/K700 rc3.d/S995'
                ;;
esac

if [ "$mwos" != "AIX" ]; then
  if [ -f ${boot_file} ]; then
    echo ""
    echo "Warning: '${boot_file}' file exists. Overwriting it."
    /bin/rm -f ${boot_file} 2> /dev/null
  fi
  for prefix in ${filelist}; do
    link=${top_dir}/${prefix}mwcore_services
    /bin/rm -f ${link} 2> /dev/null
    if ln -s ${boot_file} ${link}; then
      : success, do nothing
    else
      echo "Error: cannot create link '${link}' -> '${boot_file}'. Aborting." 1>&2
      exit_status=1
      break
    fi
  done
fi

if [ "$MWTEMP" != "" ];then
  tmpboot=$MWTEMP/boot_file
else
  tmpboot=/var/tmp/boot_file
fi
rm -f $tmpboot 2> /dev/null

if [ $exit_status = 0 ];then
  echo "#!/bin/sh"      >  $tmpboot
  echo ""               >> $tmpboot
  echo "case \"\$1\" in" >> $tmpboot
  echo "'start')"       >> $tmpboot
  echo "        $start_core_script start $special_user $install_dir $data_dir"  >> $tmpboot
  echo "        ;;"     >> $tmpboot
  echo ""               >> $tmpboot
  echo " 'stop')"       >> $tmpboot
  echo "        $start_core_script stop -f $special_user $install_dir $data_dir"   >> $tmpboot
  echo "        ;;"     >> $tmpboot
  echo ""               >> $tmpboot
  echo "'start_msg')"   >> $tmpboot
  echo "        echo \"Start Core Services daemon\"" >> $tmpboot
  echo "        ;;"     >> $tmpboot
  echo ""               >> $tmpboot
  echo "'stop_msg')"    >> $tmpboot
  echo "        echo \"Stop Core Services daemon\"" >> $tmpboot
  echo "        ;;"     >> $tmpboot
  echo ""               >> $tmpboot
  echo "	*)"     >> $tmpboot
  echo "        echo \"Usage: \$0 { start | stop }\""  >> $tmpboot
  echo "        ;;"     >> $tmpboot
  echo ""               >> $tmpboot
  echo "esac"           >> $tmpboot

  cp $tmpboot $boot_file 2> /dev/null
  if [ $? != 0 ];then
     echo "Error: failed to create $boot_file"
     exit 1
  fi
  chmod +x $boot_file
  rm -f $tmpboot 2> /dev/null
fi
exit $exit_status
