#!/bin/sh
#
ME=$(readlink -f "$0")
MEDIR=${ME%/*}

EXT=open-vm-tools

. $MEDIR/phase-default-init.sh

DEPS="glibc_apps libtool-dev icu-dev libtirpc-dev
 glib2-dev gtkmm-dev gtk3-dev glibmm-dev
 Xorg-7.7-3d-dev libSM-dev libXau-dev libdnet-dev"

if [ "$KBITS" = "" ]; then
	test "$(uname -m)" = "x86_64" && export KBITS=64 || export KBITS=32
fi
test "$KBITS" = "64" && DEPS="$DEPS fuse-dev procps-dev" || DEPS="$DEPS fuse procps-ng-dev"

. $MEDIR/phase-default-deps.sh
. $MEDIR/phase-cc-opts-flto.sh

export LDFLAGS="-lm -ltirpc"

#####autoreconf -i

export RPCGEN=$(readlink -f $(which rpcgen))
export RPCGENFLAGS="-Y $(dirname $(which cpp))"

[ -e /etc/sysconfig/tcedir/copy2fs.flg ] && \
	sudo ln -sf /usr /lib/modules/$(uname -r)/build || \
	sudo ln -sf /tmp/tcloop/linux-4.14.3_api_headers/usr /lib/modules/$(uname -r)/build

sed -i -e 's/(linux)/(__linux__)/' lib/include/vmblock.h
#for a in $(grep -r -l vmware-tools *); do sed -i -e "s/vmware-tools/$EXT/g" $a; done
##sed -i -e "/libc_ver=/s#ldd #/lib/libc.so.6 #" configure


### create Makefiles
#make clean
./configure \
	--prefix=/usr/local \
	--localstatedir=/var \
	--sysconfdir=/etc \
	--disable-glibc-check \
	--disable-static \
	--without-pam \
	--without-xerces \
	--disable-deploypkg \
	--enable-resolutionkms \
	--with-x \
	--without-kernel-modules \
	|| exit

### compile open-vm-tools

. $MEDIR/phase-default-make.sh
. $MEDIR/phase-default-make-install.sh

sudo chown -R tc.staff /tmp/$EXT

### create tcz extension onload script

mkdir -p $TCZ/usr/local/tce.installed
cat > $TCZ/usr/local/tce.installed/$EXT <<EOF
#!/bin/sh

if [ ! -f /etc/udev/rules.d/99-vmware-scsi-udev.rules ]; then
  cp -p /usr/local/etc/udev/rules.d/99-vmware-scsi-udev.rules /etc/udev/rules.d
  udevadm control --reload-rules
  udevadm trigger
fi

if [ ! -f /sbin/mount.vmhgfs ]; then
  ln -s /usr/local/sbin/mount.vmhgfs /sbin/mount.vmhgfs
fi

/usr/local/etc/init.d/$EXT start
EOF

### create vmware-tools initialization script

mkdir -p $TCZ/usr/local/etc/init.d
cat > $TCZ/usr/local/etc/init.d/$EXT <<'EOF'
#!/bin/sh
# Start, stop, and restart vmtoolsd

echo "TinyCoreLinux $(version)" >/etc/release

case "$1" in
start)

# Interface check
NIF1=$(wc -l < /proc/net/dev)

# Load vmblock
vmblock_dev=/tmp/VMwareDnD
vmblockfusemntpt=/var/run/vmblock-fuse
[ -d $vmblock_dev ] || mkdir -m 1777 -p $vmblock_dev

if grep -q "$vmblockfusemntpt" /etc/mtab; then
	true
else
	mkdir -m 1777 -p $vmblockfusemntpt
	vmware-vmblock-fuse -o subtype=vmware-vmblock,default_permissions,allow_other $vmblockfusemntpt
fi

# Start vmtoolsd
if pidof vmtoolsd &>/dev/null; then
	echo vmtoolsd already running
else
	rm -f /var/run/vmtoolsd.pid
	/usr/local/bin/vmtoolsd --background=/var/run/vmtoolsd.pid
fi

# Mount vmhgfs now a userspace program
# /dev/fuse needs to be writable to user
# /mnt/hgfs needs to be a directory and 777
vmhgfsmntpt=/mnt/hgfs
if grep -q $vmhgfsmntpt /etc/mtab; then
	umount -f -l $vmhgfsmntpt
fi
if [ -e $vmhgfsmntpt ]; then
	if [ -d $vmhgfsmntpt ]; then
		chmod 777 $vmhgfsmntpt
	else
		rm -f $vmhgfsmntpt
		mkdir -m 777 -p $vmhgfsmntpt
	fi
else
	mkdir -m 777 -p $vmhgfsmntpt
fi

# add "vmhgfs-fuse $mountpoint" to .profile
# [ $(which vmware-checkvm) ] && \
# [ vmware-checkvm ] && \
# [ $(which vmhgfs-fuse) ] && \
# [ -d /mnt/hgfs ] && \
# vmhgfs-fuse /mnt/hgfs

# Start vmware-user in user's .xsession using the following after mouse initialization:
# [ $(which vmware-checkvm) ] && \
# [ vmware-checkvm ] && \
# vmware-user &

# Use ethtool to optimize vmxnet
	if which ethtool &> /dev/null; then
		for e in $(grep eth /proc/net/dev|cut -d: -f1); do
			ethtool -K $e gso on &> /dev/null
			ethtool -K $e tso on &> /dev/null
		done
	fi

# Start DHCP client for new interfaces
if ! grep -q nodhcp /proc/cmdline; then
	NIF2=$(wc -l < /proc/net/dev)
	if [ $NIF2 -gt $NIF1 ]; then
		  /etc/init.d/dhcp.sh
	fi
fi
;;
stop)
	if pidof vmtoolsd &>/dev/null; then
		killall vmtoolsd
		echo stopped vmtoolsd
		umount -l /mnt/hgfs
	else
		echo vmtoolsd is not running
	fi
;;
restart)
	$0 stop
	$0 start
;;
*)
	echo "Usage: $0 (start|stop|restart)"
	exit 1
;;
esac
EOF
chmod 775 $TCZ/usr/local/etc/init.d/$EXT

### create -dev directory if anyone ever wants it

mkdir -p $TCZ-dev/usr/local/lib
mv $TCZ/usr/local/share $TCZ-dev/usr/local
mv $TCZ/usr/local/include $TCZ-dev/usr/local
mv $TCZ/usr/local/lib/pkgconfig $TCZ-dev/usr/local
for a in $(find $TCZ -name '*.la'); do
        b=$(echo $(dirname $a) | sed "s#$TCZ#$TCZ-dev#")
        mkdir -p $b
        mv $a $b
done

### fix up files

mv $TCZ/lib/udev $TCZ/usr/local/etc
rm -rf $TCZ/lib
rm -rf $TCZ/sbin
mkdir -p $TCZ/etc/vmware-tools/scripts/poweroff-vm-default.d
cat >$TCZ/etc/vmware-tools/scripts/poweroff-vm-default.d/99-shutdown <<EOF
#!/bin/sh
#
/usr/bin/exitcheck.sh shutdown
EOF

### create desktop extension by moving user parts

mkdir -p $TCZ-desktop/usr/local/etc
mv $TCZ/etc/xdg $TCZ-desktop/usr/local/etc

mkdir -p $TCZ-desktop/usr/local/lib/$EXT/plugins
mv $TCZ/usr/local/lib/$EXT/plugins/vmusr $TCZ-desktop/usr/local/lib/$EXT/plugins

mkdir -p $TCZ-desktop/usr/local/bin
mv $TCZ/usr/local/bin/vmware-user* $TCZ-desktop/usr/local/bin

### set file permissions and ownership

. $MEDIR/phase-default-strip.sh

chmod -R 755 $TCZ
sudo chown -R root.root $TCZ*
sudo chown -R root.staff $TCZ*/usr/local/tce.installed
sudo chmod -R g+w $TCZ/usr/local/tce.installed
sudo chmod u+s $TCZ-desktop/usr/local/bin/vmware-user-suid-wrapper

. $MEDIR/phase-default-squash-tcz.sh

