#!/bin/sh
#
# Copyright (C) 2011 Rinat Bikov <becase@altlinux.org>
# 
# Script to fixing StarBoard installation script
# 
# This file 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 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

# Root of StarBoardSoftware installation
sroot=/usr/local/StarBoardSoftware

if [ ! -d "$sroot" ]; then
    echo "Please, install StarBoardSoftware first.";
    exit 1;
fi

uid=`id | cut -d= -f2 | cut -d\( -f1`;
if [ "$uid" != "0" ]; then
    echo "This script must be run under root.";
    exit 1;
fi

echo -n "Fixing ... ";

sed -i "s/sudo//g" $sroot/install.sh

for i in installation-scripts/{,un}install.sh installation-tools/{reinstalldriver.sh,starboardservice} ; do
  sed -i "s/kernel\/drivers\/usb\/input/lsadrv/g" $sroot/$i
done;

#delete ldconfig
for i in installation-scripts/{,un}install.sh ; do
  sed -i "s/ldconfig/#ldconfig/g" $sroot/$i
done;

sed -i "s/LD_LIBRARY_PATH=\"/LD_LIBRARY_PATH=\"\/usr\/local\/lsadrv\/lib:/g" $sroot/StarBoard.sh
sed -i 's/echo $LSADRV_DEST\/lib/true #/g' $sroot/installation-scripts/install.sh

sed -i "s/MODULE=0/MODULE=1/g" $sroot/installation-scripts/install.sh
sed -i "s/DISTRO=/DISTRO=ALTLinux#/g" $sroot/installation-scripts/install.sh
sed -i 's/$DEFAULTSLIST =/"$DEFAULTSLIST" =/g' $sroot/installation-scripts/install.sh
sed -i 's/alterservice.pl -cx/cp $ROOT\/installation-tools\/starboardservice \/etc\/init.d\/;alterservice.pl -cx/g' $sroot/installation-scripts/install.sh

# Set installation scripts verbose
#sed -i 's/#!\/bin\/bash/#!\/bin\/bash -x/g' $sroot/installation-scripts/install.sh 
#sed -i 's/#!\/bin\/sh/#!\/bin\/sh -x/g' $sroot/installation-tools/install_touchscreen_xorg

sed -i "s/binary builder source uspace app/x11 uspace app/g" $sroot/installation-scripts/install.sh
sed -i 's/zypper/echo "zypper removed"; #zypper/g' $sroot/installation-scripts/install_sub.sh
sed -i 's/src\/linux/src\/linux-`uname -r`/g' $sroot/installation-scripts/install_sub.sh
for i in examineSystem detectX11 ; do
  sed -i "s/usr\/lib\/xorg/usr\/lib\/X11/g" $sroot/installation-tools/$i.pl
done
sed -i "s/--set//g" $sroot/installation-tools/alterservice.pl

#sed -i 's/SUBSYSTEM="usb_device"/SUBSYSTEM="usb", ENV{DEVTYPE}="usb_device"/g' $sroot/installation-tools/42-starboard.rules # needed?

echo "DONE";
