#!/bin/bash
# This file is part of the X-Plane 8 installer for Linux.
# You do not need to copy this file or edit this file.  Simply
# run the application "Install X-Plane 8" on the DVD to install
# X-Plane 8.

# linux-install.sh
# Linux install shell script
# X-System
# 2004-11-08
# Copyright (c) 2004 Joshua Wise. All rights reserved.

fail() {
	echo "Cannot proceed. Sorry. If you have any questions about this error, contact X-Plane Linux technical support at <linux@x-plane.com>."
	exit 2
}

echo "X-System Installer"
echo
echo -n "Where would you like to install X-System? "
read installpath

installorbit=false
installfullus=false
echo -n "Do you wish to install Earth low-orbit images? These bitmaps take up approximately 450MB on your hard drive. [y/N] "
read ok
if [ "x$ok" == xy -o "x$ok" == xY ]; then
  installorbit=true
fi

echo -n "Do you wish to install scenery for the entire United States? This will take up approximately 2GB on your hard drive. [y/N] "
read ok
if [ "x$ok" == xy -o "x$ok" == xY ]; then
  installfullus=true
fi


echo "Okay, to recap, I will:"
echo " - Nuke $installpath"
echo " - (Re-)create $installpath"
echo " - Unpack base X-Plane resources into $installpath"
echo " - Unpack Linux-specific binaries into $installpath"
if $installfullus; then
	echo " - Install full U. S. scenery into $installpath"
else
	echo " - Skip installing full U. S. scenery"
fi
if $installorbit; then
	echo " - Install orbit textures into $installpath"
else
	echo " - Skip installing orbit textures"
fi

echo -n "Is this OK? [y/N] "
read ok
if [ "x$ok" != xy -a "x$ok" != xY ]; then
  echo "Okay, bailing."
  exit 1
fi

cdpath=`pwd`

echo -n "Doing a quick sanity check first... "
if ! unzip -v >/dev/null 2>/dev/null; then
  echo "could not find unzip. Please make sure that it is in your path."
  fail
fi
if [ ! -f data2/linapps.tar.bz2 ]; then
  echo "could not find needed data on CD. Make sure that you are running this script from the CD."
  fail
fi
echo "Ok. Your system seems sane enough."


echo -n "Nuking $installpath... "
rm -rf $installpath || fail
echo "done"

echo -n "Creating $installpath... "
mkdir $installpath || fail
echo "done"

echo -n "Unpacking base X-Plane resources into $installpath... "
cd $installpath || fail
unzip -qq $cdpath/data1/xsystem.zip || fail
echo "done"

echo -n "Unpacking Linux-specific binaries into $installpath... "
tar xjf $cdpath/data2/linapps.tar.bz2 || fail
echo "done"

if $installfullus; then 
	echo -n "Unpacking full U. S. scenery... "
	mkdir -p "Resources/default scenery/US/Earth nav data" || fail
	cd "Resources/default scenery/US/Earth nav data" || fail
	for i in $cdpath/data1/scenery/*.zip; do
		sceneryname=`echo $i | sed -e s"#^$cdpath/data1/scenery/##" -e 's#.zip$##'`
		echo -n "[$sceneryname"
		mkdir $sceneryname || fail
		cd $sceneryname || fail
		unzip -qq $cdpath/data1/scenery/$sceneryname.zip || fail
		cd .. || fail
		echo -n "] "
	done
else
	echo -n "Unpacking base SoCal scenery... "
	mkdir -p "Resources/default scenery/US/Earth nav data" || fail
	cd "Resources/default scenery/US/Earth nav data" || fail
	for i in $cdpath/data1/scenery/+30-120.zip; do
		sceneryname=`echo $i | sed -e "s#^$cdpath/data1/scenery/##" -e 's#.zip$##'`
		echo -n "[$sceneryname"
		mkdir $sceneryname || fail
		cd $sceneryname || fail
		unzip -qq $cdpath/data1/scenery/$sceneryname.zip || fail
		cd .. || fail
		echo -n "] "
	done
fi
echo "done"

cd $installpath || fail
if $installorbit; then
	echo -n "Unpacking Earth low-orbit scenery... "
	mkdir -p "Resources/bitmaps/earth orbit textures" || fail
	cd "Resources/bitmaps/earth orbit textures" || fail
	unzip -qq $cdpath/data1/earth_texes.zip || fail
	echo "done"
fi

cd $installpath || fail
echo -n "Doing a few last-minute cleanups... "
cp -R Resources/preferences Resources/Preferences || fail
for i in English/ French/ German/ Italian/ Korean/ Portugese/ Spanish/; do
	mv Resources/menus/$i Resources/menus/`echo $i | tr '[:upper:]' '[:lower:]'` || fail
	mv Resources/text/$i Resources/text/`echo $i | tr '[:upper:]' '[:lower:]'` || fail
done
mv Resources/bitmaps/cockpit/-PANELS- Resources/bitmaps/cockpit/-panels- || fail
mv Resources/bitmaps/runways/asphalt/Numbers Resources/bitmaps/runways/asphalt/numbers || fail
mv Resources/bitmaps/runways/concrete/Numbers Resources/bitmaps/runways/concrete/numbers || fail
mv Resources/bitmaps/runways/Dirt Resources/bitmaps/runways/dirt || fail
mv Resources/bitmaps/runways/Grass Resources/bitmaps/runways/grass || fail
mv Resources/bitmaps/runways/Grav Resources/bitmaps/runways/grav || fail
mv Resources/bitmaps/runways/Lakebed Resources/bitmaps/runways/lakebed || fail
mv "Resources/Mars Nav Data" "Resources/Mars nav data" || fail
echo "done"
echo
echo "Cool. If you didn't see any errors, it looks like installation is complete."
echo "Please read the README if you have not already done so."
echo "Questions? Contact X-Plane Linux technical support at <linux@x-plane.com>."
