#!/bin/sh
#newhostid
#Author: Jean-Louis Faraut [jlf]
#Organization: Ecole Superieure en Sciences Informatiques 
#   (Universite de Nice Sophia-Antipolis)
#Address: Sophia-Antipolis (France)
#
# This program is distributed in the hope that it will be useful,
# but without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose. You use
# this program at your own risk. The author disclaims responsibility for
# any damages that might result from the use of this program, even
# if they result from negligence on the part of the author.
# 
# Also, please don't use this program to steal software. The intended
# use is for emergency situations where an application has to be moved from
# one computer to another (e.g. in the event of a hardware malfunction)
# and licence keys cannot be obtained quickly from the vendor. Many
# vendors will not supply licence keys outside of business hours.
#
#run a Sun dynamically linked executable  with a new hostid
# - notes - only works on a Sun. Only tested under Solaris 2.3 and 1.[01]
#         - only works with dymanically linked executables.
#
# SunOS 5.x code is an alpha release (not fully tested)
# usage:
#   newhostid Base10Hostid command [args]
# On Solaris 1.x (Sun OS 4.1.x) you can also
#  newhostid 0x72000000 command
#   if you want to use hex
# Sun OS 5.x note:
# if it turns out that you need somewhat more complete sysinfo emulation
#  check out sidump.c
#Warning: The following file names are used by this script:
#/tmp/gethostid.{c,o,so}           -> if OS == SunOS 4.1.x
#/tmp/sysinfo.{c,c.head,c.tail,so} -> if OS == SunOS 5.x
#
#modify the following line to meet your site requirement
GCC=/opt/local/bin/gcc	#only required by SunOS 5.x
#
if [ $# -lt 2 ] 
then
	echo 'Usage:newhostid hostid(base 10) command [args]'
	exit 1
fi

case `/usr/bin/uname -s` in
     SunOS) ;;
     *) echo 'Only for SunOS. Sorry.'
     exit 1 ;;
esac

case `/usr/bin/uname -r` in
     4.1.*)
     (cd /tmp
     cat > gethostid.c <<!
     gethostid()
     {
       return $1;
       }
!
       cc -c gethostid.c
       ld gethostid.o -assert pure-text -o gethostid.so
       rm gethostid.o gethostid.c
       )

       shift
       LD_PRELOAD="/tmp/gethostid.so" $*
       rm /tmp/gethostid.so
     ;;

     5.*)
     (cd /tmp
     # first part of program
     STRLEN=`expr $1 : '.*'`
     cat > sysinfo.c.head <<!
     long sysinfo(command,buf,count)
     int command; char *buf; long count;
     {
!

     # second part of program
     touch sysinfo.c.tail
     DOT=""
     COUNT=0
     while [ $COUNT -lt $STRLEN ]
     do
     	echo "  buf[$COUNT] = '`expr $1 : ${DOT}'\(.\)'`';" >> sysinfo.c.tail
		DOT=${DOT}.
			COUNT=`expr $COUNT + 1`

			done
			echo "buf[$COUNT] = 0;"  >> sysinfo.c.tail
			cat >> sysinfo.c.tail <<!
			  return 11l;
			  }
!

			  cat sysinfo.c.head sysinfo.c.tail > sysinfo.c
			  rm sysinfo.c.head sysinfo.c.tail
			  #debug: cat sysinfo.c
			  $GCC -fpic -c sysinfo.c
			  ld sysinfo.o  -G -o sysinfo.so
			  rm sysinfo.o sysinfo.c
			  )

			  shift
			  LD_PRELOAD=/tmp/sysinfo.so $*
			  rm /tmp/sysinfo.so
    ;;

esac


