#! /sbin/installer/sh

chk_me=`whoami | grep root`
if [ "$chk_me.x" == ".x" ]; then
    echo "Need root priviledge to update your system."
    exit
fi

# for etc-5.0_64-noarch-P34

echo "Update to etc-5.0_64-noarch-P32"
cat << "EOF" | patch -p0
--- /etc/template/Source/.bash_login	2013-05-10 23:16:13.000000000 +0900
+++ /etc/template/Source/.bash_login	2015-01-06 10:57:24.000000000 +0900
@@ -24,7 +24,7 @@ if [ $SHLVL -eq 1 ] ; then
   if [ -x /usr/bin/esdctl ] && ! esdctl serverinfo &> /dev/null ; then
     if [ -x /usr/bin/esd ] ; then
       ( setsid dbus-launch esd -r 48000 & ) &> /dev/null
-      ( setsid sh -c "sleep 5 ; /usr/sbin/alsactl restore" & ) &> /dev/null
+      ( setsid sh -c "sleep 5 ; /usr/sbin/alsactl -L restore" & ) &> /dev/null
     fi
   fi
 fi
diff -uprN Old/work/etc/template/Source/.bashrc ./work/etc/template/Source/.bashrc
--- /etc/template/Source/.bashrc	2012-10-01 19:43:16.000000000 +0900
+++ /etc/template/Source/.bashrc	2014-12-27 07:45:59.000000000 +0900
@@ -43,8 +43,8 @@ PAGER='less'
 export LESS EDITOR PAGER
 
 # for Python 2.7
-PYTHONPATH=/usr/lib64/python2.7/site-packages:/opt/kde/lib64/python2.7/site-packages
-export PYTHONPATH
+#PYTHONPATH=/usr/lib64/python2.7/site-packages:/opt/kde/lib64/python2.7/site-packages
+#export PYTHONPATH
 
 # if [ $SHLVL = 1 ] ; then
 #   PATH="$PATH:." ;
diff -uprN Old/work/etc/template/Source/.emacs.d/site-start.d/look.el ./work/etc/template/Source/.emacs.d/site-start.d/look.el
--- /etc/template/Source/.emacs.d/site-start.d/look.el	2011-08-31 15:18:54.000000000 +0900
+++ /etc/template/Source/.emacs.d/site-start.d/look.el	2014-12-29 13:14:36.000000000 +0900
@@ -29,9 +29,9 @@
 ;; 
 
 ;;; メニューバーを日本語にします
-(setq menu-coding-system 'euc-japan)
-(if (equal (substring (concat (getenv "LANG") "__") 0 2) "ja")
-    (load "menu-tree"))
+;(setq menu-coding-system 'euc-japan)
+;(if (equal (substring (concat (getenv "LANG") "__") 0 2) "ja")
+;    (load "menu-tree"))
 
 ;;; Window システムで起動した場合
 (if window-system
diff -uprN Old/work/etc/template/Source/.login ./work/etc/template/Source/.login
--- /etc/template/Source/.login	2013-05-10 23:16:51.000000000 +0900
+++ /etc/template/Source/.login	2015-01-06 10:57:33.000000000 +0900
@@ -24,7 +24,7 @@ if ( $SHLVL == 1 ) then
   if ( -x /usr/bin/esdctl && ! { esdctl serverinfo } ) then >& /dev/null
     if ( -x /usr/bin/esd ) then
       ( setsid dbus-launch esd -r 48000 & ) >& /dev/null
-      ( setsid sh -c "sleep 5 ; /usr/sbin/alsactl restore" & ) >& /dev/null
+      ( setsid sh -c "sleep 5 ; /usr/sbin/alsactl -L restore" & ) >& /dev/null
     endif
   endif
 endif
diff -uprN Old/work/etc/template/Source/.qvwmrc ./work/etc/template/Source/.qvwmrc
--- /etc/template/Source/.qvwmrc	2009-06-16 08:12:39.000000000 +0900
+++ /etc/template/Source/.qvwmrc	2014-12-20 04:39:43.000000000 +0900
@@ -43,7 +43,7 @@ RestoreButtonMessage	= "元のサイズに戻す
 ; 仮想画面
 PagingResistance	= 250		; ページ切替えの際の抵抗
 PagingMovement		= 100		; ページ切替えの際の移動量(%)
-PagingBeltSize		= 2		; 画面端のページ切替え可能領域
+PagingBeltSize		= 0		; 画面端のページ切替え可能領域
 TopLeftPage		= -1-1		; 仮想画面の左上ページ
 PagingSize		= 3x3		; 仮想画面の大きさ
 PagingSpeed		= 5		; ページを切り替える段階数
@@ -116,7 +116,7 @@ ScreenSaverDelay	= 600		; スクリーンセー
 ; サウンド
 EnableSound		= True		; サウンドを有効にする
 EnableAlsa		= False		; ALSA を使う(Linux のみ)
-EnableEsd		= False		; EsounD を使う
+EnableEsd		= True		; EsounD を使う
 
 ; テーマ
 include			~/.qvwm-theme
diff -uprN Old/work/etc/template/Source/.tcshrc ./work/etc/template/Source/.tcshrc
--- /etc/template/Source/.tcshrc	2011-09-30 02:00:22.000000000 +0900
+++ /etc/template/Source/.tcshrc	2014-12-27 07:46:14.000000000 +0900
@@ -33,7 +33,7 @@ setenv EDITOR 'vi'
 setenv PAGER 'less'
 
 # for Python 2.7
-setenv PYTHONPATH /usr/lib64/python2.7/site-packages:/opt/kde/lib64/python2.7/site-packages
+#setenv PYTHONPATH /usr/lib64/python2.7/site-packages:/opt/kde/lib64/python2.7/site-packages
 
 # NetNews のサーバを指定してください
 # setenv NNTPSERVER news.hoge.hoge.ac.jp
diff -uprN Old/work/etc/template/Source/.xinitrc ./work/etc/template/Source/.xinitrc
--- /etc/template/Source/.xinitrc	2013-11-22 08:56:00.000000000 +0900
+++ /etc/template/Source/.xinitrc	2014-12-30 14:26:24.000000000 +0900
@@ -106,8 +106,8 @@ fi
 # jmode を使う場合はこちら
 #XMODIFIERS="@im=jmode"
 # 上記を使う場合はこちらも
-QT_IM_MODULE=xim
-GTK_IM_MODULE=xim
+#QT_IM_MODULE=xim
+#GTK_IM_MODULE=xim
 # uim を使う場合はこちら
 XMODIFIERS="@im=uim"
 QT_IM_MODULE=uim
diff -uprN Old/work/etc/template/Source/.zlogin ./work/etc/template/Source/.zlogin
--- /etc/template/Source/.zlogin	2013-05-10 23:17:21.000000000 +0900
+++ /etc/template/Source/.zlogin	2015-01-06 10:57:45.000000000 +0900
@@ -22,7 +22,7 @@ if [ $SHLVL -eq 1 ] ; then
   if [ -x /usr/bin/esdctl ] && ! esdctl serverinfo &> /dev/null ; then
     if [ -x /usr/bin/esd ] ; then
       ( setsid dbus-launch esd -r 48000 & ) &> /dev/null
-      ( setsid sh -c "sleep 5 ; /usr/sbin/alsactl restore" & ) &> /dev/null
+      ( setsid sh -c "sleep 5 ; /usr/sbin/alsactl -L restore" & ) &> /dev/null
     fi
   fi
 fi
diff -uprN Old/work/etc/template/Source/.zshrc ./work/etc/template/Source/.zshrc
--- /etc/template/Source/.zshrc	2011-09-30 01:59:50.000000000 +0900
+++ /etc/template/Source/.zshrc	2014-12-27 07:46:25.000000000 +0900
@@ -30,13 +30,13 @@ PAGER='less'
 export LESS EDITOR PAGER
 
 # for Python 2.7
-PYTHONPATH=/usr/lib64/python2.7/site-packages:/opt/kde/lib64/python2.7/site-packages
+#PYTHONPATH=/usr/lib64/python2.7/site-packages:/opt/kde/lib64/python2.7/site-packages
 
 HISTFILE=$HOME/.zhistory
 
 # color-ls
 # 色設定等は ~/.dir_colors
-if which dircolors >& /dev/null; then
+if [ -x /usr/bin/dircolors ]; then
   eval `dircolors -b ~/.dir_colors`
   LS_OPTIONS=(--color=auto -F -T 0 -N) ;
 else

EOF

# for sysvinit-2.88dsf-x86_64-P13

echo "update sysvinit-2.88dsf-x86_64-P13"
cat <<"EOF" | patch -p0
--- /etc/rc.d/rc.M	2013-12-21 14:38:37.000000000 +0900
+++ /etc/rc.d/rc.M	2015-01-05 14:53:44.000000000 +0900
@@ -253,7 +253,18 @@ if [ -x /usr/bin/update-desktop-database
     if [ ! -f $apps_dir/mimeinfo.cache ] \
         || [ -n "`find $apps_dir -cnewer $apps_dir/mimeinfo.cache`" ] ; then
       echo "Need update mimeinfo.cache"
-      update-desktop-database $apps_dir
+      update-desktop-database $apps_dir ; touch $apps_dir/mimeinfo.cache
+    fi
+  fi
+fi
+
+if [ -x /usr/bin/gtk-update-icon-cache ] ; then
+  iconsdir=/usr/share/icons/hicolor
+  if [ -d $iconsdir ] ; then
+    if [ ! -f $iconsdir/icon-theme.cache ] \
+        || [ -n "`find $iconsdir -cnewer $iconsdir/icon-theme.cache`" ] ; then
+      echo "Need update icon-theme.cache"
+      gtk-update-icon-cache -f $iconsdir ; touch $iconsdir/icon-theme.cache
     fi
   fi
 fi
diff -uprN Old/work/etc/rc.d/rc.inet2 ./work/etc/rc.d/rc.inet2
--- /etc/rc.d/rc.inet2	2013-11-01 22:08:03.000000000 +0900
+++ /etc/rc.d/rc.inet2	2014-02-11 09:33:36.000000000 +0900
@@ -28,6 +28,10 @@ if [ -x /usr/bin/ssh-keygen ] ; then
     echo "/etc/ssh/ssh_host_ecdsa doesn't exist, generating..."
     ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
   fi
+  if [ ! -f /etc/ssh/ssh_host_ed25519_key ] ; then
+    echo "/etc/ssh/ssh_host_ed25519 doesn't exist, generating..."
+    ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
+  fi
 fi
 
 # 起動したいサービスの部分のコメントを削除

EOF

# for network_configs-0.1-noarch-P9

echo "update network_configs-0.1-noarch-P9"
cat << "EOF" | patch -p0
--- /sbin/enetconfig.tradnet	2013-11-12 17:15:52.000000000 +0900
+++ /sbin/enetconfig.tradnet	2014-12-22 13:02:29.000000000 +0900
@@ -617,6 +617,8 @@ HOSTNAME=\`cat /etc/HOSTNAME\`
 /$IFCONFIG lo 127.0.0.1
 /$ROUTE add -net 127.0.0.0 netmask 255.0.0.0 lo
 
+WPASCONF=/etc/wpa_supplicant.conf
+
 get_iwlist() {
   iwlist=\`iwlist wlan0 scan essid "\$1" | awk 'BEGIN {
       RS = "Cell [0-9][0-9] -"; FS = "\n" } enc = wpa = 0
@@ -627,38 +629,65 @@ get_iwlist() {
       printf("%s\n", (enc) ? (wpa) ? "WPA" : "WEP" : "Open") }'\`
 }
 
+get_enc() {
+  enc=\`echo "\$iwlist" | grep "^\$ssid"\$'\t' | uniq | cut -f2\`
+}
+
 if [ -n "\`cat /proc/cmdline | grep "\<wlan\>"\`" ] ; then
   ifconfig wlan0 up
   get_iwlist ""
-  echo "\$iwlist"
-  read -p "Enter SSID: " ssid
-  enc=\`echo "\$iwlist" | grep "\$ssid" | cut -f2\`
+  echo "[Access Points]"
+  echo "\$iwlist" | nkf -e
+  preset=\`[ -f \$WPASCONF ] && grep \$'\t'"ssid=" \$WPASCONF | cut -d\" -f2\`
+  echo "[Preset SSIDs]"
+  echo "\$preset" | nkf -e
+  read -t10 -p "Enter SSID [Preset SSIDs]: " ssid || echo
+  if [ -z "\$ssid" ] ; then
+    newap=0 ; while [ -n "\$preset" ] && read ssid ; do
+      get_enc ; [ -n "\$enc" ] && break
+    done <<< "\$preset"
+  else
+    newap=1 ; get_enc
+  fi
   if [ -z "\$enc" ] ; then
-    get_iwlist "\$ssid"
-    enc=\`echo "\$iwlist" | grep "\$ssid" | cut -f2\`
+    if [ \$newap -eq 0 ] ; then
+      while [ -n "\$preset" ] && read ssid ; do
+        get_iwlist "\$ssid" ; get_enc ; [ -n "\$enc" ] && break
+      done <<< "\$preset"
+    else
+      get_iwlist "\$ssid" ; get_enc
+    fi
     until [ -n "\$enc" ] ; do
       read -p "Enter SSID: " ssid
-      get_iwlist "\$ssid"
-      enc=\`echo "\$iwlist" | grep "\$ssid" | cut -f2\`
+      get_iwlist "\$ssid" ; get_enc
     done
   fi
-  cat <<- "EOF" > /etc/wpa_supplicant.conf
+  if ! echo "\$preset" | grep -q "^\$ssid$" ; then
+    if ! grep -q "^# to be removed" \$WPASCONF ; then
+      cat <<- "EOF" > \$WPASCONF
 	ctrl_interface=/var/run/wpa_supplicant
 	ctrl_interface_group=wheel
+	# to be removed below
 	EOF
-  if [ "\$enc" == "WPA" ] ; then
-    read -p "Enter WPA key: " -s key ; echo
-    err=\`wpa_passphrase \$ssid \$key\`
-    until [ \$? -eq 0 ] ; do
-      echo "\$err"
+    elif ! tail -n1 \$WPASCONF | grep -q "^# to be removed" ; then
+      ed - \$WPASCONF <<- "EOF"
+	/^# to be removed/+1,\$d
+	w
+	EOF
+    fi
+    if [ "\$enc" == "WPA" ] ; then
       read -p "Enter WPA key: " -s key ; echo
       err=\`wpa_passphrase \$ssid \$key\`
-    done
-    wpa_passphrase \$ssid \$key | sed '3i \\\\tscan_ssid=1' | grep -v "#psk=" \\
-        >> /etc/wpa_supplicant.conf
-  elif [ "\$enc" == "WEP" ] ; then
-    read -p "Enter WEP key: " -s key ; echo
-    cat <<- EOF | unexpand >> /etc/wpa_supplicant.conf
+      until [ \$? -eq 0 ] ; do
+        echo "\$err"
+        read -p "Enter WPA key: " -s key ; echo
+        err=\`wpa_passphrase \$ssid \$key\`
+      done
+      wpa_passphrase \$ssid \$key | sed '3i \\\\tscan_ssid=1' | grep -v "#psk=" \\
+          >> \$WPASCONF
+    elif [ "\$enc" == "WEP" ] ; then
+      read -p "Enter WEP key: " -s key ; echo
+      cat <<- EOF | unexpand >> \$WPASCONF
 	network={
 	        ssid="\$ssid"
 	        scan_ssid=1
@@ -666,16 +695,24 @@ if [ -n "\`cat /proc/cmdline | grep "\<w
 	        wep_key0="\$key"
 	}
 	EOF
-  else
-    cat <<- EOF | unexpand >> /etc/wpa_supplicant.conf
+    else
+      cat <<- EOF | unexpand >> \$WPASCONF
 	network={
 	        ssid="\$ssid"
 	        scan_ssid=1
 	        key_mgmt=NONE
 	}
 	EOF
+    fi
+    read  -p "Do you want to save it as a preset? [y/N] " ans
+    if [ "\$ans" == "Y" -o "\$ans" == "y" ] ; then
+      ed - \$WPASCONF <<- "EOF"
+	/^# to be removed/m$
+	w
+	EOF
+    fi
   fi
-  wpa_supplicant -B -c /etc/wpa_supplicant.conf -i wlan0
+  wpa_supplicant -B -c \$WPASCONF -i wlan0
 fi
 
 if [ -f /$INET1SCHEME ] ; then
diff -uprN Old/work/sbin/netconfig.tradnet ./work/sbin/netconfig.tradnet
--- /sbin/netconfig.tradnet	2013-11-12 17:14:39.000000000 +0900
+++ /sbin/netconfig.tradnet	2014-12-22 13:01:16.000000000 +0900
@@ -617,6 +617,8 @@ HOSTNAME=\`cat /etc/HOSTNAME\`
 /$IFCONFIG lo 127.0.0.1
 /$ROUTE add -net 127.0.0.0 netmask 255.0.0.0 lo
 
+WPASCONF=/etc/wpa_supplicant.conf
+
 get_iwlist() {
   iwlist=\`iwlist wlan0 scan essid "\$1" | awk 'BEGIN {
       RS = "Cell [0-9][0-9] -"; FS = "\n" } enc = wpa = 0
@@ -627,38 +629,65 @@ get_iwlist() {
       printf("%s\n", (enc) ? (wpa) ? "WPA" : "WEP" : "Open") }'\`
 }
 
+get_enc() {
+  enc=\`echo "\$iwlist" | grep "^\$ssid"\$'\t' | uniq | cut -f2\`
+}
+
 if [ -n "\`cat /proc/cmdline | grep "\<wlan\>"\`" ] ; then
   ifconfig wlan0 up
   get_iwlist ""
-  echo "\$iwlist"
-  read -p "Enter SSID: " ssid
-  enc=\`echo "\$iwlist" | grep "\$ssid" | cut -f2\`
+  echo "[Access Points]"
+  echo "\$iwlist" | nkf -e
+  preset=\`[ -f \$WPASCONF ] && grep \$'\t'"ssid=" \$WPASCONF | cut -d\" -f2\`
+  echo "[Preset SSIDs]"
+  echo "\$preset" | nkf -e
+  read -t10 -p "Enter SSID [Preset SSIDs]: " ssid || echo
+  if [ -z "\$ssid" ] ; then
+    newap=0 ; while [ -n "\$preset" ] && read ssid ; do
+      get_enc ; [ -n "\$enc" ] && break
+    done <<< "\$preset"
+  else
+    newap=1 ; get_enc
+  fi
   if [ -z "\$enc" ] ; then
-    get_iwlist "\$ssid"
-    enc=\`echo "\$iwlist" | grep "\$ssid" | cut -f2\`
+    if [ \$newap -eq 0 ] ; then
+      while [ -n "\$preset" ] && read ssid ; do
+        get_iwlist "\$ssid" ; get_enc ; [ -n "\$enc" ] && break
+      done <<< "\$preset"
+    else
+      get_iwlist "\$ssid" ; get_enc
+    fi
     until [ -n "\$enc" ] ; do
       read -p "Enter SSID: " ssid
-      get_iwlist "\$ssid"
-      enc=\`echo "\$iwlist" | grep "\$ssid" | cut -f2\`
+      get_iwlist "\$ssid" ; get_enc
     done
   fi
-  cat <<- "EOF" > /etc/wpa_supplicant.conf
+  if ! echo "\$preset" | grep -q "^\$ssid$" ; then
+    if ! grep -q "^# to be removed" \$WPASCONF ; then
+      cat <<- "EOF" > \$WPASCONF
 	ctrl_interface=/var/run/wpa_supplicant
 	ctrl_interface_group=wheel
+	# to be removed below
 	EOF
-  if [ "\$enc" == "WPA" ] ; then
-    read -p "Enter WPA key: " -s key ; echo
-    err=\`wpa_passphrase \$ssid \$key\`
-    until [ \$? -eq 0 ] ; do
-      echo "\$err"
+    elif ! tail -n1 \$WPASCONF | grep -q "^# to be removed" ; then
+      ed - \$WPASCONF <<- "EOF"
+	/^# to be removed/+1,\$d
+	w
+	EOF
+    fi
+    if [ "\$enc" == "WPA" ] ; then
       read -p "Enter WPA key: " -s key ; echo
       err=\`wpa_passphrase \$ssid \$key\`
-    done
-    wpa_passphrase \$ssid \$key | sed '3i \\\\tscan_ssid=1' | grep -v "#psk=" \\
-        >> /etc/wpa_supplicant.conf
-  elif [ "\$enc" == "WEP" ] ; then
-    read -p "Enter WEP key: " -s key ; echo
-    cat <<- EOF | unexpand >> /etc/wpa_supplicant.conf
+      until [ \$? -eq 0 ] ; do
+        echo "\$err"
+        read -p "Enter WPA key: " -s key ; echo
+        err=\`wpa_passphrase \$ssid \$key\`
+      done
+      wpa_passphrase \$ssid \$key | sed '3i \\\\tscan_ssid=1' | grep -v "#psk=" \\
+          >> \$WPASCONF
+    elif [ "\$enc" == "WEP" ] ; then
+      read -p "Enter WEP key: " -s key ; echo
+      cat <<- EOF | unexpand >> \$WPASCONF
 	network={
 	        ssid="\$ssid"
 	        scan_ssid=1
@@ -666,16 +695,24 @@ if [ -n "\`cat /proc/cmdline | grep "\<w
 	        wep_key0="\$key"
 	}
 	EOF
-  else
-    cat <<- EOF | unexpand >> /etc/wpa_supplicant.conf
+    else
+      cat <<- EOF | unexpand >> \$WPASCONF
 	network={
 	        ssid="\$ssid"
 	        scan_ssid=1
 	        key_mgmt=NONE
 	}
 	EOF
+    fi
+    read  -p "Do you want to save it as a preset? [y/N] " ans
+    if [ "\$ans" == "Y" -o "\$ans" == "y" ] ; then
+      ed - \$WPASCONF <<- "EOF"
+	/^# to be removed/m$
+	w
+	EOF
+    fi
   fi
-  wpa_supplicant -B -c /etc/wpa_supplicant.conf -i wlan0
+  wpa_supplicant -B -c \$WPASCONF -i wlan0
 fi
 
 if [ -f /$INET1SCHEME ] ; then


EOF

# for hdsetup version tag
if [ -f /usr/lib/setup/Plamo-5.2 ]; then
    mv /usr/lib/setup/Plamo-5.2 /usr/lib/setup/Plamo-5.3.1
fi

for i in *txz ; do
    updatepkg -f $i
done
