#!/bin/sh
clear
echo "########################################################"
echo "# JSBoard Pre Installer v2.0                           #"
echo "# Scripted By JoungKyun Kim < http://www.oops.org >    #"
echo "########################################################"
echo 

EXE=

echo "STEP 1 Language Check"
echo "---------------------"
echo -n "Can you enable to use KOREAN in this console? [Y/N](default Y) : "
read langs

case "${langs}" in
  N*|n*)
    langs=en ;;
  *)
    langs=ko ;;
esac

me=$(whoami)

if [ "${me}" != "root" ]; then
  if [ "${langs}" = "ko" ]; then
    echo
    echo " !!!"
    echo "  ${me}    ϰ ֽϴ."
    echo " Ʈ     Ʈ  Ͻʽÿ"
    echo
  else
    echo
    echo "Attention !!!"
    echo "Now, U are running this file as ${me} user."
    echo "If u have root privilege, U must excute this file as root user."
    echo
  fi

  if [ "${langs}" = "ko" ]; then
    echo "Root  ٽ ۾ Ͻðڽϱ?"
    echo -n "Root   ٸ N  Ͻʽÿ. [Y/N](default N) : "
  else
    echo "Do you continue as root user?"
    echo -n "If you can't login as root, type N. [Y/N](default N) : "
  fi
  read priv

  case "${priv}" in
    Y*|y*)
      priv=y ;;
    *)
      priv=n ;;
  esac

  if [ "${priv}" = "y" ]; then
    if [ "${langs}" = "ko" ]; then
      echo
      echo "root  α Ͻ Ŀ ٽ  ֽʽÿ"
    else
      echo
      echo "Excute this file after login as root user"
    fi
    exit 0
  fi
fi

if [ ! -d "../../data" ]; then
  mkdir ../../data
fi

if [ ! -d "../../config" ]; then
  mkdir ../../config
fi

if [ "${me}" = "root" ]; then
  AUSER=$(ps aux | grep -E "apache|httpd" | grep -v ^root | grep -v grep | awk '{print $1}' | uniq)

  if [ "${langs}" = "ko" ]; then
    echo
    echo "ġ ${AUSER}   ۵ϰ ִµ ͽϴ."
    echo "½ϱ? (  httpd.conf  Group ڿ Ǿ"
    echo -n "ִ  ġؾ մϴ. [Y/N](default Y) : "
  else
    echo
    echo "Maybe apache is running as ${AUSER} privilege. is Right? This value"
    echo -n "is confirmed value of Group directive in httpd.conf [Y/N](default Y) : "
  fi

  read achk

  case "${achk}" in
    N*|n*)
      achk=n ;;
    *)
      achk=y ;;
  esac

  if [ "${achk}" = "n" ]; then
    AUSER=
    while [ true ]
    do
      if [ "${AUSER}" != "" ]; then
        break
      else
        if [ "${langs}" = "ko" ]; then
          echo
          echo -n "httpd.conf  Group   ־ ֽʽÿ : "
        else
          echo
          echo -n "Input value of Group directive in httpd.conf : "
        fi
        read AUSER
      fi
    done  
  fi

  chgrp ${AUSER} ../../config > /dev/null 2>&1
  [ "$?" != "0" ] && EXE=1
  chgrp ${AUSER} ../../data > /dev/null 2>&1
  [ "$?" != "0" ] && EXE=1
  chmod 731 ../../config > /dev/null 2>&1
  [ "$?" != "0" ] && EXE=1
  chmod 775 ../../data > /dev/null 2>&1
  [ "$?" != "0" ] && EXE=1

  if [ "${EXE}" = "1" ]; then
    if [ "${langs}" = "ko" ]; then
      echo
      echo "${AUSER}  ߸ Դϴ. httpd.conf  Group "
      echo " ȮϽð ٽ õϽʽÿ"
    else
      echo
      echo "Value ${AUSER} is incollect. Retry after confirms value of"
      echo "Group directive in httpd.conf"
    fi
    exit 1
  fi

else
  chmod 707 ../../config
  chmod 707 ../../data
fi

if [ "${langs}" = "ko" ]; then
  echo
  echo "۾ Ϸ Ǿϴ.  jsboard/INSTALLER/  Ͽ"
  echo "ġ  Ͻñ ٶϴ."
else
  echo
  echo "Complete this working. And access jsboard/INSTALLER with web browser,"
  echo "continue install jop"
fi
exit 0
