#!/bin/bash
#
# setup pt2 for Ubuntu 12.04
#
# encoding: UTF-8N
#

ARIB25_BRANCH=ec7c87854f2f
CURRENT_DIR=$(cd $(dirname $0);pwd)
WORK_DIR=$CURRENT_DIR/pt2-`date '+%Y%m%d-%H%M%S'`
mkdir $WORK_DIR
cd $WORK_DIR


# -----------------------------------------------------------------------------
# インストールに必要なパッケージ
sudo apt-get -y install pkg-config unzip autoconf build-essential linux-headers-`uname -r`

# -----------------------------------------------------------------------------
# カードリーダ用パッケージ
# sudo apt-get -y install libccid libpcsclite1 libpcsclite-dev pcsc-tools pcscd
# 2013/08/30現在、最新版だとスマートカードリーダ(SCR3310-NTTCom)が正常に動作しない(v1.7.x は /etc/init.d/pcscd でexit 0が入っているため起動しない。pcscd 1.6.0以下なら動作する。他多重起動等してしまう)
# https://forums.ubuntulinux.jp/viewtopic.php?id=11285

# 旧バージョンをインストール
MARVERICK_REPOSITORY="deb http://old-releases.ubuntu.com/ubuntu/ maverick main universe"
SOURCES_LIST=/etc/apt/sources.list
if [ `grep "$MARVERICK_REPOSITORY" $SOURCES_LIST | wc -l` -ne 0 ]; then
    echo "already exists : $MARVERICK_REPOSITORY"
else
    echo "add repository : $MARVERICK_REPOSITORY"
    sudo echo $'\n\n'# marverick japanese repository$'\n'${MARVERICK_REPOSITORY} | sudo tee -a $SOURCES_LIST
    sudo apt-get update
fi
sudo apt-get --force-yes install libccid=1.3.11-1 libpcsclite1=1.5.5-3ubuntu2 libpcsclite-dev=1.5.5-3ubuntu2 pcscd=1.5.5-3ubuntu2 pcsc-tools=1.4.16-1

# バージョンアップしないように固定
sudo apt-get install wajig
sudo wajig hold libccid libpcsclite1 libpcsclite pcscd pcsc-tools
sudo dpkg --get-selections | grep hold

# -----------------------------------------------------------------------------
# arib25
cd $WORK_DIR
wget http://hg.honeyplanet.jp/pt1/archive/${ARIB25_BRANCH}.zip
unzip ${ARIB25_BRANCH}.zip
cd pt1-${ARIB25_BRANCH}/arib25/src
make
sudo make install

# -----------------------------------------------------------------------------
# pt2 driver
cd $WORK_DIR
wget http://hg.honeyplanet.jp/pt1/archive/tip.tar.bz2
tar jxf tip.tar.bz2
cd `ls | grep pt1-`/driver

# kernel 3.8 patch
cat > pt1_pci.c.3.8.patch << 'EOS'
--- org/pt1_pci.c	2013-08-30 16:14:04.574461359 +0900
+++ pt1_pci.c	2013-08-30 16:17:43.762336654 +0900
@@ -45,7 +45,11 @@
 #include	"pt1_ioctl.h"
 
 /* These identify the driver base version and may not be removed. */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
 static char version[] __devinitdata =
+#else
+static char version[] =
+#endif
 DRV_NAME ".c: " DRV_VERSION " " DRV_RELDATE " \n";
 
 MODULE_AUTHOR("Tomoaki Ishikawa tomy@users.sourceforge.jp and Yoshiki Yazawa yaz@honeyplanet.jp");
@@ -694,7 +698,11 @@
 	}
 	return 0 ;
 }
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
 static int __devinit pt1_pci_init_one (struct pci_dev *pdev,
+#else
+static int pt1_pci_init_one (struct pci_dev *pdev,
+#endif
 				     const struct pci_device_id *ent)
 {
 	int			rc ;
@@ -920,7 +928,11 @@
 
 }
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
 static void __devexit pt1_pci_remove_one(struct pci_dev *pdev)
+#else
+static void pt1_pci_remove_one(struct pci_dev *pdev)
+#endif
 {
 
 	int		lp ;
@@ -987,7 +999,11 @@
 static struct pci_driver pt1_driver = {
 	.name		= DRV_NAME,
 	.probe		= pt1_pci_init_one,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
 	.remove		= __devexit_p(pt1_pci_remove_one),
+#else
+        .remove         = pt1_pci_remove_one,
+#endif
 	.id_table	= pt1_pci_tbl,
 #ifdef CONFIG_PM
 	.suspend	= pt1_pci_suspend,
EOS
patch -p0 < pt1_pci.c.3.8.patch

make
sudo make install
sudo insmod /lib/modules/`uname -r`/kernel/drivers/video/pt1_drv.ko
ls /dev/pt1*

# -----------------------------------------------------------------------------
# recpt1(録画ソフト)
sudo apt-get -y php5-cli
cd ../recpt1
./autogen.sh
./configure --enable-b25
make
sudo make install

# -----------------------------------------------------------------------------
# linuxに最初から入っているpt2ドライバの無効化
BLACK_LIST=`grep "blacklist earth-pt1" /etc/modprobe.d/blacklist.conf`
if [ -z "$BLACK_LIST" ]; then
    sudo sh -c 'echo "\nblacklist earth-pt1" >> /etc/modprobe.d/blacklist.conf'
fi

# -----------------------------------------------------------------------------
# epgdump
cd $WORK_DIR
wget 'http://www.mda.or.jp/epgrec/index.php?plugin=attach&refer=%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E5%89%8D%E3%81%AE%E6%BA%96%E5%82%99&openfile=epgdumpr2.tar.gz' -O epgdumpr2.tar.gz
tar xvzf epgdumpr2.tar.gz
cd epgdumpr2/
make
sudo cp epgdump /usr/local/bin

# -----------------------------------------------------------------------------
# epgrec (WEB番組表+録画管理)
cd $WORK_DIR
wget 'http://sourceforge.jp/frs/redir.php?m=jaist&f=%2Fepgrec%2F46492%2Fepgrec-20100322.tar.gz' -O epgrec-20100322.tar.gz
sudo tar -xvzpf epgrec-20100322.tar.gz -C /var/www/
cd /var/www/epgrec/
[ -f config.php ] || { sudo cp config.php.sample config.php }
sudo cp do-record.sh.pt1 do-record.sh
sudo sed -i -e s/www-data// /etc/at.deny

# fix1
cd $WORK_DIR
wget "http://sourceforge.jp/frs/redir.php?m=globalbase&f=%2Fepgrec%2F46704%2Fepgrec-20100322fix1.tar.gz" -O epgrec-20100322fix1.tar.gz
tar -xf epgrec-20100322fix1.tar.gz
cd /var/www/epgrec/
patch -p1 < $WORK_DIR/epgrec-20100322fix1.diff

# fix2
cd $WORK_DIR
wget "http://sourceforge.jp/frs/redir.php?m=keihanna&f=%2Fepgrec%2F46851%2Fepgrec-20100322fix2.tar.gz" -O epgrec-20100322fix2.tar.gz
tar -xf epgrec-20100322fix2.tar.gz
patch -p1 < $WORK_DIR/epgrec-20100322fix2.diff

# epg
sudo cp /var/www/epgrec/cron.d/getepg /etc/cron.d/
sudo chmod 644 /etc/cron.d/getepg

# -----------------------------------------------------------------------------
# 作業ディレクトリを削除
cd $CURRENT_DIR
echo -n "作業ディレクトリを削除しますか? [y]es, or [n]o: "
read DEL_WORK_DIR
if [ ${DEL_WORK_DIR} = "y" ]; then
    rm -rf ${WORK_DIR}
fi

# -----------------------------------------------------------------------------
# 再起動
echo -n "再起動しますか? [y]es, or [n]o: "
read DEL_WORK_DIR
if [ ${DEL_WORK_DIR} = "y" ]; then
    sudo reboot
fi
