#!/bin/sh

homedir=

if test -z $homedir; then
  echo "This script must be installed before usage"
  exit 1
fi

export homedir
cd $homedir

perl -Ilib -mAppConfig < /dev/null 2>/dev/null || { 
  echo You need to have AppConfig perl module installed
  exit 1
}



if test ! -r main.conf; then
  echo "put your configuration into main.conf"
  exit 1
fi


export perllibprepend=""
export perllibadd=""
export presentation_dir="$homedir/presentation"
export sessions_dir="$homedir/sessions"
export userdata_dir="$homedir/userdata"
export unconfirmed_dir="$homedir/unconfirmed"
export del_userda_dir="$homedir/deleted-userdata"


###  parse main.conf and create specific configuration files
perl -Ilib bin/conf.pl mainconf || exit 1

###  load parsed and translated into shell format configuration
. thisconf.sh

if test "$echo_arpu_mails"; then
   echo ''
   echo WARNING! You have an obsolete echo-arpu-mails parameter in main.conf.  Replace it with echo-apu-mails.
   echo ''
fi

function print_perl_preamble ()
{
    file=$1
    addlib=
    echo "#!$perlbin $addlib"     > $1
    echo "# automatically generated by $0"   >>$1
    echo ""                                  >>$1
    echo "my \$homedir = '$homedir';"        >>$1
    echo ""                                  >>$1
    echo "use lib qw( $homelib );"  >>$1
    if [[ $perllibprepend ]]; then
       echo "use lib qw( $perllibprepend );"      >>$1
       echo ""                               >>$1
    fi
    echo ""                                  >>$1
}

function print_sh_preamble ()
{
    file=$1
    addlib=
    echo "#!/bin/bash"     > $1
    echo "# automatically generated by $0"   >>$1
    echo "homedir=$homedir"                  >>$1
    echo ""                                  >>$1
    echo ". \$homedir/thisconf.sh"           >>$1
    echo ""                                  >>$1
}


###  NOW SETUP ACIS ENVIRONMENT

if test -z $static_base_dir; then
  echo "You have to set static-base-dir in main.conf";
  exit 1
fi


###  checking directories

test -d $homebin || mkdir $homebin
test -d $homelib || mkdir $homelib

test -d $presentation_dir    || mkdir $presentation_dir 
test -d $sessions_dir        || mkdir $sessions_dir
test -d $userdata_dir        || mkdir $userdata_dir
test -d $unconfirmed_dir     || mkdir $unconfirmed_dir
test -d $del_userda_dir      || mkdir $del_userda_dir

test $metadata_ReDIF_output_dir && ( test -d $metadata_ReDIF_output_dir || mkdir $metadata_ReDIF_output_dir )
test $metadata_AMF_output_dir && ( test -d $metadata_AMF_output_dir || mkdir $metadata_AMF_output_dir )

test -d $static_base_dir  || mkdir $static_base_dir


###  install support scripts 
echo -n installing additional scripts into $homedir/bin:
for i in bin/templates/*.pl; 
do
    name=${i##*/}
    name=${name%.pl}
    destname="bin/$name"
    echo -n " $name"
    print_perl_preamble $destname
    cat $i >> $destname
    chmod +x $destname
done
for i in bin/templates/*.sh; 
do
     name=${i##*/}
     name=${name%.sh}
     destname="bin/$name"
     echo -n " $name"
     print_sh_preamble $destname
     cat $i >> $destname
     chmod +x $destname
done
echo ""



###  install ACIS CGI script
###  install static web-accessible stuff

if test $base_cgi_script_filename; then
  echo writing $base_cgi_script_filename
  . bin/setup.cgi_frontend > $base_cgi_script_filename
  chmod +x $base_cgi_script_filename
fi

if test "$cgi_pperl_reinit"; then
    # reinitialize the PPerl persistent environment
    /bin/bash -c "$cgi_pperl_reinit"
fi

if test -z $tempate_set; then
    template_set='default'
fi

if test -d $static_base_dir; then 
  dest=$static_base_dir
  cp -r $presentation_dir/$template_set/{style,script}  $dest
  cat $dest/style/main.css $dest/style/brownish.css.add > $dest/style/brownish.css 
fi

# . bin/setup.make_author_index 

# cleanupscript=$homebin/clean-up.pl
# echo installing clean-up script to $cleanupscript
# . bin/setup.clean-up.pl > $cleanupscript
# chmod +x $cleanupscript


test $log_browsing_cgi_filename && {
    dest=$log_browsing_cgi_filename
    echo installing log browsing script to $dest
    . bin/setup.logs-browsing > $dest
    chmod +x $dest
}



###  SETUP OTHER RELATED PIECES 

###  RePEc-Index and ShortIDs

test -d $ri_dir  || mkdir $ri_dir
test -d $sid_dir || mkdir $sid_dir


###  write RI/collections
. bin/setup.ri_collections > RI/collections

###  write RePEc-Index local setup
. bin/setup.ri_local_setup.pm  > lib/RePEc/Index/local_setup.pm

###  write ShortIDs local setup
test -d lib/ACIS/ShortIDs || mkdir  lib/ACIS/ShortIDs
. bin/setup.sid_local.pm  > lib/ACIS/ShortIDs/Local.pm
bin/sid_base create-tables


###  write lib/ARDB/LocalConfig.pm
echo configuring ARDB for home dir
perl -e 'print "package ARDB::LocalConfig;\n\$local_path=\"$ENV{homedir}\";\n1;\n";' > lib/ARDB/LocalConfig.pm

### finalize ARDB setup
bin/setup_ardb
bin/create_tables


if [ -f VERSION.NEW ]; then 
    $homedir/bin/upgrade && mv VERSION.NEW VERSION
fi


###  write lib/ACIS/LocalConfig.pm
echo configuring ACIS for home dir
perl -e 'print "package ACIS::LocalConfig;\n\$home_dir=\"$ENV{homedir}\";\n1;\n";' > lib/ACIS/LocalConfig.pm

### finalize ACIS
echo parsing and checking ACIS::Web configuration
bin/reparse_config 'ACIS::Web' acis.conf

bin/setup_profile_menu








