#!/bin/bash
# (C) 2016 Arahne CAD/CAM for weaving, Ljubljana, Slovenia
# ArahWeave, ArahDrape, ArahPaint4 installation script
# OSX version for Intel Mac
# 
# Extracts the tar file with the software
# Creates ArahWeave, ArahDrape, ArahPaint4, application icons on desktop
# increases shared memory size, change is applied after reboot
# creates directory /sw/lib and copies some shared libraries to that location
# runs ArahWeave as root, so that fontconfig library creates font cache

echo "Arahne CAD/CAM for weaving, software installation (C) 2016"

if test -e /usr/X11/bin/xmodmap ;
then 
 echo "XQuarz appears to be installed, continuing with installation..."
else
 echo "Please install XQuartz from"
 echo "http://xquartz.macosforge.org"
 echo "before running this script"
 exit
fi

# unzip the ademo.osx.tar.gz, if OSX did not do it during the download
if test -e ademo.osx.tar.gz ;
then
 echo "Unzipping ademo.osx.tar.gz ..."
 gzip -d ademo.osx.tar.gz
else
 echo "ademo.osx.tar.gz not found, it is probably already unzipped"
fi

# untar the arahne software
if test -e ademo.osx.tar ;
then
 tar -xf ademo.osx.tar
 rm ademo.osx.tar
# make sure we have read and write permissions, so we can save and overwrite the old files during upgrade
 chmod -R a+rw ad ap aw common data
else
 echo "Can not find ademo.osx.tar installation file!"
 echo "Place it in home directory together with installation script."
 exit
fi

# creation of icons on desktop for Arahne programs

cp common/icons/ArahWeave.png icon.png
common/sips -i icon.png
common/DeRez -only icns icon.png >icon.rsrc
common/Rez -append icon.rsrc -o aw/aweave
common/SetFile -a C aw/aweave
rm icon.png
rm icon.rsrc

cp common/icons/ArahPaint4.png icon.png
common/sips -i icon.png
common/DeRez -only icns icon.png >icon.rsrc
common/Rez -append icon.rsrc -o ap/apaint4
common/SetFile -a C ap/apaint4
rm icon.png
rm icon.rsrc

cp common/icons/ArahDrape.png icon.png
common/sips -i icon.png
common/DeRez -only icns icon.png >icon.rsrc
common/Rez -append icon.rsrc -o ad/adrape
common/SetFile -a C ad/adrape
rm icon.png
rm icon.rsrc

osascript -e 'tell application "Finder" to make new alias at POSIX file "'$HOME'/Desktop" to POSIX file "'$HOME'/aw/aweave"'
mv $HOME/Desktop/aweave $HOME/Desktop/ArahWeave

osascript -e 'tell application "Finder" to make new alias at POSIX file "'$HOME'/Desktop" to POSIX file "'$HOME'/ap/apaint4"'
mv $HOME/Desktop/apaint4 $HOME/Desktop/ArahPaint4

osascript -e 'tell application "Finder" to make new alias at POSIX file "'$HOME'/Desktop" to POSIX file "'$HOME'/ad/adrape"'
mv $HOME/Desktop/adrape $HOME/Desktop/ArahDrape

# increase shared memory size by modifying kernel paremeters
if test -e /etc/sysctl.conf ;
then 
 echo "File /etc/sysctl.conf already exists."
 echo "If ArahWeave is not running, add these lines to /etc/sysctl.conf and reboot."
 echo "kern.sysv.shmmax=134217728"
 echo "kern.sysv.shmmin=1"
 echo "kern.sysv.shmmni=1024"
 echo "kern.sysv.shmseg=256"
 echo "kern.sysv.shmall=32768"
else
 echo "increased shared memory size, will be applied after reboot."
 cp common/sysctl.conf /etc
fi

# creates directory /sw/lib and copies some libraries to that location
mkdir /sw
mkdir /sw/lib
tar -zxf common/lib/qtlib.tar.gz -C /sw/lib

# run ArahWeave once as root, so that fontconfig library will create the font cache on default /sw/lib location
./aw/aweave &
# wait for font cache creation
sleep 30
# then kill ArahWeave since we do not want it to run as root
killall arahweave

echo "Installation finished"
echo "click ArahWeave, ArahDrape or ArahPaint4 icons on the desktop in order to run the programs"

exit



