#!/bin/bash
#

#set -x

if [ ! -f "gvSIG.sh" ] ; then
  echo "Usage: $0"
  echo "  Current folder can be a gvSIG installation"
  exit -1
fi

GVSIG_INSTALL_FOLDER="$PWD"
TARGET_FOLDER="/tmp/gvsig-deb"

function message() {
  echo $@
}


initArchitecture() {
  architecture=""
  cd "$GVSIG_INSTALL_FOLDER"
  eval $(find . -name package.info -exec grep "architecture=[^a]" {} ';' | sort | head -n 1)
  if [ "${architecture}" == "" ] ; then
    echo "Can't determine the gvSIG installation architecture." >&2
    exit 1
  fi
  if [ "${architecture/*x86_64*/x86_64}" == "x86_64" ] ; then
    GVSIG_ARCHITECTURE="amd64"
  else
    GVSIG_ARCHITECTURE="x86"
  fi
  cd - >/dev/null
}

CORE_VERSION=$(echo lib/org.gvsig.andami-*.jar)
CORE_VERSION=${CORE_VERSION/lib\/org.gvsig.andami-/}
CORE_VERSION=${CORE_VERSION/.jar/}
GVSIG_VERSION=$(sed -n 's/^version=\(.*\)/\1/p' package.info)
GVSIG_BUILDNUMBER=$(sed -n 's/^buildNumber=\(.*\)/\1/p' package.info)
initArchitecture


sudo rm -rf "$TARGET_FOLDER"
mkdir -p "$TARGET_FOLDER/DEBIAN"
sed "s/\${GVSIG_ARCHITECTURE}/${GVSIG_ARCHITECTURE}/
s/\${GVSIG_VERSION}/${GVSIG_VERSION}/
s/\${GVSIG_BUILDNUMBER}/${GVSIG_BUILDNUMBER}/
s/\${CORE_VERSION}/${CORE_VERSION}/" "$GVSIG_INSTALL_FOLDER/tools/debian.control" >"$TARGET_FOLDER/DEBIAN/control"

#=============================
# Load debian.control values
#
typeset -l Package
eval $(sed -n 's/\([A-Za-z0-9]*\)[ ]*:[ ]*\(.*\)/\1="\2"/p' "$TARGET_FOLDER/DEBIAN/control")

#=============================
# Prepare target folder

message "Prepare package folder in $TARGET_FOLDER"

mkdir -p "$TARGET_FOLDER/DEBIAN"
mkdir -p "$TARGET_FOLDER/usr/bin"
mkdir -p "$TARGET_FOLDER/usr/share/applications"
mkdir -p "$TARGET_FOLDER/usr/share/menu"
mkdir -p "$TARGET_FOLDER/usr/share/pixmaps"
mkdir -p "$TARGET_FOLDER/usr/local/bin"
mkdir -p "$TARGET_FOLDER/usr/local/lib/${Package}/${Version}-${Architecture}"

#===========================================
# Create gvsig launcher in /usr/local/bin

message "Create gvsig launcher in $TARGET_FOLDER/usr/local/bin"
echo "#!/bin/sh
exec /usr/local/lib/${Package}/default/gvSIG.sh \$@
" > "$TARGET_FOLDER/usr/local/bin/${Package}"

#============================================
# Link gvSIG launcher to /usr/bin

message "Link gvSIG launcher to $TARGET_FOLDER/usr/bin"
cd "$TARGET_FOLDER/usr/bin"
ln -s "../local/bin/${Package}" .

#============================================
# Link installation to default folder

message "Link installation to default folder"
cd "$TARGET_FOLDER/usr/local/lib/${Package}"
ln -s "${Version}-${Architecture}" "default"

#===========================================
# Create postinst for install dsktop icons

message "Creating post installation script"
cat <<EOF-postinst >"$TARGET_FOLDER/DEBIAN/postinst"
#!/bin/sh
set -e
if [ "$1" = "configure" ] && [ -x "`which update-menus 2>/dev/null`" ]; then
	update-menus
fi
EOF-postinst


#===========================================
# Create menu entry file, icon and desktop file

message "Create desktop file, icon and menu entry file"

DesktopCategory="Graphics"
cp "$GVSIG_INSTALL_FOLDER/gvsig-icon48x48.png" "$TARGET_FOLDER/usr/share/pixmaps/${Package}.png"

cat <<EOF-menu >"$TARGET_FOLDER/usr/share/menu/${Package}"
?package(${Package}):command="/usr/local/bin/${Package}" icon="/usr/share/pixmaps/${Package}.png" needs="X11" section="Applications/${DesktopCategory}" title="gvSIG desktop" hotkey="gvSIG"
EOF-menu

cat <<EOF-desktop-file >"$TARGET_FOLDER/usr/share/applications/${Package}.desktop"
Name=gvSIG desktop
Version=${Version}
Exec=${Package}
Comment=
Icon=/usr/share/pixmaps/${Package}.png
Type=Application
Terminal=false
StartupNotify=true
Encoding=UTF-8
Categories=${DesktopCategory};
EOF-desktop-file

#============================================
# Copy files to target folder

message "Copy installation files..."
cd "$GVSIG_INSTALL_FOLDER"

cp -R . "$TARGET_FOLDER/usr/local/lib/${Package}/${Version}-${Architecture}"

#============================================
# Fix permissions

message "Fix permissions"
cd "$TARGET_FOLDER"

find . -type d -exec chmod go=rx,u=rwx {} ';'
find . -type f -exec chmod go=r,u=rw {} ';'

chmod go=rx,u=rwx "DEBIAN/postinst"
chmod go=r,u=rw "usr/share/applications/${Package}.desktop"
chmod go=rx,u=rwx "usr/local/bin/${Package}"
chmod go=rx,u=rwx "usr/local/lib/${Package}/${Version}-${Architecture}/gvSIG.sh"

sudo chown -R root.root .

#============================================
# Create debian package

cd "$TARGET_FOLDER"

message "Creating debian package in $TARGET_FOLDER/${Package}_${Version}_${Architecture}.deb"
sudo dpkg -b . "${Package}_${Version}_${Architecture}.deb"

