#!/bin/bash
# LinuxMania : setup less XGA display ORCA Ubuntu 10.04 32bit deb ; 2011/03/09

if [ "$1" != 'terminal' ]; then
	/usr/bin/gnome-terminal -e "sudo bash -c \"$0 terminal\""
	sleep 3
	exit $?
fi

TMP=$(mktemp -d)
LOG=$TMP/log
head -n 2 "$0" | sed -n '2,1 p' | tee -a $LOG
echo ""
echo "  このスクリプトは、Ubuntu 10.04 ORCA モデルにおいて、ディスプレイの解像度が"
echo "  XGA (1024 x 768 ) の場合の設定を行います。"
echo ""
echo -n "  設定を開始しますか。[y/n]"
read line
if [ "$line" != "y" ] && [ "$line" != "yes" ] && [ "$line" != "Y" ] && [ "$line" != "YES" ] ; then
	echo
	echo "中止しました。"
	sleep 3
	exit
fi
echo ""

rc=0
function fnc_exit() {
rc=$1
echo ""
nlog=${0%sh}$(date +%Y-%m-%d-%H-%M-%S).log
cp -p $LOG $nlog
echo "ログ： $nlog"
if [ $rc -eq 0 ]; then
	echo -e "設定が完了しました。 [\033[32mOK\033[0m]"
	echo "設定を反映させるために、一度ログアウトしてください。"
else
	echo -e "エラーがあったようです。 [\033[31mエラー\033[0m]"
	echo "LinxuManiaカスタマーセンターまでお問い合わせいただけますでしょうか。"
fi
echo ""
echo "[Enter] キーで終了します。"
read line
exit
}

echo "開始：$(date)" >> $LOG 2>&1

if [ $(uname -m) != 'i686' ]; then
	echo "$(uname -m) には対応していません。" | tee -a $LOG
	fnc_exit 16
fi

SUDO_GID=$(cat /etc/passwd | grep -E ^${SUDO_USER}: | cut -d : -f 4)
SUDO_GROUP=$(cat /etc/group | grep :${SUDO_GID}: | cut -d : -f 1)

skel_dir='/etc/skel'
# devilspie の設定
echo "devilspie の設定" | tee -a $LOG

# .devilspie/gnome-panel.ds

mkdir -vp ${skel_dir}/.devilspie 												>> $LOG 2>&1 || fnc_exit 16
cat > ${skel_dir}/.devilspie/gnome-panel.ds <<EOF 								2>>$LOG || fnc_exit 16
(if
  (and
    (is (window_class) "Gnome-panel")
    (not (is (application_name) "gnome-panel")))
  (below)
)
EOF
mkdir -vp $HOME/.devilspie 													>> $LOG 2>&1 || fnc_exit 16
cp -vp ${skel_dir}/.devilspie/gnome-panel.ds $HOME/.devilspie 					>> $LOG 2>&1 || fnc_exit 16
chown -vR $SUDO_USER:$SUDO_GROUP $HOME/.devilspie 								>> $LOG 2>&1 || fnc_exit 16

# .config/autostart/devilspie.desktop
mkdir -vp ${skel_dir}/.config/autostart 										>> $LOG 2>&1 || fnc_exit 16
cat > ${skel_dir}/.config/autostart/devilspie.desktop <<EOF 					2>>$LOG || fnc_exit 16
[Desktop Entry]
Type=Application
Encoding=UTF-8
Version=1.0
Name=devilspie
Name[ja_JP]=devilspie
Exec=devilspie
X-GNOME-Autostart-enabled=true
EOF
mkdir -vp $HOME/.config/autostart 												>> $LOG 2>&1 || fnc_exit 16
cp -vp ${skel_dir}/.config/autostart/devilspie.desktop $HOME/.config/autostart >> $LOG 2>&1 || fnc_exit 16
chown -vR $SUDO_USER:$SUDO_GROUP $HOME/.config/autostart 						>> $LOG 2>&1 || fnc_exit 16

# パネルの位置の変更
echo "パネルの位置の変更" | tee -a $LOG

# gconftool -t string -s /apps/panel/toplevels/top_panel_screen0/orientation bottom
# cat /apps/panel/toplevels/top_panel_screen0/%gconf.xml | awk 'BEGIN{flag=1};flag{print}/orientation/{flag=0;print"\t\t<stringvalue>bottom</stringvalue>"};/\/entry/{print;flag=1}'

gconf_path=apps/panel/toplevels/top_panel_screen0
gconf_change_regex='s#<stringvalue>top</stringvalue>#<stringvalue>bottom</stringvalue>#'
perl -pi -e ${gconf_change_regex} ${skel_dir}/.gconf/${gconf_path}/%gconf.xml 	>> $LOG 2>&1 || fnc_exit 16
mkdir -vp $HOME/.gconf/${gconf_path} 											>> $LOG 2>&1 || fnc_exit 16
cp -vp ${skel_dir}/.gconf/${gconf_path}/%gconf.xml $HOME/.gconf/${gconf_path} 	>> $LOG 2>&1 || fnc_exit 16
chown -vR $SUDO_USER:$SUDO_GROUP $HOME/.gconf/${gconf_path} 					>> $LOG 2>&1 || fnc_exit 16

fnc_exit 0
___END___

