[platform] fixes IDE launch from a bin/ directory on Unix (IDEA-161265)
[idea/community.git] / bin / scripts / unix / idea.sh
1 #!/bin/sh
2 #
3 # ---------------------------------------------------------------------
4 # @@product_full@@ startup script.
5 # ---------------------------------------------------------------------
6 #
7
8 message()
9 {
10   TITLE="Cannot start @@product_full@@"
11   if [ -n "`which zenity`" ]; then
12     zenity --error --title="$TITLE" --text="$1"
13   elif [ -n "`which kdialog`" ]; then
14     kdialog --error "$1" --title "$TITLE"
15   elif [ -n "`which xmessage`" ]; then
16     xmessage -center "ERROR: $TITLE: $1"
17   elif [ -n "`which notify-send`" ]; then
18     notify-send "ERROR: $TITLE: $1"
19   else
20     echo "ERROR: $TITLE\n$1"
21   fi
22 }
23
24 UNAME=`which uname`
25 GREP=`which egrep`
26 GREP_OPTIONS=""
27 CUT=`which cut`
28 READLINK=`which readlink`
29 XARGS=`which xargs`
30 DIRNAME=`which dirname`
31 MKTEMP=`which mktemp`
32 RM=`which rm`
33 CAT=`which cat`
34 TR=`which tr`
35
36 if [ -z "$UNAME" -o -z "$GREP" -o -z "$CUT" -o -z "$MKTEMP" -o -z "$RM" -o -z "$CAT" -o -z "$TR" ]; then
37   message "Required tools are missing - check beginning of \"$0\" file for details."
38   exit 1
39 fi
40
41 OS_TYPE=`"$UNAME" -s`
42
43 # ---------------------------------------------------------------------
44 # Ensure IDE_HOME points to the directory where the IDE is installed.
45 # ---------------------------------------------------------------------
46 SCRIPT_LOCATION=$0
47 if [ -x "$READLINK" ]; then
48   while [ -L "$SCRIPT_LOCATION" ]; do
49     SCRIPT_LOCATION=`"$READLINK" -e "$SCRIPT_LOCATION"`
50   done
51 fi
52
53 IDE_BIN_HOME=`dirname "$SCRIPT_LOCATION"`
54 if [ "$IDE_BIN_HOME" = "." ]; then
55   IDE_HOME=".."
56 else
57   IDE_HOME=`dirname "$IDE_BIN_HOME"`
58 fi
59
60 # ---------------------------------------------------------------------
61 # Locate a JDK installation directory which will be used to run the IDE.
62 # Try (in order): @@product_uc@@_JDK, @@vm_options@@.jdk, ../jre, JDK_HOME, JAVA_HOME, "java" in PATH.
63 # ---------------------------------------------------------------------
64 if [ -n "$@@product_uc@@_JDK" -a -x "$@@product_uc@@_JDK/bin/java" ]; then
65   JDK="$@@product_uc@@_JDK"
66 elif [ -s "$HOME/.@@system_selector@@/config/@@vm_options@@.jdk" ]; then
67   JDK=`"$CAT" $HOME/.@@system_selector@@/config/@@vm_options@@.jdk`
68   if [ ! -d "$JDK" ]; then
69     JDK="$IDE_HOME/$JDK"
70   fi
71 elif [ -x "$IDE_HOME/jre/jre/bin/java" ] && "$IDE_HOME/jre/jre/bin/java" -version > /dev/null 2>&1 ; then
72   JDK="$IDE_HOME/jre"
73 elif [ -n "$JDK_HOME" -a -x "$JDK_HOME/bin/java" ]; then
74   JDK="$JDK_HOME"
75 elif [ -n "$JAVA_HOME" -a -x "$JAVA_HOME/bin/java" ]; then
76   JDK="$JAVA_HOME"
77 else
78   JAVA_BIN_PATH=`which java`
79   if [ -n "$JAVA_BIN_PATH" ]; then
80     if [ "$OS_TYPE" = "FreeBSD" -o "$OS_TYPE" = "MidnightBSD" ]; then
81       JAVA_LOCATION=`JAVAVM_DRYRUN=yes java | "$GREP" '^JAVA_HOME' | "$CUT" -c11-`
82       if [ -x "$JAVA_LOCATION/bin/java" ]; then
83         JDK="$JAVA_LOCATION"
84       fi
85     elif [ "$OS_TYPE" = "SunOS" ]; then
86       JAVA_LOCATION="/usr/jdk/latest"
87       if [ -x "$JAVA_LOCATION/bin/java" ]; then
88         JDK="$JAVA_LOCATION"
89       fi
90     elif [ "$OS_TYPE" = "Darwin" ]; then
91       JAVA_LOCATION=`/usr/libexec/java_home`
92       if [ -x "$JAVA_LOCATION/bin/java" ]; then
93         JDK="$JAVA_LOCATION"
94       fi
95     fi
96
97     if [ -z "$JDK" -a -x "$READLINK" -a -x "$XARGS" -a -x "$DIRNAME" ]; then
98       JAVA_LOCATION=`"$READLINK" -f "$JAVA_BIN_PATH"`
99       case "$JAVA_LOCATION" in
100         */jre/bin/java)
101           JAVA_LOCATION=`echo "$JAVA_LOCATION" | "$XARGS" "$DIRNAME" | "$XARGS" "$DIRNAME" | "$XARGS" "$DIRNAME"`
102           if [ ! -d "$JAVA_LOCATION/bin" ]; then
103             JAVA_LOCATION="$JAVA_LOCATION/jre"
104           fi
105           ;;
106         *)
107           JAVA_LOCATION=`echo "$JAVA_LOCATION" | "$XARGS" "$DIRNAME" | "$XARGS" "$DIRNAME"`
108           ;;
109       esac
110       if [ -x "$JAVA_LOCATION/bin/java" ]; then
111         JDK="$JAVA_LOCATION"
112       fi
113     fi
114   fi
115 fi
116
117 JAVA_BIN="$JDK/bin/java"
118 if [ ! -x "$JAVA_BIN" ]; then
119   JAVA_BIN="$JDK/jre/bin/java"
120 fi
121
122 if [ -z "$JDK" ] || [ ! -x "$JAVA_BIN" ]; then
123   message "No JDK found. Please validate either @@product_uc@@_JDK, JDK_HOME or JAVA_HOME environment variable points to valid JDK installation."
124   exit 1
125 fi
126
127 VERSION_LOG=`"$MKTEMP" -t java.version.log.XXXXXX`
128 JAVA_TOOL_OPTIONS= "$JAVA_BIN" -version 2> "$VERSION_LOG"
129 "$GREP" "64-Bit|x86_64|amd64" "$VERSION_LOG" > /dev/null
130 BITS=$?
131 "$RM" -f "$VERSION_LOG"
132 test ${BITS} -eq 0 && BITS="64" || BITS=""
133
134 # ---------------------------------------------------------------------
135 # Collect JVM options and IDE properties.
136 # ---------------------------------------------------------------------
137 if [ -n "$@@product_uc@@_PROPERTIES" ]; then
138   IDE_PROPERTIES_PROPERTY="-Didea.properties.file=$@@product_uc@@_PROPERTIES"
139 fi
140
141 VM_OPTIONS_FILE=""
142 if [ -n "$@@product_uc@@_VM_OPTIONS" -a -r "$@@product_uc@@_VM_OPTIONS" ]; then
143   # explicit
144   VM_OPTIONS_FILE="$@@product_uc@@_VM_OPTIONS"
145 elif [ -r "$HOME/.@@system_selector@@/@@vm_options@@$BITS.vmoptions" ]; then
146   # user-overridden
147   VM_OPTIONS_FILE="$HOME/.@@system_selector@@/@@vm_options@@$BITS.vmoptions"
148 elif [ -r "$IDE_BIN_HOME/@@vm_options@@$BITS.vmoptions" ]; then
149   # default, standard installation
150   VM_OPTIONS_FILE="$IDE_BIN_HOME/@@vm_options@@$BITS.vmoptions"
151 else
152   # default, universal package
153   test "$OS_TYPE" = "Darwin" && OS_SPECIFIC="mac" || OS_SPECIFIC="linux"
154   VM_OPTIONS_FILE="$IDE_BIN_HOME/$OS_SPECIFIC/@@vm_options@@$BITS.vmoptions"
155 fi
156
157 VM_OPTIONS=""
158 if [ -r "$VM_OPTIONS_FILE" ]; then
159   VM_OPTIONS=`"$CAT" "$VM_OPTIONS_FILE" | "$GREP" -v "^#.*"`
160 else
161   message "Cannot find VM options file"
162 fi
163
164 IS_EAP="@@isEap@@"
165 if [ "$IS_EAP" = "true" ]; then
166   OS_NAME=`echo "$OS_TYPE" | "$TR" '[:upper:]' '[:lower:]'`
167   AGENT_LIB="yjpagent-$OS_NAME$BITS"
168   if [ -r "$IDE_BIN_HOME/lib$AGENT_LIB.so" ]; then
169     AGENT="-agentlib:$AGENT_LIB=disablealloc,delay=10000,sessionname=@@system_selector@@"
170   fi
171 fi
172
173 @@class_path@@
174 if [ -n "$@@product_uc@@_CLASSPATH" ]; then
175   CLASSPATH="$CLASSPATH:$@@product_uc@@_CLASSPATH"
176 fi
177
178 # ---------------------------------------------------------------------
179 # Run the IDE.
180 # ---------------------------------------------------------------------
181 IFS="$(printf '\n\t')"
182 LD_LIBRARY_PATH="$IDE_BIN_HOME:$LD_LIBRARY_PATH" "$JAVA_BIN" \
183   ${AGENT} \
184   "-Xbootclasspath/a:$IDE_HOME/lib/boot.jar" \
185   -classpath "$CLASSPATH" \
186   ${VM_OPTIONS} \
187   "-Djb.vmOptionsFile=$VM_OPTIONS_FILE" \
188   "-XX:ErrorFile=$HOME/java_error_in_@@product_uc@@_%p.log" \
189   "-XX:HeapDumpPath=$HOME/java_error_in_@@product_uc@@.hprof" \
190   -Djb.restart.code=88 -Didea.paths.selector=@@system_selector@@ \
191   ${IDE_PROPERTIES_PROPERTY} \
192   @@ide_jvm_args@@ \
193   com.intellij.idea.Main \
194   "$@"
195 EC=$?
196 unset IFS
197
198 test ${EC} -ne 88 && exit ${EC}
199
200 RESTARTER="$HOME/.@@system_selector@@/system/restart/restarter.sh"
201 if [ -x "$RESTARTER" ]; then
202   "$RESTARTER"
203   "$RM" -f "$RESTARTER"
204 fi
205
206 exec "$0"