#!/bin/sh

# Application Directory Enabler 0.2
# Copyright(C) 2005 m.kato <mkato@par.odn.ne.jp>

# 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 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#__________________________________________________________________
# ΥץȤʾǼ¹ԤƤ뤫ɤåޤ

dir=`pwd`
dir=`basename $dir`
pwd
echo --- Start of apps-enabler ---

read -p "press Enter $dir" tmp000

#___________________________________________
#     ȥǥ쥯ȥ̾åޤ
case $dir in
    Apps|apps)
        echo ǥ쥯ȥ å OK!
        ;;
    *)
        echo Ŭڤʥǥ쥯ȥ [$dir] Ǽ¹Ԥ褦ȤƤޤ
        read -p "λޤpress Enter " tmp000        
        exit 1
esac

# ______________________________________________
#     ֥ǥ쥯ȥ꤬ͭǧޤ
if [ -d ./AppsConf/.archives ] ; then
    echo  å OK
else
    echo Ŭڤʥǥ쥯ȥ [$dir] Ǽ¹Ԥ褦ȤƤޤ
    echo [֥ǥ쥯ȥ꤬ޤ]
    read -p "λޤpress Enter " tmp000        
    exit 1
fi

# ______________________________________________
#     .DirIcon 򥳥ԡޤ
cp -f ./AppsConf/.archives/.DirIcon.png .DirIcon

# ______________________________________________
# ¸ΥƥΥǥ쥯ȥöޤ

for dir in ./AppsConf/.archives/* ; do
    # my_apps Ϻޤ
    if [ `basename $dir` = my_apps ] ; then
        continue
    fi
    pdir=`pwd`
    pdir=$pdir/`basename $dir`
    if [ -d $pdir ] ; then
        echo ƹΰ١ö [$pdir] ޤ
        rm -r $pdir
    fi
done
echo
read -p "Ǥޤ press Enter. [ctrl+C for Abort]" tmp

# ________________________________________________
# μ¹Բǽ AppDir ΤߤŸޤ
for dir in ./AppsConf/.archives/* ; do
    # ~/Apps/my_apps ¸ߤ.archives/my_apps Ÿޤ
    if [ `basename $dir` = my_apps ] ; then
        if [ -d ./my_apps ] ; then
            continue
        fi
    fi
    cp -a $dir .
    # ǥ쥯ȥʳ򥳥ԡϺʲν򥹥åפޤ
    # ǥ쥯ȥξϥԡǥ쥯ȥ pushd ǰưޤ
    if ! pushd `basename $dir` > /dev/null ; then
        rm ./`basename $dir`
        continue
    fi
    #read -p"$dir processing ------------------------------"
    # ƥ/ǥ쥯ȥ LINKs Ÿޤ
    # LINKs ñŸΤߤԤޤ
    if [ `basename $dir` = LINKs ] ; then
        for url in * ; do
            tar zxvf $url
            rm $url
        done
        popd > /dev/null
        # Υǥ쥯ȥäƼΥƥ/ǥ쥯ȥޤ
        continue
    fi
    # ƥ/ǥ쥯ȥ AppDir Ÿޤ
    for app in * ; do
        # ץ꤬󥹥ȡ뤵Ƥ뤫ɤĴ٤ޤ
        if `type $app 1>& /dev/null` ; then
            # 󥹥ȡѤߤʤ  AppDir Ÿͭˤޤ
            echo "extract "[$app]
            tar zxvf $app
        else
           # ̤󥹥ȡʤ饹åפޤ
            echo "    [    "$app is none. skip]
        fi
        # ƥ/ǥ쥯ȥΥ֤ޤ
        rm $app
    done
    # Υǥ쥯ȥäƼΥƥ/ǥ쥯ȥޤ
    popd > /dev/null
done
read -p "Extract complated. press Enter " tmp000

# ______________________________________________
# ¸Υץåöޤ
if [ -f  ./AppsConf/.archives/.Rox-Utils.tgz ] ; then
    pushd ./AppsConf/.archives
    tar zxf .Rox-Utils.tgz
    popd
    for dir in ./AppsConf/.archives/.Rox-Utils/* ; do
        pdir=`pwd`
        pdir=$pdir/`basename $dir`
        if [ -d $pdir ] ; then
            rm -r $pdir
            echo ƹΰ١ö [$pdir] ޤ
        fi
    done
    echo
    read -p "Ǥޤ press Enter. [ctrl+C for Abort]" tmp
# ____________________________________________
# rox  AppDir ȥåץ٥˥ԡޤ
# ͭΤϥԡޤ
    for dir in ./AppsConf/.archives/.Rox-Utils/* ; do
        if ! [ -d `basename $dir` ] ; then
            #  AppDir ̵Хԡޤ
            echo
            echo Copy Rox-Utils [`basename $dir`]
            cp -a $dir .
        fi
    done
    read -p " --- End of apps-enabler ---" tmp000
    rm -rf ./AppsConf/.archives/.Rox-Utils
fi

# ____________________________________________
# ~/Apps/.archives ѤŸե
# ξ֤ᤷޤ
mv  ./AppsConf/.archives/arcs.tgz ./
rm -rf  ./AppsConf/.archives/*
rm -f   ./AppsConf/.archives/.DirIcon.png
mv  ./arcs.tgz ./AppsConf/.archives/

kterm -geom 55x4 -e ./AppsConf/lastmsg
exit 0
