#!/bin/bash
# (C) 2026 Arahne CAD/CAM for weaving, Ljubljana, Slovenia
# ArahWeave and ArahDrape DEMO installation script
# OS X 64 bit version for Intel Mac
# 
# Extracts the tar file with the software
# Creates ArahWeave and ArahDrape application icons on desktop
# increases shared memory size, change is applied after reboot

echo "Arahne CAD/CAM for weaving, 64 bit DEMO software installation (C) 2026"

if test -e /opt/X11/bin/xmodmap ;
then 
 echo "XQuartz 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.osx64.tar.gz, if OS X did not do it during the download
if test -e ademo.osx64.tar.gz ;
then
 echo "Checking gzip format of ademo.osx64.tar.gz ..."
 # Test whether the file is a valid gzip archive
 if gzip -t ademo.osx64.tar.gz 2>/dev/null ; then
     echo "Unzipping ademo.osx64.tar.gz ..."
     gzip -d ademo.osx64.tar.gz
 else
     echo "File is not a valid gzip archive, renaming..."
     mv ademo.osx64.tar.gz ademo.osx64.tar
 fi
else
 echo "ademo.osx64.tar.gz not found"
fi

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

# creation of icons on desktop for Arahne programs
$HOME/arahne/common/set-finder-icon $HOME/arahne/aw/AW.icns $HOME/arahne/aw/aweave
osascript -e 'tell application "Finder" to make new alias at POSIX file "'$HOME'/Desktop" to POSIX file "'$HOME'/arahne/aw/aweave"'
mv $HOME/Desktop/aweave $HOME/Desktop/ArahWeave

$HOME/arahne/common/set-finder-icon $HOME/arahne/ad/AD.icns $HOME/arahne/ad/adrape
osascript -e 'tell application "Finder" to make new alias at POSIX file "'$HOME'/Desktop" to POSIX file "'$HOME'/arahne/ad/adrape"'
mv $HOME/Desktop/adrape $HOME/Desktop/ArahDrape

# apply icons again, so it will also work on Big Sur
$HOME/arahne/common/set-finder-icon $HOME/arahne/aw/AW.icns $HOME/Desktop/ArahWeave
$HOME/arahne/common/set-finder-icon $HOME/arahne/ad/AD.icns $HOME/Desktop/ArahDrape

# increase shared memory size by modifying kernel paremeters
# works for Intel based MacOS
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 arahne/common/sysctl.conf /etc
fi

# Apple silicon (ARM, M1 and later) require a different way of increasing shared memory
cp arahne/common/memory.plist /Library/LaunchDaemons
cd /Library/LaunchDaemons
chown root memory.plist
chgrp wheel memory.plist
chmod 600 memory.plist
launchctl load memory.plist
cd

echo "Installation finished"
echo "Click ArahWeave or ArahDrape icons on the desktop in order to run the programs."
echo "First time use right mouse button, open, and confirm security exception."

exit



