#!/bin/sh
# Copyright (c) 2000-2012 Synology Inc. All rights reserved.

if [ "$SYNOPKG_PKG_STATUS" = "UPGRADE" ]; then
	exit 0
fi

# Check phpbb's link not exist
if [ -e /var/services/web/phpbb ]; then
	if [ -z "$SYNOPKG_DSM_LANGUAGE" ]; then
		echo "The path of phpbb already exist in web folder, please rename or move it to other place" > $SYNOPKG_TEMP_LOGFILE
		exit 1
	fi
	case $SYNOPKG_DSM_LANGUAGE in
				chs)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		cht)
			echo "在你的web資料夾底下已經存在phpbb這個檔案或資料夾，再安裝phpBB套件前請更明或搬動此檔案或資料夾" > $SYNOPKG_TEMP_LOGFILE 
		;;
		csy)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		dan)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		enu)
			echo "A phpBB folder already exists under your Web folder. Please rename it or move it before you install the phpBB package." > $SYNOPKG_TEMP_LOGFILE 
		;;
		fre)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		ger)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		hun)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		ita)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		jpn)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		krn)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		nld)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		nor)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		plk)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		ptb)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		ptg)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		rus)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		spn)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		sve)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		trk)
			echo "there already exist phpbb folder under your web folder. please renamed or moved it before you install phpBB package" > $SYNOPKG_TEMP_LOGFILE 
		;;
		*)
			echo "The path of phpbb already exist in web folder, please rename or move it to other place" > $SYNOPKG_TEMP_LOGFILE 
		;;
	esac
	exit 1
fi

RunWeb=`/bin/get_key_value /etc/synoinfo.conf runweb` 
RunMySQL=`/bin/get_key_value /etc/synoinfo.conf runmysql`
if [ ! -d /var/services/web -o "x$RunWeb" != "xyes" ]; then
	echo "Please enable Web Station first." > $SYNOPKG_TEMP_LOGFILE
	exit 1
fi
if [ "x$RunMySQL" != "xyes" ]; then
	echo "Please enable MySQL first." >$SYNOPKG_TEMP_LOGFILE
	exit 1
fi

# Check database name
if [ -z "$pkgwizard_db_name" ]; then
	if [ -z "$SYNOPKG_DSM_LANGUAGE" ]; then
		echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE
		exit 1
	fi
	case $SYNOPKG_DSM_LANGUAGE in
				chs)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		cht)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		csy)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		dan)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		enu)
			echo "Please specify the database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		fre)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		ger)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		hun)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		ita)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		jpn)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		krn)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		nld)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		nor)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		plk)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		ptb)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		ptg)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		rus)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		spn)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		sve)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		trk)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
		*)
			echo "Please specify database name" > $SYNOPKG_TEMP_LOGFILE 
		;;
	esac
	exit 1
fi

# Check database password
pkgwizard_mysql_password=`echo $pkgwizard_mysql_password | sed 's/"/\\"/gp'`
/usr/syno/mysql/bin/mysql -u root --password="$pkgwizard_mysql_password" -e "exit"
if [ $? -eq 1 ]; then
	if [ -z "$SYNOPKG_DSM_LANGUAGE" ]; then
		echo "Please check your MySQL password" > $SYNOPKG_TEMP_LOGFILE
		exit 1
	fi
	case $SYNOPKG_DSM_LANGUAGE in
				chs)
			echo "请检查您的 MySQL 密码，然后再试一次。" > $SYNOPKG_TEMP_LOGFILE 
		;;
		cht)
			echo "請檢查您的 MySQL 密碼，然後再試一次。" > $SYNOPKG_TEMP_LOGFILE 
		;;
		csy)
			echo "Zkontrolujte heslo MySQL a zkuste to znovu." > $SYNOPKG_TEMP_LOGFILE 
		;;
		dan)
			echo "Kontroller din MySQL-adgangskode, og prøv igen." > $SYNOPKG_TEMP_LOGFILE 
		;;
		enu)
			echo "Please check your MySQL password and try again." > $SYNOPKG_TEMP_LOGFILE 
		;;
		fre)
			echo "Veuillez vérifier votre mot de passe MySQL et réessayer." > $SYNOPKG_TEMP_LOGFILE 
		;;
		ger)
			echo "Prüfen Sie Ihr MySQL-Passwort und versuchen Sie es erneut." > $SYNOPKG_TEMP_LOGFILE 
		;;
		hun)
			echo "Ellenőrizze MySQL–jelszavát, majd próbálja újra." > $SYNOPKG_TEMP_LOGFILE 
		;;
		ita)
			echo "Controllare la password MySQL e riprovare." > $SYNOPKG_TEMP_LOGFILE 
		;;
		jpn)
			echo "MySQL のパスワードを確認してもう一度行ってください。" > $SYNOPKG_TEMP_LOGFILE 
		;;
		krn)
			echo "MySQL 패스워드를 확인하고 다시 시도하십시오." > $SYNOPKG_TEMP_LOGFILE 
		;;
		nld)
			echo "Controleer uw MySQL-wachtwoord en probeer het opnieuw." > $SYNOPKG_TEMP_LOGFILE 
		;;
		nor)
			echo "Vennligst sjekk MySQL-passordet og prøv igjen." > $SYNOPKG_TEMP_LOGFILE 
		;;
		plk)
			echo "Sprawdź swoje hasło MySQL i spróbuj ponownie." > $SYNOPKG_TEMP_LOGFILE 
		;;
		ptb)
			echo "Verifique sua senha MySQL e tente novamente." > $SYNOPKG_TEMP_LOGFILE 
		;;
		ptg)
			echo "Verifique a sua palavra-passe do MySQL e tente novamente." > $SYNOPKG_TEMP_LOGFILE 
		;;
		rus)
			echo "Проверьте пароль MySQL и повторите попытку." > $SYNOPKG_TEMP_LOGFILE 
		;;
		spn)
			echo "Compruebe su contraseña de MySQL y vuelva a intentarlo." > $SYNOPKG_TEMP_LOGFILE 
		;;
		sve)
			echo "Kontrollera ditt MySQL-lösenord och försök igen." > $SYNOPKG_TEMP_LOGFILE 
		;;
		trk)
			echo "Lütfen MySQL parolanızı kontrol edin ve yeniden deneyin." > $SYNOPKG_TEMP_LOGFILE 
		;;
		*)
			echo "Check your MySQL password" > $SYNOPKG_TEMP_LOGFILE 
		;;
	esac
	exit 1
fi

# Check existence of database
/usr/syno/mysql/bin/mysql -u root --password="$pkgwizard_mysql_password" -e "SHOW DATABASES" | grep ^$pkgwizard_db_name$
[ $? -eq 0 ] && DB_EXIST="yes" || DB_EXIST="no"

# Check previous installation
if [ -f /var/packages/.phpbb.conf ]; then
	TEMP_PATH=`/bin/get_key_value /var/packages/.phpbb.conf path`
	if [ -s "$TEMP_PATH/phpBB3/config.php" ]; then
		set -o pipefail
		DB_NAME=`grep '$dbname' $TEMP_PATH/phpBB3/config.php | cut -d"'" -f2`
		[ $? -eq 0 -a "$DB_NAME" = $pkgwizard_db_name ] && DB_SAME="yes" || DB_SAME="no"
		set +o pipefail
	else
		DB_SAME="no"
	fi
else
	DB_SAME="no"
fi

# Decide whether to continue
if [ "x$DB_EXIST" = "xyes" -a "x$DB_SAME" = "xno" ]; then
	if [ -z "$SYNOPKG_DSM_LANGUAGE" ]; then
		echo "This database already exists" > $SYNOPKG_TEMP_LOGFILE
		exit 1
	fi
	case $SYNOPKG_DSM_LANGUAGE in
				chs)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		cht)
			echo "資料庫已存在。為避免被 phpBB 的安裝步驟所覆寫，請使用另一個資料庫名稱。" > $SYNOPKG_TEMP_LOGFILE 
		;;
		csy)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		dan)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		enu)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		fre)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		ger)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		hun)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		ita)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		jpn)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		krn)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		nld)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		nor)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		plk)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		ptb)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		ptg)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		rus)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		spn)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		sve)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		trk)
			echo "Database already exists. In order to avoid being overwritten by phpBB's install procedures, please use another database name." > $SYNOPKG_TEMP_LOGFILE 
		;;
		*)
			echo "This database already exists" > $SYNOPKG_TEMP_LOGFILE 
		;;
	esac
	exit 1
fi

exit 0
