platform: Unix startup script refactored
[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=""
137 VM_OPTIONS_FILES_USED=""
138 for vm_opts_file in "$@@product_uc@@_VM_OPTIONS" "$HOME/.@@system_selector@@/@@vm_options@@$BITS.vmoptions" "$IDE_BIN_HOME/@@vm_options@@$BITS.vmoptions"; do
139   if [ -r "$vm_opts_file" ]; then
140     VM_OPTIONS_DATA=`"$CAT" "$vm_opts_file" | "$GREP" -v "^#.*" | "$TR" '\n' ' '`
141     VM_OPTIONS="$VM_OPTIONS $VM_OPTIONS_DATA"
142     if [ -n "$VM_OPTIONS_FILES_USED" ]; then
143       VM_OPTIONS_FILES_USED="$VM_OPTIONS_FILES_USED,"
144     fi
145     VM_OPTIONS_FILES_USED="$VM_OPTIONS_FILES_USED$vm_opts_file"
146   fi
147 done
148
149 IS_EAP="@@isEap@@"
150 if [ "$IS_EAP" = "true" ]; then
151   OS_NAME=`echo $OS_TYPE | "$TR" '[:upper:]' '[:lower:]'`
152   AGENT_LIB="yjpagent-$OS_NAME$BITS"
153   if [ -r "$IDE_BIN_HOME/lib$AGENT_LIB.so" ]; then
154     AGENT="-agentlib:$AGENT_LIB=disablej2ee,disablealloc,delay=10000,sessionname=@@system_selector@@"
155   fi
156 fi
157
158 IDE_JVM_ARGS="@@ide_jvm_args@@"
159
160 @@class_path@@
161 if [ -n "$@@product_uc@@_CLASSPATH" ]; then
162   CLASSPATH="$CLASSPATH:$@@product_uc@@_CLASSPATH"
163 fi
164
165 # ---------------------------------------------------------------------
166 # Run the IDE.
167 # ---------------------------------------------------------------------
168 while true ; do
169   LD_LIBRARY_PATH="$IDE_BIN_HOME:$LD_LIBRARY_PATH" "$JDK/bin/java" \
170     $AGENT \
171     "-Xbootclasspath/a:$IDE_HOME/lib/boot.jar" \
172     -classpath "$CLASSPATH" \
173     $VM_OPTIONS "-Djb.vmOptionsFile=$VM_OPTIONS_FILES_USED" \
174     "-XX:ErrorFile=$HOME/java_error_in_@@product_uc@@_%p.log" \
175     -Djb.restart.code=88 -Didea.paths.selector=@@system_selector@@ \
176     $IDE_PROPERTIES_PROPERTY \
177     $IDE_JVM_ARGS \
178     $REQUIRED_JVM_ARGS \
179     $MAIN_CLASS_NAME \
180     "$@"
181   test $? -ne 88 && break
182 done