#!/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/

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