#!/bin/sh

#install path
dir_last=/usr
destdir=/usr/realdesktop
user_name=$(whoami| awk '{print $1}')
myPath="/usr/lib/qt4/"  

if [ "$user_name"x = "root"x ];then
    dir_last=/usr
    destdir=/usr/realdesktop
    echo $user_name
else
    dir_last=/home/$user_name
    destdir=/home/$user_name/realdesktop
    echo "current user is $user_name"
fi

destlib=/usr/lib
lib_dir=/lib
libqtcore=/usr/lib/qt4/plugins/codecs
lib_share=/usr/share
qttranslate=/usr/share/qt4/translations
lib_plugin=/usr/lib/qt4/plugins
curr_path=$(pwd)
#echo $curr_path

#remove old files
echo "remove old files..."
if [ "$user_name"x = "root"x ];then
    cd /$user_name/Desktop
    rm realordesktop.desktop
fi

cd $dir_last
rm -rf realdesktop
mkdir realdesktop
chmod 777 realdesktop
#echo $(pwd)

cd $destlib
rm -f libcrypto.so libssl.so libcrypto.so.0.9.8 libssl.so.0.9.8 
rm -f libpoppler-glib.so.4.0.0 libpoppler.so.5.0.0 libpoppler.so libpoppler.so.5 libpoppler-glib.so libpoppler-glib.so.4

if [ ! -d "$myPath" ]; then 
rm -f libQtCore.so libQtGui.so libQtNetwork.so libQtCore.so.4.6.2 libQtGui.so.4.6.2 libQtNetwork.so.4.6.2 
rm -f libQtCore.so.4 libQtGui.so.4 libQtNetwork.so.4 libQtCore.so.4.6 libQtGui.so.4.6 libQtNetwork.so.4.6 
rm -f libaudio.so.2.4 libaudio.so libaudio.so.2 libQtCLucene.so.4.6.2 libQtCLucene.so libQtCLucene.so.4 libQtCLucene.so.4.6
rm -rf qt4

cd $lib_share
rm -rf qt4

cd $destlib
mkdir qt4
cd $destlib/qt4
mkdir plugins
cd $destlib/qt4/plugins
mkdir codecs

cd $lib_share
mkdir qt4
cd $lib_share/qt4
mkdir translations

cd $curr_path/install
cp libQtCore.so.4.6.2 $destlib/libQtCore.so.4.6.2
cp libQtGui.so.4.6.2 $destlib/libQtGui.so.4.6.2
cp libQtNetwork.so.4.6.2 $destlib/libQtNetwork.so.4.6.2
cp libQtCLucene.so.4.6.2 $destlib/libQtCLucene.so.4.6.2
cp libqcncodecs.so $libqtcore/libqcncodecs.so
cp libqjpcodecs.so $libqtcore/libqjpcodecs.so
cp libqkrcodecs.so $libqtcore/libqkrcodecs.so
cp libqtwcodecs.so $libqtcore/libqtwcodecs.so
cp qt_zh_CN.qm $qttranslate/qt_zh_CN.qm

cp -r accessible $lib_plugin/accessible
cp -r graphicssystems $lib_plugin/graphicssystems
cp -r imageformats $lib_plugin/imageformats
cp -r inputmethods $lib_plugin/inputmethods

ln -s $destlib/libQtCore.so.4.6.2  $destlib/libQtCore.so
ln -s $destlib/libQtCore.so.4.6.2  $destlib/libQtCore.so.4
ln -s $destlib/libQtCore.so.4.6.2  $destlib/libQtCore.so.4.6
ln -s $destlib/libQtGui.so.4.6.2  $destlib/libQtGui.so
ln -s $destlib/libQtGui.so.4.6.2  $destlib/libQtGui.so.4
ln -s $destlib/libQtGui.so.4.6.2  $destlib/libQtGui.so.4.6
ln -s $destlib/libQtNetwork.so.4.6.2  $destlib/libQtNetwork.so
ln -s $destlib/libQtNetwork.so.4.6.2  $destlib/libQtNetwork.so.4
ln -s $destlib/libQtNetwork.so.4.6.2  $destlib/libQtNetwork.so.4.6
ln -s $destlib/libQtCLucene.so.4.6.2 $destlib/libQtCLucene.so.4.6
ln -s $destlib/libQtCLucene.so.4.6.2 $destlib/libQtCLucene.so.4
ln -s $destlib/libQtCLucene.so.4.6.2 $destlib/libQtCLucene.so
fi

cd $curr_path/install
cp libcrypto.so.0.9.8 $lib_dir/libcrypto.so.0.9.8
cp libssl.so.0.9.8 $lib_dir/libssl.so.0.9.8
cp libpoppler.so.5.0.0 $destlib/libpoppler.so.5.0.0
cp libpoppler-glib.so.4.0.0 $destlib/libpoppler-glib.so.4.0.0
cp libaudio.so.2.4 $destlib/libaudio.so.2.4

#create link for library
ln -s $lib_dir/libcrypto.so.0.9.8  $destlib/libcrypto.so
ln -s $lib_dir/libcrypto.so.0.9.8  $destlib/libcrypto.so.0.9.8
ln -s $lib_dir/libssl.so.0.9.8  $destlib/libssl.so
ln -s $lib_dir/libssl.so.0.9.8  $destlib/libssl.so.0.9.8
ln -s $destlib/libpoppler.so.5.0.0 $destlib/libpoppler.so
ln -s $destlib/libpoppler.so.5.0.0 $destlib/libpoppler.so.5
ln -s $destlib/libpoppler-glib.so.4.0.0 $destlib/libpoppler-glib.so
ln -s $destlib/libpoppler-glib.so.4.0.0 $destlib/libpoppler-glib.so.4
ln -s $destlib/libaudio.so.2.4 $destlib/libaudio.so
ln -s $destlib/libaudio.so.2.4 $destlib/libaudio.so.2

#copy files
echo "install files..."
cd $curr_path/install
#echo $(pwd)
cp newtest $destdir/newtest
cp vdeskset.conf $destdir/vdeskset.conf
cp Serverset.conf $destdir/Serverset.conf
cp rdesktop $destdir/rdesktop
cp rdesktop2 $destdir/rdesktop2
#cp qtsysicon2 $destdir/qtsysicon2
cp qt_zh_CN.qm $destdir/qt_zh_CN.qm
cp qtsysicon $destdir/qtsysicon
cp vdesktop.qm $destdir/vdesktop.qm
cp qtsysicon.qm $destdir/qtsysicon.qm
cp rdesktop.qm $destdir/rdesktop.qm
cp epdfview $destdir/epdfview
cp test.sh $destdir/test.sh
cp createstart.sh $destdir/createstart.sh
cp daletestart.sh $destdir/daletestart.sh
#cp runtest.sh $destdir/runtest.sh
cp realordesktop.desktop $destdir/realordesktop.desktop

cp -r doc $destdir/doc
cp -r images $destdir/images
cp -r keymaps $destdir/keymaps
cp -r pixmaps $destdir/pixmaps
cp -r ui $destdir/ui

#create link to desktop
echo "create desktop shotcut..."
cd $destdir
chmod 777 newtest rdesktop rdesktop2 qtsysicon qtsysicon2 epdfview test.sh realordesktop.desktop createstart.sh daletestart.sh

if [ "$user_name"x = "root"x ];then
    cp $destdir/realordesktop.desktop /root/Desktop/realordesktop.desktop
    cp $destdir/realordesktop.desktop /root/桌面/realordesktop.desktop
    echo "#!/bin/sh" > 2.txt
    echo "cd $destdir" >> 2.txt
    echo "./newtest" >> 2.txt
   
    cp 2.txt $destdir/runtest.sh
    chmod 777 $destdir/runtest.sh
    #rm 2.txt
    echo $user_name
else
    #cp $destdir/realordesktop2.desktop /home/$user_name/Desktop/realordesktop2.desktop
    echo "#!/usr/bin/env xdg-open" > 1.txt
    echo "" >> 1.txt
    echo "[Desktop Entry]" >> 1.txt
    echo "Version=1.0"      >> 1.txt
    echo "Type=Application" >> 1.txt
    echo "Terminal=false"   >> 1.txt
    echo "Icon[en_US]=/home/$user_name/realdesktop/images/shotcut.png" >> 1.txt
    echo "Name[en_US]=瑞友天翼虚拟化客户端"  >> 1.txt
    echo "Exec=/home/$user_name/realdesktop/runtest.sh" >> 1.txt
    echo "Comment[en_US]=瑞友天翼虚拟化客户端" >> 1.txt
    echo "Name=瑞友天翼虚拟化客户端" >> 1.txt
    echo "Comment=瑞友天翼虚拟化客户端" >> 1.txt
    echo "Icon=/home/$user_name/realdesktop/images/shotcut.png" >> 1.txt

    cp 1.txt /home/$user_name/Desktop/realordesktop.desktop
    chmod 777 /home/$user_name/Desktop/realordesktop.desktop
    cp 1.txt /home/$user_name/桌面/realordesktop.desktop
    chmod 777 /home/$user_name/桌面/realordesktop.desktop
    rm 1.txt

    echo "#!/bin/sh" > 2.txt
    echo "cd $destdir" >> 2.txt
    echo "./newtest" >> 2.txt
   
    cp 2.txt $destdir/runtest.sh
    chmod 777 $destdir/runtest.sh
    #rm 2.txt
    echo "current user is $user_name"
fi

cd $curr_path
echo "install end..."
exit
