#!/bin/sh
set -e

# make-sqldeveloper-package

# (2017-10-29)

# Copyright © 2009-2017 Lazarus Long <lazarus (dot) long (at) sapo (dot) pt>

##########################################################################
#  This program is free software: you can redistribute it and/or modify  #
#  it under the terms of the GNU General Public License as published by  #
#  the Free Software Foundation, either version 3 of the License, or     #
#  (at your option) any later version.                                   #
#                                                                        #
#  This program is distributed in the hope that it will be useful,       #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#  GNU General Public License for more details.                          #
#                                                                        #
#  You should have received a copy of the GNU General Public License     #
#  along with this program.  If not, see <http://www.gnu.org/licenses/>. #
##########################################################################

# Debian package builder and installer for Oracle SQL Developer

#DEBUG=1
#VERBOSE=1

PATH="/usr/bin:/bin:/usr/sbin:/sbin"

if [ ${DEBUG:=0} -ne 0 ] ; then
	trap
	set -x
fi

BASENAME="basename"
BUNZIP2="bunzip2"
CAT="cat"
CHMOD="chmod"
CONVERT="convert"
CUT="cut"
DATE="date"
DEBUILD="debuild"
DIRNAME="dirname"
DPKG="dpkg"
FAKEROOT="fakeroot"
FILE="file"
FIND="find"
FOLD="fold"
FROMDOS="fromdos"
GETOPT="getopt"
GREP="grep"
GUNZIP="gunzip"
HOSTNAME="hostname"
LS="ls"
MKDIR="mkdir"
MKTEMP="mktemp"
MV="mv"
RM="rm"
RMDIR="rmdir"
SED="sed"
TAR="tar"
TOUCH="touch"
TRUE="true"
UNZIP="unzip"
XARGS="xargs"

CHMOD_OPTS="755"
DPKG_OPTS="-i"
FOLD_OPTS="-w 79 -s"
GETOPT_OPTS="-o hb:e:ikl:m:r:u:v -l help,build-dir:,email:,install,keep-dir,revision:,maintainer:,root-command:,upstream-version:,version -s sh"
GREP_OPTS="-E"
MKDIR_OPTS="-p"
MKTEMP_OPTS="-d"
MV_OPTS="--strip-trailing-slashes"
RM_OPTS="-rf"
RMDIR_OPTS="-p --ignore-fail-on-non-empty"
SED_OPTS="-e"

VERSION="0.4.4"
ITP=515233
DEBFULLNAME=${DEBFULLNAME:=""}
DEBEMAIL=${DEBEMAIL:="${USER}@$(${HOSTNAME})"}

HOMEPAGE="http://www.oracle.com/technetwork/developer-tools/sql-developer/"

INSTDIR="usr/share"

CURDIR="${PWD}"
INVOCATION="$(${BASENAME} "${0}")"

# Cleanup at exit
#
func_exit() {
	if [ ${DEBUG} -ne 0 ] ; then
		trap
		set +x
	fi
}

# Trap interrupt signals
#
func_break() {
	func_exit
}

# Trap events
#
func_trap() {
	trap func_exit 0

	if [ ${DEBUG} -eq 0 ] ; then
		trap func_break HUP INT QUIT ABRT
	fi
}

# Display the program version
#
func_version() {
	printf "%s %s\n" "${INVOCATION}" "${VERSION}"
}

# Display the program help
#
func_help() {
	HELP_MSG="Usage:\t${INVOCATION} [options] <file>\n\nOptions:\n\t-b|--build-dir <dir>\n\t\tbase directory to build package (autogenerated)\n\t-e|--email <email>\n\t\tlocal maintainer email (use DEBEMAIL or auto-generate)\n\t-i|--install\n\t\tinstall the generated package - needs root privilege (no)\n\t-k|--keep-dir\n\t\tkeep the build directory (no)\n\t-l|--revision <number>\n\t\tlocal revision to append to package version (1)\n\t-m|--maintainer <name>\n\t\tlocal maintainer name (use DEBFULLNAME or leave empty)\n\t-r|--root-command <command>\n\t\tcommand to gain root privilege for package build (fakeroot)\n\t-u|--upstream-version <version>\n\t\tforce the upstream version (autodetected)\n\t-h|--help\n\t\tdisplay this help screen\n\t-v|--version\n\t\tshow the program version (${VERSION})\n\n\t<file>\n\t\tthe \"Oracle SQL Developer for other platforms\" archive\n\t\tfrom <${HOMEPAGE}>"
	printf "%b\n" "${HELP_MSG}"
}

# Parse the command line options
#
func_opts() {
	GETOPT_RUN="$(${GETOPT} ${GETOPT_OPTS} -n ${INVOCATION} -- "${@}")"
	INSTALL=0
	KEEPDIR=0

	if [ ${?} -ne 0 ] ; then
		printf  "${INVOCATION}: Error parsing command line, terminating...\n" >&2
		exit 1
	fi
	eval set -- "${GETOPT_RUN}"

	while $(${TRUE}) ; do
		case "${1}" in
			-h|--help)
				func_help
				exit 0
			;;
			-b|--build-dir)
				WORKDIR="${2}"
				shift 2
			;;
			-e|--email)
				DEBEMAIL="${2}"
				shift 2
			;;
			-i|--install)
				INSTALL=1
				shift
			;;
			-k|--keep-dir)
				KEEPDIR=1
				shift
			;;
			-l|--revision)
				LOCALVER="${2}"
				shift 2
			;;
			-m|--maintainer)
				DEBFULLNAME="${2}"
				shift 2
			;;
			-r|--root-command)
				ROOTCMD="${2}"
				shift 2
			;;
			-u|--upstream-version)
				UPSTREAMVER="${2}"
				shift 2
			;;
			-v|--version)
				func_version
				exit 0
			;;
			--)
				shift
				break
			;;
			*)
				printf  "${INVOCATION}: Unknown option \"%s\" or internal error, terminating...\n" "${1}" >&2
				exit 1
			;;
		esac
	done

	if [ ${#} -lt 1 ] ; then
		printf "${INVOCATION}: Missing command line parameter, use \"-h\" or \"--help\" for syntax, terminating...\n" >&2
		exit 1
	elif [ ${#} -gt 1 ] ; then
		printf "${INVOCATION}: Extra command line parameter, use \"-h\" or \"--help\" for syntax, terminating...\n" >&2
		exit 1
	else
		ARCHIVE="${1}"

		if ! [ -f "${ARCHIVE}" ] ; then
			printf "${INVOCATION}: \"%s\" not found, aborting...\n" "${ARCHIVE}"
			exit 1
		fi
	fi
}

# Setup the work directory
#
func_workdir() {
	if [ -z "${WORKDIR}" ] ; then
		WORKDIR="$(${MKTEMP} ${MKTEMP_OPTS})" || {
			printf "${INVOCATION}: Unable to create the work directory, aborting...\n" >&2
			exit 1
		}
	else
		if ! [ -d "${WORKDIR}" ] ; then
			if [ -e "${WORKDIR}" ] ; then
				printf "${INVOCATION}: \"%s\" already exists and isn't a directory, aborting...\n" "${WORKDIR}" >&2
				exit 1
			fi

			${MKDIR} ${MKDIR_OPTS} "${WORKDIR}"
		else
			DIRTEST="$(${LS} "${WORKDIR}")"

			if [ ${#DIRTEST} -ne 0 ] ; then
				printf "${INVOCATION}: \"%s\" is not empty, aborting...\n" "${WORKDIR}" >&2
				exit 1
			fi
		fi
	fi

	if [ ${KEEPDIR} -ne 1 ] ; then
		trap "${RM} ${RM_OPTS} ${WORKDIR}" EXIT HUP INT TRAP TERM
	fi

	WORKDIR="${WORKDIR}/sqldeveloper"
	${MKDIR} ${MKDIR_OPTS} "${WORKDIR}/${INSTDIR}"
}

# Extract the archive to the work directory
#
func_extract() {
	if [ ${#} -ne 2 ] ; then
		printf "Usage: func_extract() <archive> <path for extraction>\n"
		return 1
	fi

	TEST4TAR=0
	FILETYPE="$(${FILE} ${FILE_OPTS} "${1}")"

	printf "${FILETYPE}\n" |${GREP} ${GREP_OPTS} "Zip archive data" >/dev/null 2>&1 && {
		ZIPFILE=1
		EXTRACT="${UNZIP}"
		EXTRACT_OPTS="-Xq ${1} -d ${2}"
	} || {
		printf "${FILETYPE}\n" |${GREP} ${GREP_OPTS} "gzip compressed data" >/dev/null 2>&1 && {
			TEST4TAR=1
			GZIPFILE=1
			EXTRACT="${GUNZIP}"
			EXTRACT_OPTS="-q ${1}"
			TEST4TAR_OPTS="-c"
		} || {
			printf "${FILETYPE}\n" |${GREP} ${GREP_OPTS} "bzip2 compressed data" >/dev/null 2>&1 && {
				TEST4TAR=1
				BZIP2FILE=1
				EXTRACT="${BUNZIP2}"
				EXTRACT_OPTS="-q ${1}"
				TEST4TAR_OPTS="-c"
			}
		}
	}

	if [ ${TEST4TAR} -ne 0 ] ; then
		${EXTRACT} ${TEST4TAR_OPTS} "${1}" |${FILE} -b - |${GREP} ${GREP_OPTS} "tar archive" >/dev/null 2>&1 && {
			EXTRACT="${TAR}"

			if [ ${GZIPFILE} -ne 0 ] ; then
				EXTRACT_OPTS="xzf ${1} -C ${2}"
			elif [ ${BZIP2FILE} -ne 0 ] ; then
				EXTRACT_OPTS="xjf ${1} -C ${2}"
			fi
		}
	fi

	${EXTRACT} ${EXTRACT_OPTS}
}

# Set the program version and modifies paths accordingly
#
func_upstreamversion() {
	if [ ${#} -ne 1 ] ; then
		printf "Usage: func_upstreamversion() <directory>\n"
		return 1
	fi

	CUT_OPTS_FUNC_UPVER="-d = -f 2"

	if ! [ -d "${1}" ] ; then
		printf "${INVOCATION} - func_upstreamversion(): Directory \"%s\" not found, returning...\n" "${1}"
		return 1
	fi

	if [ -z "${UPSTREAMVER}" ] ; then
		VERSIONFILE="${1}/${INSTDIR}/sqldeveloper/sqldeveloper/bin/version.properties"

		if [ -e "${VERSIONFILE}" ] ; then
			UPSTREAMVER="$(${GREP} ${GREP_OPTS} "VER_FULL" "${VERSIONFILE}" |${CUT} ${CUT_OPTS_FUNC_UPVER})"
		else
			VERSIONFILE="${1}/${INSTDIR}/sqldeveloper/jdev/bin/version.properties"

			if [ -e "${VERSIONFILE}" ] ; then
				UPSTREAMVER="$(${GREP} ${GREP_OPTS} "VER_FULL" "${VERSIONFILE}" |${CUT} ${CUT_OPTS_FUNC_UPVER})"
			else
				printf "${INVOCATION} - func_upstreamversion(): File \"%s\" not found, returning...\n" "${VERSIONFILE}"
				return 1
			fi
		fi
	fi

	UPSTREAMVERMAJ="${UPSTREAMVER%%.*}"
	UPSTREAMVERMIN="${UPSTREAMVER#*.}"
	UPSTREAMVERREV="${UPSTREAMVERMIN#*.}"
	UPSTREAMVERMIN="${UPSTREAMVERMIN%%.*}"
	UPSTREAMVERREV="${UPSTREAMVERREV%%.*}"

	if [ -z "${LOCALVER}" ] ; then
		LOCALVER=1
	fi

	OLDNAME="${WORKDIR}"
	WORKDIR="${WORKDIR}-${UPSTREAMVER}"
	${MV} ${MV_OPTS} "${OLDNAME}" "${WORKDIR}"
}

# Special case for SQL Developer first released version
#
func_firstver() {
	if [ ${#} -ne 1 ] ; then
		printf "Usage: func_firstver() <directory>\n"
		return 1
	fi

	if ! [ -d "${1}" ] ; then
		printf "${INVOCATION} - func_firstver(): Directory \"%s\" not found, returning...\n" "${1}"
		return 1
	fi

	if ! [ -f "${1}/sqldeveloper" ] ; then
		printf "${INVOCATION} - func_firstver(): File \"%s/sqldeveloper\" not found, this doesn't seem the first version, returning...\n" "${1}"
		return 1
	fi

	TOUCH_OPTS="-r"
	OPTDIR="${1}"

	printf "%s\n" '#!/bin/sh' >"${OPTDIR}/sqldeveloper.sh"
	${CAT} ${CAT_OPTS} "${OPTDIR}/sqldeveloper" >>"${OPTDIR}/sqldeveloper.sh"
	${CHMOD} ${CHMOD_OPTS} "${OPTDIR}/sqldeveloper.sh"
	${TOUCH} ${TOUCH_OPTS} "${OPTDIR}/sqldeveloper" "${OPTDIR}/sqldeveloper.sh"
	${RM} ${RM_OPTS} "${OPTDIR}/sqldeveloper"

	# Place holders
	${TOUCH} ${TOUCH_OPTS} "${OPTDIR}/jdev/lib/ext/README.TXT" "${OPTDIR}/jdev/lib/ext/.placeholder"
	${MV} ${MV_OPTS} "${OPTDIR}/jdev/lib/ext/README.TXT" "${WORKDIR}/usr/share/doc/sqldeveloper/extensions.README.TXT"
	${TOUCH} ${TOUCH_OPTS} "${OPTDIR}/jdev/lib/patches/README.TXT" "${OPTDIR}/jdev/lib/patches/.placeholder"
	${MV} ${MV_OPTS} "${OPTDIR}/jdev/lib/patches/README.TXT" "${WORKDIR}/usr/share/doc/sqldeveloper/patches.README.TXT"
}

# Cleans up the work direcory from cruft and binaries from other OS
#
func_cleanup() {
	if [ ${#} -ne 1 ] ; then
		printf "Usage: func_cleanup() <directory>\n"
		return 1
	fi

	CUT_OPTS_FUNC_CLEAN="-d : -f 1"
	SED_OPTS_INLINE="-i -e"
	OPTDIR="${1}/${INSTDIR}/sqldeveloper"

	if ! [ -d "${OPTDIR}" ] ; then
		printf "${INVOCATION} - func_cleanup(): Directory \"%s\" not found, returning...\n" "${OPTDIR}"
		return 1
	fi

	# Deletes
	#

	# Binaries from other OSs and Zip archives
	${FIND} "${OPTDIR}" ! \( -type d -o -name "*.jar" \) |${XARGS} ${XARGS_OPTS} ${FILE} ${FILE_OPTS} |${GREP} ${GREP_OPTS} "PE32\+ executable|PE32 executable|MS-DOS batch|Zip archive data" |${CUT} ${CUT_OPTS_FUNC_CLEAN} |${XARGS} ${XARGS_OPTS} ${RM} ${RM_OPTS}
	${FIND} "${OPTDIR}" ! -type d \( -iname "*.bat" \) |${XARGS} ${XARGS_OPTS} ${RM} ${RM_OPTS}

	# Image thumbnails and caches
	${FIND} "${OPTDIR}" ! -type d \( -iname "Thumbs.db" \) |${XARGS} ${XARGS_OPTS} ${RM} ${RM_OPTS}

	# Configuration files from other OSs
	${RM} ${RM_OPTS} "${OPTDIR}/sqldeveloper/bin/sqldeveloper-Darwin.conf"

	# Source-code
	${RM} ${RM_OPTS} "${OPTDIR}/jdev/extensions/oracle.jdeveloper.subversion/svnClientAdapter-src.jar"

	# Empty directories
	${FIND} "${OPTDIR}" -type d -empty |${XARGS} ${XARGS_OPTS} ${RM} ${RM_OPTS}

	# Fixes
	#

	# Remove spaces and fix path from shebang line
	for f in $(${FIND} "${OPTDIR}" ! \( -type d -o -name "*.jar" \) |${XARGS} ${XARGS_OPTS} ${FILE} ${FILE_OPTS} |${GREP} ${GREP_OPTS} "ASCII|Perl script" |${CUT} ${CUT_OPTS_FUNC_CLEAN} |${SED} ${SED_OPTS} 's/[[:space:]]/#/g') ; do
		${SED} ${SED_OPTS_INLINE} 's%^ #!%#!%1;s%^#!/usr/bin%#!/bin%1;s%^#!/usr/local%#!/usr%1' "$(printf "${f}\n" |${SED} ${SED_OPTS} 's/#/ /g')"
	done

	# Set executable bit
	${FIND} "${OPTDIR}" ! \( -type d -o -name "*.jar" \) |${XARGS} ${XARGS_OPTS} ${FILE} ${FILE_OPTS} |${GREP} ${GREP_OPTS} "text executable" |${GREP} ${GREP_OPTS} "Perl script|shell script|/bin/ksh"|${CUT} ${CUT_OPTS_FUNC_CLEAN} |${XARGS} ${XARGS_OPTS} ${CHMOD} ${CHMOD_OPTS}

	# Documentation
	#
	${MKDIR} ${MKDIR_OPTS} "${WORKDIR}/usr/share/doc/sqldeveloper"

	# Demo files
	if [ -d "${OPTDIR}/sqldeveloper/demo" ] ; then
		${MV} ${MV_OPTS} "${OPTDIR}/sqldeveloper/demo" "${WORKDIR}/usr/share/doc/sqldeveloper"
	fi

	# Theme files
	if [ -d "${OPTDIR}/ide/themes" ] ; then
		${MKDIR} ${MKDIR_OPTS} "${WORKDIR}/usr/share/doc/sqldeveloper/themes"
		${MV} ${MV_OPTS} "${OPTDIR}/ide/themes/"*.html "${OPTDIR}/ide/themes/"*.png "${WORKDIR}/usr/share/doc/sqldeveloper/themes"
	fi

	# Application notes
	for note in $( ${FIND} "${OPTDIR}" -maxdepth 1 -mindepth 1 ! -type d \( -name "readme.*" -o -name "relnotes.*" \)) ; do
		${MV} ${MV_OPTS} "${note}" "${WORKDIR}/usr/share/doc/sqldeveloper"
	done

	if [ "${UPSTREAMVER}" = "1.0.0" ] ; then
		func_firstver ${OPTDIR}
	fi
}

# Setup the debian directory
#
func_debianize() {
	if [ ${#} -ne 1 ] ; then
		printf "Usage: func_debianize() <directory>\n"
		return 1
	fi

	if ! [ -d "${1}" ] ; then
		printf "${INVOCATION} - func_debianize(): Directory \"%s\" not found, returning...\n" "${1}"
		return 1
	fi

	DATE_OPTS="-R"
	GREP_OPTS_COPYRIGHT="${GREP_OPTS} -c"
	DEBIAN_WORKDIR="${1}/debian"
	${MKDIR} ${MKDIR_OPTS} "${DEBIAN_WORKDIR}"
	(
	${CAT} <<EOF
sqldeveloper (${UPSTREAMVER}+${VERSION}-${LOCALVER}) unstable; urgency=low

  * Built with ${INVOCATION} ${VERSION} (Closes: #${ITP})

 -- ${DEBFULLNAME} <${DEBEMAIL}>  $(${DATE} ${DATE_OPTS})

EOF
	) >"${DEBIAN_WORKDIR}/changelog"
	(
	${CAT} <<EOF
Source: sqldeveloper
Section: non-free/misc
Priority: optional
Maintainer: ${DEBFULLNAME} <${DEBEMAIL}>
Build-Depends: debhelper (>= 10.2.5)
Standards-Version: 4.1.1
Homepage: ${HOMEPAGE}

Package: sqldeveloper
Architecture: all
Depends: xterm | x-terminal-emulator
EOF
	) >"${DEBIAN_WORKDIR}/control"
	printf "Recommends: " >>"${DEBIAN_WORKDIR}/control"
	[ ${UPSTREAMVERMAJ} -gt 3 ] && {
		[ ${UPSTREAMVERMAJ} -gt 4 ] && {
			printf "java8-sdk | java9-sdk" >>"${DEBIAN_WORKDIR}/control"
		} || {
			[ ${UPSTREAMVERMIN} -gt 0 ] && {
				printf "java8-sdk | java9-sdk" >>"${DEBIAN_WORKDIR}/control"
			} || {
				printf "java7-sdk" >>"${DEBIAN_WORKDIR}/control"
			}
		}
	} || {
		printf "java6-sdk" >>"${DEBIAN_WORKDIR}/control"
		[ ${UPSTREAMVERMAJ} -lt 2 ] && printf " | java5-sdk" >>"${DEBIAN_WORKDIR}/control"
	}
	printf "\n" >>"${DEBIAN_WORKDIR}/control"
	(
	${CAT} <<EOF
Description: Oracle SQL Developer
 Oracle SQL Developer is a free graphical tool that enhances productivity and
 simplifies database development tasks.  With SQL Developer, you can browse
 database objects, run SQL statements and SQL scripts, and edit and debug
 PL/SQL statements.  You can also run any number of provided reports, as well
 as create and save your own.  You can connect to and migrate 3rd party
 databases to an Oracle database.
 .
 Oracle SQL Developer is a Java application and requires a full Java SDK.  The
 minimum JDK you should use is as follows:
 .
 Oracle SQL Developer versions up to 1.5.5 require a minimum JDK of 1.5.0_06,
 or if you use JDK 1.6 (JDK6.0), the minimum you should use is JDK 1.6.0_03.
 Oracle SQL Developer versions 2.1 up to 3.1 require a minimum JDK of 1.6.0_11.
 Oracle SQL Developer version 3.2 requires a minimum JDK of 1.6.0_04.
 Oracle SQL Developer version 4.0 requires a minimum JDK of 1.7 (JDK7.0).
 Oracle SQL Developer versions 4.1 up to 17.3  require a minimum  JDK  of  1.8
 (JDK8.0).
 .
 Note that JDK1.7 (JDK7.0) or newer is not supported  by  Oracle SQL Developer
 prior to version 4.0 and that JDK1.8 (JDK8.0) or newer is not supported prior
 to version 4.1.
EOF
	) >>"${DEBIAN_WORKDIR}/control"
	printf "10\n" >"${DEBIAN_WORKDIR}/compat"
	(
	${CAT} <<EOF
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: Oracle SQL Developer
Upstream-Contact: https://www.oracle.com/corporate/contact/index.html
Source: ${HOMEPAGE}
Disclaimer:
 This package is not part of the Debian GNU/Linux distribution. It falls into
 category 2.2.3 of the Debian Policy Manual ("... not compliant with the DFSG
 or are encumbered by patents or other legal issues that make their
 distribution problematic.").
Comment:
 This package was debianized by ${DEBFULLNAME} <${DEBEMAIL}> on
 $(${DATE} ${DATE_OPTS}).

Files: *
Copyright: Copyright © 2009-2017 Oracle USA, Inc.
License: OracleSQLDeveloper

Files: debian/*
Copyright: Copyright © 2009-2017 Lazarus Long <lazarus.long@sapo.pt>
License: GPL-3+

License: OracleSQLDeveloper
 Oracle SQL Developer License Terms
 Oracle SQL Developer Data Modeler License Terms
 .
 Export Controls on the Programs
 Selecting the "Accept License Agreement" button is a confirmation of
 your agreement that you comply, now and during the trial term, with
 each of the following statements:
 .
 -You are not a citizen, national, or resident of, and are not under
  control of, the government of Cuba, Iran, Sudan, Libya, North Korea,
  Syria, nor any country to which the United States has prohibited export.
 -You will not download or otherwise export or re-export the Programs,
  directly or indirectly, to the above mentioned countries nor to citizens,
  nationals or residents of those countries.
 -You are not listed on the United States Department of Treasury lists
  of Specially Designated Nationals, Specially Designated Terrorists,
  and Specially Designated Narcotic Traffickers, nor are you listed on
  the United States Department of Commerce Table of Denial Orders.
 .
 You will not download or otherwise export or re-export the Programs,
 directly or indirectly, to persons on the above mentioned lists.
 .
 You will not use the Programs for, and will not allow the Programs to
 be used for, any purposes prohibited by United States law, including,
 without limitation, for the development, design, manufacture or production
 of nuclear, chemical or biological weapons of mass destruction.
 .
 EXPORT RESTRICTIONS
 You agree that U.S. export control laws and other applicable export
 and import laws govern your use of the programs, including technical
 data; additional information can be found on Oracle®'s Global Trade
 Compliance web site (http://www.oracle.com/products/export).
 .
 You agree that neither the programs nor any direct product thereof will
 be exported, directly, or indirectly, in violation of these laws, or
 will be used for any purpose prohibited by these laws including, without
 limitation, nuclear, chemical, or biological weapons proliferation.
 .
 Oracle Employees: Under no circumstances are Oracle Employees
 authorized to download software for the purpose of distributing it to
 customers. Oracle products are available to employees for internal
 use or demonstration purposes only. In keeping with Oracle's trade
 compliance obligations under U.S. and applicable multilateral law,
 failure to comply with this policy could result in disciplinary action
 up to and including termination.
 .
 Note: You are bound by the Oracle Technology Network ("OTN") License
 Agreement terms. The OTN License Agreement terms also apply to all
 updates you receive under your Technology Track subscription.
 .
 The OTN License Agreement terms below supercede any shrinkwrap license
 on the OTN Technology Track software CDs and previous OTN License terms
 (including the Oracle Program License as modified by the OTN Program
 Use Certificate).
 .
 Oracle SQL Developer License Agreement
 Oracle SQL Developer Data Modeler License Agreement
 .
 "We," "us," and "our" refers to Oracle America, Inc., for and on behalf of
 itself and its subsidiaries and affiliates under common control. "You" and
 "your" refers to the individual or entity that wishes to use the programs
 from Oracle. "Programs" refers to the Oracle software product you wish to
 download and use and program documentation. "License" refers to your right
 to use the programs under the terms of this agreement. This agreement
 is governed by the substantive and procedural laws of California. You
 and Oracle agree to submit to the exclusive jurisdiction of, and venue
 in, the courts of San Francisco, San Mateo, or Santa Clara counties in
 California in any dispute arising out of or relating to this agreement.
 .
 We are willing to license the programs to you only upon the condition
 that you accept all of the terms contained in this agreement. Read the
 terms carefully and select the "Accept" button at the bottom of the
 page to confirm your acceptance. If you are not willing to be bound
 by these terms, select the "Do Not Accept" button and the registration
 process will not continue.
 .
 LICENSE RIGHTS
 We grant you a nonexclusive, nontransferable limited license to use
 the programs solely for your business operations and any third party
 training as part of such business operations. We may audit your use
 of the programs. Program documentation may be accessed online at
 http://www.oracle.com/technetwork/indexes/documentation/index.html.
 .
 Ownership and Restrictions
 We retain all ownership and intellectual property rights in the
 programs. You may make a sufficient number of copies of the programs
 for the licensed use and one copy of the programs for backup purposes.
 .
 You may not:
 - remove or modify any program markings or any notice of our proprietary
   rights;
 - make the programs available in any manner to any third party, other
   than as specified above;
 - use the programs for any purpose other than as provided above;
 - assign this agreement or give or transfer the programs or an interest
   in them to another individual or entity;
 - cause or permit reverse engineering (unless required by law for
   interoperability), disassembly or decompilation of the programs;
 - disclose results of any program benchmark tests without our prior
   consent.
 .
 Export
 You agree that U.S. export control laws and other applicable
 export and import laws govern your use of the programs,
 including technical data; additional information can be
 found on Oracle's Global Trade Compliance web site located at
 http://www.oracle.com/products/export/index.html?content.html. You
 agree that neither the programs nor any direct product thereof will be
 exported, directly, or indirectly, in violation of these laws, or will
 be used for any purpose prohibited by these laws including, without
 limitation, nuclear, chemical, or biological weapons proliferation.
 .
 Disclaimer of Warranty and Exclusive Remedies
 THE PROGRAMS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. WE
 FURTHER DISCLAIM ALL WARRANTIES, EXPRESS AND IMPLIED, INCLUDING WITHOUT
 LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE OR NONINFRINGEMENT.
 .
 IN NO EVENT SHALL WE BE LIABLE FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
 PUNITIVE OR CONSEQUENTIAL DAMAGES, OR DAMAGES FOR LOSS OF PROFITS,
 REVENUE, DATA OR DATA USE, INCURRED BY YOU OR ANY THIRD PARTY, WHETHER
 IN AN ACTION IN CONTRACT OR TORT, EVEN IF WE HAVE BEEN ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGES. OUR ENTIRE LIABILITY FOR DAMAGES HEREUNDER
 SHALL IN NO EVENT EXCEED ONE THOUSAND DOLLARS (U.S. \$1,000).
 .
 Trial Programs Included With Orders
 We may include additional programs with an order which may be used for
 trial purposes only. You will have 30 days from the delivery date to
 evaluate these programs. Any use of these programs after the 30 day
 trial period requires you to obtain the applicable license. Programs
 licensed for trial purposes are provided "as is" and we do not provide
 technical support or any warranties for these programs.
 .
 Technical Support
 Our technical support organization does not provide technical support,
 phone support, or updates specifically for the programs licensed under
 this agreement. However, if you have a supported license of an Oracle
 database program, then the technical support organization will provide
 technical support, phone support for the program licensed hereunder in
 conjunction with the Oracle database program license.
 .
 End of Agreement
 You may terminate this agreement by destroying all copies of the
 programs. We have the right to terminate your right to use the programs
 if you fail to comply with any of the terms of this agreement, in which
 case you shall destroy all copies of the programs.
 .
 Relationship Between the Parties
 The relationship between you and us is that of licensee/licensor. Neither
 party will represent that it has any authority to assume or create
 any obligation, express or implied, on behalf of the other party,
 nor to represent the other party as agent, employee, franchisee, or
 in any other capacity. Nothing in this agreement shall be construed
 to limit either party's right to independently develop or distribute
 software that is functionally similar to the other party's products,
 so long as proprietary information of the other party is not included
 in such software.
 .
 Open Source
 Third party technology that may be appropriate or necessary for use with
 the program may be specified in the program documentation. To the extent
 stated in the program documentation, such third party technology is
 licensed to you under the terms of the third party technology license
 agreement specified in the program documentation and not under the
 terms of this agreement. Nothing in this agreement should be construed
 as modifying or limiting your rights to use such third party technology
 under the terms of the specified third party license.
 .
 Entire Agreement
 You agree that this agreement is the complete agreement for the programs
 and licenses, and this agreement supersedes all prior or contemporaneous
 agreements or representations. If any term of this agreement is found
 to be invalid or unenforceable, the remaining provisions will remain
 effective.
 .
 Last updated: 09/17/10 (jlr)
 .
 Should you have any questions concerning this License Agreement, or if
 you desire to contact Oracle for any reason, please write:
 .
 Oracle America, Inc.
 500 Oracle Parkway,
 Redwood City, CA 94065
 .
 Oracle may contact you to ask if you had a satisfactory experience
 installing and using this OTN software download.

License: GPL-3+
 This package is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3 of the License, or (at your option) any later
 version.
 .
 This package is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 details.
 .
 You should have received a copy of the GNU General Public License along with
 this package; if not, write to the Free Software Foundation, Inc., 51
 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 .
 On Debian systems, the complete text of the GNU General Public License
 version 3, can be found in \`/usr/share/common-licenses/GPL-3'.
EOF
	) >"${DEBIAN_WORKDIR}/copyright"

	if [ -f "${1}/${INSTDIR}/sqldeveloper/sqldeveloper/extensions/oracle.sqldeveloper.onsd/LICENSE.txt" ] ; then
		(
		${CAT} <<EOF

Files: sqldeveloper/sqldeveloper/extensions/oracle.sqldeveloper.onsd*
Copyright: Copyright © 2009-2017 Oracle USA, Inc.
License: Apache-2.0
EOF
		) >>"${DEBIAN_WORKDIR}/copyright"

		${RM} ${RM_OPTS} "${1}/${INSTDIR}/sqldeveloper/sqldeveloper/extensions/oracle.sqldeveloper.onsd/LICENSE.txt"
	fi

	if [ -d "${1}/${INSTDIR}/sqldeveloper/jdev/extensions/oracle.jdeveloper.git/license" ] ; then
		(
		${CAT} <<EOF

Files: sqldeveloper/jdev/extensions/oracle.jdeveloper.git*
Copyright: Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors.
License: Eclipse-1.0

License: Eclipse-1.0
EOF
		) >>"${DEBIAN_WORKDIR}/copyright"

		for l in ${1}/${INSTDIR}/sqldeveloper/jdev/extensions/oracle.jdeveloper.git/license/* ; do
			LICFILE="$(${BASENAME} ${l})"

			${CAT} "${l}" | ${FROMDOS} |${SED} ${SED_OPTS} 's/^[[:space:]]*//g' |${FOLD} ${FOLD_OPTS} |${SED} ${SED_OPTS} 's/^$/./g;s/^/ /g' >>"${DEBIAN_WORKDIR}/copyright"
		done

		${RM} ${RM_OPTS} ${1}/${INSTDIR}/sqldeveloper/jdev/extensions/oracle.jdeveloper.git/license/*
		${RMDIR} ${RMDIR_OPTS} "${1}/${INSTDIR}/sqldeveloper/jdev/extensions/oracle.jdeveloper.git/license"
	fi


	if [ -d "${1}/${INSTDIR}/sqldeveloper/jdev/extensions/oracle.jdeveloper.subversion/licenses" ] ; then
		(
		${CAT} <<EOF

Files: sqldeveloper/jdev/extensions/oracle.jdeveloper.subversion*
Copyright: Copyright (c) 2004-2009 TMate Software. All rights reserved.
License: Apache-2.0 or TMate

License: TMate
EOF
		) >>"${DEBIAN_WORKDIR}/copyright"

		for l in ${1}/${INSTDIR}/sqldeveloper/jdev/extensions/oracle.jdeveloper.subversion/licenses/* ; do
			LICFILE="$(${BASENAME} ${l})"

			if [ "${LICFILE}" = "license.txt" ] ; then
				continue
			fi

			${CAT} "${l}" | ${FROMDOS} |${SED} ${SED_OPTS} 's/^[[:space:]]*//g' |${FOLD} ${FOLD_OPTS} |${SED} ${SED_OPTS} 's/^$/./g;s/^/ /g' >>"${DEBIAN_WORKDIR}/copyright"
		done

		${RM} ${RM_OPTS} ${1}/${INSTDIR}/sqldeveloper/jdev/extensions/oracle.jdeveloper.subversion/licenses/*
		${RMDIR} ${RMDIR_OPTS} "${1}/${INSTDIR}/sqldeveloper/jdev/extensions/oracle.jdeveloper.subversion/licenses"
	fi

	if [ -d "${1}/${INSTDIR}/sqldeveloper/svnkit/licenses" ] ; then
		(
		${CAT} <<EOF

Files: sqldeveloper/svnkit/svnClientAdapter.jar
Copyright: Copyright (c) 2004-2009 TMate Software. All rights reserved.
License: Apache-2.0 or TMate

Files: sqldeveloper/svnkit/sqljet.jar
Copyright: Copyright (C) 2009-2010 TMate Software Ltd
License: GPL-2 or TMate

Files: sqldeveloper/svnkit/antlr-runtime.jar
Copyright: Copyright (c) 2003-2008 Terence Parr
License: BSD-3-clause

Files: sqldeveloper/svnkit/svnjavahl.jar
Copyright: Copyright (c) 2000-2005 CollabNet.  All rights reserved.
License: CollabNet-1

Files: sqldeveloper/svnkit/sequence.jar
Copyright: Copyright (c) 2000-2008 SyntEvo GmbH, Ainring, GERMANY.
License: SyntEvo

Files: sqldeveloper/svnkit/trilead.jar
Copyright: Copyright (c) 2007-2008 Trilead AG (http://www.trilead.com)
           Copyright (c) 2005 - 2006 Swiss Federal Institute of Technology
            (ETH Zurich), Department of Computer Science
            (http://www.inf.ethz.ch), Christian Plattner. All rights reserved.
           Copyright (c) 2000 - 2004 The Legion Of The Bouncy Castle
            (http://www.bouncycastle.org)
License: TriLead-ETHZurich-TheLegionOfTheBouncyCastle

Files: sqldeveloper/svnkit/svnkit.jar
Copyright: Copyright (c) 2004-2009 TMate Software. All rights reserved.
License: TMate
EOF
		) >>"${DEBIAN_WORKDIR}/copyright"

		for l in ${1}/${INSTDIR}/sqldeveloper/svnkit/licenses/* ; do
			LICFILE="$(${BASENAME} ${l})"

			case "${LICFILE}" in
				"COPYING")
					LICNAME="TMate" ;;
				"JAVAHL-LICENSE")
					LICNAME="CollabNet-1" ;;
				"SEQUENCE-LICENSE")
					LICNAME="SyntEvo" ;;
				"TRILEAD-LICENSE")
					LICNAME="TriLead-ETHZurich-TheLegionOfTheBouncyCastle" ;;
				*)
					continue ;;
			esac

			if [ $(${GREP} ${GREP_OPTS_COPYRIGHT} "^License: ${LICNAME}$" "${DEBIAN_WORKDIR}/copyright") -gt 1 ] ; then
				continue
			fi

			printf "\nLicense: %s:\n" "${LICNAME}" >>"${DEBIAN_WORKDIR}/copyright"
			${CAT} "${l}" | ${FROMDOS} |${SED} ${SED_OPTS} 's/^[[:space:]]*//g' |${FOLD} ${FOLD_OPTS} |${SED} ${SED_OPTS} 's/^$/./g;s/^/ /g' >>"${DEBIAN_WORKDIR}/copyright"
		done

		${RM} ${RM_OPTS} ${1}/${INSTDIR}/sqldeveloper/svnkit/licenses/*
		${RMDIR} ${RMDIR_OPTS} "${1}/${INSTDIR}/sqldeveloper/svnkit/licenses"
	fi

	if [ $(${GREP} ${GREP_OPTS} "^License:" "${DEBIAN_WORKDIR}/copyright" |${GREP} ${GREP_OPTS_COPYRIGHT} "Apache-2") -gt 0 ] ; then
		(
		${CAT} <<EOF

License: Apache-2.0
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at
 .
      http://www.apache.org/licenses/LICENSE-2.0
 .
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 .
 On Debian systems, the full text of the Apache Software License version 2 can
 be found in the file \`/usr/share/common-licenses/Apache-2.0'.
EOF
		) >>"${DEBIAN_WORKDIR}/copyright"
	fi

	if [ $(${GREP} ${GREP_OPTS} "^License:" "${DEBIAN_WORKDIR}/copyright" |${GREP} ${GREP_OPTS_COPYRIGHT} "GPL-2\+") -gt 0 ] ; then
		(
		${CAT} <<EOF

License: GPL-2+
 This package is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
 Foundation; either version 2 of the License, or (at your option) any later
 version.
 .
 This package is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 details.
 .
 You should have received a copy of the GNU General Public License along with
 this package; if not, write to the Free Software Foundation, Inc., 51
 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 .
 On Debian systems, the complete text of the GNU General Public License
 version 2, can be found in \`/usr/share/common-licenses/GPL-2'.
EOF
		) >>"${DEBIAN_WORKDIR}/copyright"
	elif [ $(${GREP} ${GREP_OPTS} "^License:" "${DEBIAN_WORKDIR}/copyright" |${GREP} ${GREP_OPTS_COPYRIGHT} "GPL-2") -gt 0 ] ; then
		(
		${CAT} <<EOF

License: GPL-2
 This package is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
 Foundation; specifically version 2 of the License.
 .
 This package is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 details.
 .
 You should have received a copy of the GNU General Public License along with
 this package; if not, write to the Free Software Foundation, Inc., 51
 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 .
 On Debian systems, the complete text of the GNU General Public License
 version 2, can be found in \`/usr/share/common-licenses/GPL-2'.
EOF
		) >>"${DEBIAN_WORKDIR}/copyright"
	fi

	if [ $(${GREP} ${GREP_OPTS} "^License:" "${DEBIAN_WORKDIR}/copyright" |${GREP} ${GREP_OPTS_COPYRIGHT} "BSD-3-clause") -gt 0 ] ; then
		(
		${CAT} <<EOF

License: BSD-3-clause
 All rights reserved.
 .
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. Neither the name of the University nor the names of its contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.
 .
 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.
EOF
		) >>"${DEBIAN_WORKDIR}/copyright"
	fi

	(
	${CAT} <<EOF
debian/sqldeveloper.desktop usr/share/applications
debian/sqldeveloper.xpm usr/share/pixmaps
${INSTDIR}/* ${INSTDIR}
EOF
	) >"${DEBIAN_WORKDIR}/install"
	printf "debian/sqldeveloper.1\n" >"${DEBIAN_WORKDIR}/manpages"
	(
	${CAT} <<EOF
sqldeveloper for Debian
-----------------------

In order to run Oracle SQL Developer you'll need a full working JDK. The
versions that are compatible with SQL Developer are any JDK 1.5.0_06 or above
or JDK 1.6.0_03 or above for SQL Developer versions up to 1.5.5. SQL Developer
versions from 2.1 to 3.1 are compatible with JDK 1.6.0_11 or above and SQL
Developer version 3.2 is compatible with JDK 1.6.0_04 or above. SQL Developer
version 4.0 is compatible with JDK 1.7 (JDK7.0) series and SQL Developer
versions from 4.1 to 17.3 are compatible with JDK 1.8 (JDK8.0) series or
newer.

Note that Oracle SQL Developer prior to version 4.0 is not compatible with JDK
1.7 (JDK7.0) or newer and prior to version 4.1 is not compatible with.JDK 1.8
(JDK8.0) or newer.

There are several ways to obtain a compatible JDK:

- install default-jdk (java), making sure it depends on the required JDK
  version
- install one of the required openjdk-6-jdk, openjdk-7-jdk, openjdk-8-jdk or
  openjdk-9-jdk (java)
- install the meta-package java-sdk, making sure that it installs the required
  JDK version as dependency
- install one of the meta-packages java6-sdk, java7-sdk, java8-sdk or
  java9-sdk, making sure that it installs the required JDK version as
  dependency
- download and run the installer for the version you wish to install from
  Oracle <http://www.oracle.com/technetwork/java/javase/downloads/>
- install java-package (contrib/misc) and generate a Debian package from the
  above installer

After installing a compatible JDK simply launch SQL Developer through the
graphical interface or by invoking /usr/bin/sqldeveloper and supply the path
where the JDK was installed when prompted.

 -- Lazarus Long <lazarus.long@sapo.pt>  $(${DATE} ${DATE_OPTS})
EOF
	) >"${DEBIAN_WORKDIR}/README.Debian"
	(
	${CAT} <<EOF
.\\" sqldeveloper.1
.\\"
.\\" (2017-10-29)
.\\"
.\\" Copyright © 2009-2017 Lazarus Long <lazarus (dot) long (at) sapo (dot) pt>
.\\"
.\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\"
.\\"  This program is free software: you can redistribute it and/or modify  .\\"
.\\"  it under the terms of the GNU General Public License as published by  .\\"
.\\"  the Free Software Foundation, either version 3 of the License, or     .\\"
.\\"  (at your option) any later version.                                   .\\"
.\\"                                                                        .\\"
.\\"  This program is distributed in the hope that it will be useful,       .\\"
.\\"  but WITHOUT ANY WARRANTY; without even the implied warranty of        .\\"
.\\"  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         .\\"
.\\"  GNU General Public License for more details.                          .\\"
.\\"                                                                        .\\"
.\\"  You should have received a copy of the GNU General Public License     .\\"
.\\"  along with this program.  If not, see <http://www.gnu.org/licenses/>. .\\"
.\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\".\\"
.\\"
.\\" Debian package builder and installer for Oracle SQL Developer
.\\"
.\\"
.TH SQLDEVELOPER 1 2017-10-29 Oracle "Oracle SQL Developer"
.\\"
.SH NAME
sqldeveloper \\- Oracle SQL Developer
.\\"
.SH SYNOPSIS
.B sqldeveloper
.RI [options]\\  [[file]\\ \\...]
.\\"
.SH DESCRIPTION
\\fBsqldeveloper\\fP is a \\fIJava\\fP utility that allows to develop for and
administer \\fIOracle\\fP databases.
.PP
The \\fIOracle SQL Developer\\fP program is governed by the copyright holder
(\\fIOracle USA, Inc.\\fP), it is your responsibility to use it accordingly to
the OTN license, a copy of which is included in "\\fI/usr/share/doc/sqldeveloper\\fP".
.\\"
.SH OPTIONS
.PP
.B The following commands must appear first:
.TP
\\fB\\-verbose\\fP
Show java command line options
.TP
\\fB\\-conf[igure]\\fP \\fI<fname>\\fP
Use the specified configuration file
.PP
.B The following commands must appear last:
.TP
\\fB\\-classic\\fP
Use Classic as the Java VM
.TP
\\fB\\-hotspot\\fP
Use Hotspot client as the Java VM
.TP
\\fB\\-server\\fP
Use Hotspot server as the Java VM
.TP
\\fB\\-client\\fP
Use Hotspot client as the Java VM
.TP
\\fB\\-\\-<directive>=\\fP\\fI<value>\\fP
Override a directive from the configuration file
.TP
\\fB\\-J\\fP\\fI<flag>\\fP
Pass <flag> directly to the runtime system
.TP
\\fB\\-migrate\\fP
Migrate user settings from a previous installation
.PP
.B The following commands must appear by themselves:
.TP
\\fB\\-help\\fP
Show the help screen
.TP
\\fB\\-version\\fP
Display SQL Developer version
.PP
.TP
\\fB[[file] ...]\\fP
One or more SQL files specified withe their full pathname(s)
.\\"
.SH AUTHOR
\\fBOracle SQL Developer\\fP is copyright by \\fIOracle USA, Inc.\\fP
.PP
The \\fBsqldeveloper\\fP wrapper for the Debian package was written by \\fILazarus
Long <lazarus.long@sapo.pt>\\fP.
.PP
This manual page was written by \\fILazarus Long <lazarus.long@sapo.pt>\\fP,
for the Debian project (but may be used by others).
EOF
	) >"${DEBIAN_WORKDIR}/sqldeveloper.1"
	(
	${CAT} <<EOF
#!/bin/sh
set -e

# sqldeveloper

# (2017-10-29)

##########################################################################
#  This program is free software: you can redistribute it and/or modify  #
#  it under the terms of the GNU General Public License as published by  #
#  the Free Software Foundation, either version 3 of the License, or     #
#  (at your option) any later version.                                   #
#                                                                        #
#  This program is distributed in the hope that it will be useful,       #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#  GNU General Public License for more details.                          #
#                                                                        #
#  You should have received a copy of the GNU General Public License     #
#  along with this program.  If not, see <http://www.gnu.org/licenses/>. #
##########################################################################

# Launcher for Oracle SQL Developer

# Copyright: © 2009-2017 Lazarus Long <lazarus (dot) long (at) sapo (dot) pt>

PATH="/usr/bin:/bin:/${INSTDIR}/sqldeveloper:/${INSTDIR}/sqldeveloper/sqldeveloper/bin:/${INSTDIR}/sqldeveloper/jdev/bin"

BASENAME="basename"
CAT="cat"
CUT="cut"
GREP="grep"
MKDIR="mkdir"
RM="rm"
SED="sed"
TR="tr"
XTERM="x-terminal-emulator"

GREP_OPTS="-E"
MKDIR_OPTS="-p"
RM_OPTS="-f"
TR_OPTS="[[:upper:]] [[:lower:]]"
XTERM_OPTS="-e"

INVOCATION="\$(\${BASENAME} "\${0}")"
SQLDEVHOME="\${HOME}/.sqldeveloper"
SQLDEVDIR="/${INSTDIR}/sqldeveloper"
SQLDEVELOPER="\${SQLDEVDIR}/sqldeveloper.sh"

func_version() {
	CUT_OPTS_FUNC_VER="-d = -f 2"

	VERSIONFILE="\${SQLDEVDIR}/sqldeveloper/bin/version.properties"
	VERSION="0.0.0"

	if [ -e "\${VERSIONFILE}" ] ; then
		VERSION="\$(\${GREP} \${GREP_OPTS} "VER_FULL" "\${VERSIONFILE}" |\${CUT} \${CUT_OPTS_FUNC_VER})"
	else
		VERSIONFILE="\${SQLDEVDIR}/jdev/bin/version.properties"

		if [ -e "\${VERSIONFILE}" ] ; then
			VERSION="\$(\${GREP} \${GREP_OPTS} "VER_FULL" "\${VERSIONFILE}" |\${CUT} \${CUT_OPTS_FUNC_VER})"
		else
			printf "\${INVOCATION} - func_version(): File \\"%s\\" not found, returning...\\n" "\${VERSIONFILE}"
			return 1
		fi
	fi

	VERSIONMAJ="\${VERSION%%.*}"
	VERSIONMIN="\${VERSION#*.}"
	VERSIONREV="\${VERSIONMIN#*.}"
	VERSIONMIN="\${VERSIONMIN%%.*}"
	VERSIONREV="\${VERSIONREV%%.*}"
}

func_help() {
	\${@}
	printf "%b\\n" "The following commands must appear by themselves:\\n-help                  Show this screen\\n-version               Display SQL Developer version"
}

func_parseopts() {
	while [ \${#} -gt 0 ] ; do
		case "\$(printf "%s" \${1} |\${TR} \${TR_OPTS})" in
			-help|--help)
				func_help \${RUN} \${1}
				exit 0
			;;
			-version|--version)
				printf "\\nOracle SQL Developer: Release %s\\n" "\${VERSION}"
				exit 0
			;;
		esac

		shift
	done
}


func_checkjdkpath() {
		CUT_OPTS_FUNC_CHKJDK="-f 2-"
		SED_OPTS_FUNC_CHKJDK="-i -e"

	if [ \${VERSIONMAJ} -lt 4 ] ; then
		JDKPATH="\${SQLDEVHOME}/jdk"
	else
		if [ \${VERSIONMAJ} -eq 17  -a \${VERSIONMIN} -lt 3 ] ; then
			JDKPATH="\${SQLDEVHOME}/4.2.0/product.conf"
		else
			JDKPATH="\${SQLDEVHOME}/\${VERSIONMAJ}.\${VERSIONMIN}.\${VERSIONREV}/product.conf"
		fi
	fi

	if ! [ -f "\${JDKPATH}" ] ; then
		\${MKDIR} \${MKDIR_OPTS} "\${SQLDEVHOME}"
		RUN="\${XTERM} \${XTERM_OPTS} \${SQLDEVELOPER}"
	else
		if [ \${VERSIONMAJ} -lt 4 ]  ; then 
			if ! [ -x "\$(\${CAT} \${JDKPATH})/bin/javac" ] ; then
				\${RM} \${RM_OPTS} "\${JDKPATH}"
				RUN="\${XTERM} \${XTERM_OPTS} \${SQLDEVELOPER}"
			else
				RUN="\${SQLDEVELOPER}"
			fi
		else
			if ! [ -x "\$(\${GREP} \${GREP_OPTS} "^SetJavaHome" \${JDKPATH} |\${CUT} \${CUT_OPTS_FUNC_CHKJDK} -d\\  )/bin/javac" ] ; then
				\${SED} \${SED_OPTS_FUNC_CHKJDK} 's/^SetJavaHome.*$//g' "\${JDKPATH}"
				RUN="\${XTERM} \${XTERM_OPTS} \${SQLDEVELOPER}"
			else
				RUN="\${SQLDEVELOPER}"
			fi
		fi
	fi
}

func_version
func_parseopts \${@}
func_checkjdkpath
\${RUN} \${@}

# EOF sqldeveloper
EOF
	) >"${DEBIAN_WORKDIR}/sqldeveloper.bash"
	(
	${CAT} <<EOF
[Desktop Entry]
Version=1.0
Name=SQL Developer
Exec=/usr/bin/sqldeveloper
Icon=/usr/share/pixmaps/sqldeveloper.xpm
Type=Application
Comment=Oracle SQL Developer
X-KDE-StartupNotify=true
Terminal=false
Categories=Database;Office;Development;KDE;Qt
EOF
	) >"${DEBIAN_WORKDIR}/sqldeveloper.desktop"
	${CONVERT} ${CONVERT_OPTS} "${1}/${INSTDIR}/sqldeveloper/icon.png" "${DEBIAN_WORKDIR}/sqldeveloper.xpm"
	(
	${CAT} <<EOF
codeless-jar
classpath-contains-relative-path
EOF
	) >"${DEBIAN_WORKDIR}/sqldeveloper.lintian-overrides"
	(
	${CAT} <<EOF
#!/usr/bin/make -f

# verbose mode
#export DH_VERBOSE=1

override_dh_builddeb:
	dh_builddeb -i --destdir="${CURDIR}"

override_dh_install:
	dh_install
	install -o 0 -g 0 -m 0755 -D debian/sqldeveloper.bash debian/sqldeveloper/usr/bin/sqldeveloper

override_dh_strip_nondeterminism:

%:
	dh \$@
EOF
	) >"${DEBIAN_WORKDIR}/rules"
	${CHMOD} ${CHMOD_OPTS} "${DEBIAN_WORKDIR}/rules"
}

# Generate the Debian package
#
func_builddeb() {
	if [ ${#} -ne 1 ] ; then
		printf "Usage: func_builddeb() <directory>\n"
		return 1
	fi

	if ! [ -d "${1}/debian" ] ; then
		printf "${INVOCATION} - func_builddeb(): Directory \"%s/debian\" not found, returning...\n" "${1}"
		return 1
	fi

	DEBUILD_OPTS="--no-lintian --no-tgz-check -- binary"

	if [ -n "${ROOTCMD}" ] ; then
		DEBUILD_OPTS="--rootcmd=${ROOTCMD} ${DEBUILD_OPTS}"
	fi

	cd "${WORKDIR}"
	printf "${INVOCATION}: Building package for sqldeveloper v%s in \"%s\".\n" "${UPSTREAMVER}" "${CURDIR}"
	${DEBUILD} ${DEBUILD_OPTS} >/dev/null
	cd "${CURDIR}"

	if [ ${INSTALL} -eq 1 ] ; then
		${DPKG} ${DPKG_OPTS} "${CURDIR}/sqldeveloper_${UPSTREAMVER}-${LOCALVER}_all.deb"
	fi
}

# Run the script
#
func_run() {
	func_trap
	func_opts "${@}"
	func_workdir
	func_extract "${ARCHIVE}" "${WORKDIR}/${INSTDIR}"
	func_upstreamversion "${WORKDIR}"
	func_cleanup "${WORKDIR}"
	func_debianize "${WORKDIR}"
	func_builddeb "${WORKDIR}"
}

func_run "${@}"

# EOF make-sqldeveloper-package
