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