#!/bin/bash

# Usage
#
#   sh make_people_list.sh english-index-file norwegian-index-file < people_list.txt 
#
# The syntax of the input-file is
#   
#   Title;Tittle
#   name1; position; stilling; affiliation; picture-link; home-page
#   name2; position; stilling; affiliation; picture-link; home-page
#   ....
#
# eg.
#
# The title is the title of the people list, eg. "Leadership and Administration"
#
# The output-file is the name of the generated file, e.g. index_test.html to replace the old index.html after inspection

en=$1
no=$2

IFS=''
read line
title=$(echo $line | sed 's/; */;/g' | cut -d ';' -f 1)
tittel=$(echo $line | sed 's/; */;/g' | cut -d ';' -f 2)


#print header
echo '{                                    '   | tee    $no $en
echo '  "resourcetype": "frontpage-two",   '   | tee -a $no $en
echo '  "properties": {                    '   | tee -a $no $en
echo '    "title": "'$title'",             '   | tee -a     $en
echo '    "title": "'$tittel'",            '   | tee -a $no
echo '    "hideDocumentTitle":false,       '   | tee -a $no $en
echo '    "content": [                     '   | tee -a $no $en
echo '     {                               '   | tee -a $no $en
echo '      "color": "white",              '   | tee -a $no $en
echo '      "hidden": false,               '   | tee -a $no $en
echo '      "layout": 1,                   '   | tee -a $no $en
echo '      "boxes": [                     '   | tee -a $no $en

DONE=false
read line || DONE=true
ALIGN="left"
until $DONE
do
  name=$(echo $line | sed 's/; */;/g' | cut -d ';' -f 1)
  pos=$( echo $line | sed 's/; */;/g' | cut -d ';' -f 2)
  stilling=$( echo $line | sed 's/; */;/g' | cut -d ';' -f 3)
  aff=$( echo $line | sed 's/; */;/g' | cut -d ';' -f 4)
  pic=$( echo $line | sed 's/; */;/g' | cut -d ';' -f 5)
  www=$( echo $line | sed 's/; */;/g' | cut -d ';' -f 6)
  #Make person entry
  echo '          {'                                                | tee -a $no $en
  echo '            "boxContent": "<a href=\"'$www'\">'$name'</a>\\n<br/>'$pos' <br />\\n'$aff'\\n<p>&nbsp;</p>\\n",'                                                                           | tee -a     $en
  echo '            "boxContent": "<a href=\"'$www'\">'$name'</a>\\n<br/>'$stilling' <br />\\n'$aff'\\n<p>&nbsp;</p>\\n",'                                                                           | tee -a     $no
  echo '            "boxType": "person-image-box",'                 | tee -a $no $en
  echo '            "boxCssColor": "white-box",'                    | tee -a $no $en
  echo '            "boxPicture": "'$pic'",'                        | tee -a $no $en
  echo '            "boxPictureAlt": "portrettbilde",'              | tee -a $no $en
  if [ "$ALIGN" = "left" ];then
    echo '            "boxCssSizeAndPlacement": "third-box-left",'  | tee -a $no $en
    ALIGN="middle"
  elif [ "$ALIGN" = "middle" ];then
    echo '            "boxCssSizeAndPlacement": "third-box-middle",'| tee -a $no $en
    ALIGN="right"
  else
    echo '            "boxCssSizeAndPlacement": "third-box-right",' | tee -a $no $en
    ALIGN="left"
  fi
  echo '            "boxTitle": ""'                                 | tee -a $no $en
  echo '          },'                                               | tee -a $no $en
  read line || DONE=true
  if [ "$DONE" = true ];then 
    if [ "$ALIGN" = "left" ];then
      echo '          {'                                              | tee -a $no $en
      echo '            "boxCssSizeAndPlacement": "third-box-left"'  | tee -a $no $en
      echo '          },'                                             | tee -a $no $en
      ALIGN="middle"
    fi
    if [ "$ALIGN" = "middle" ];then
      echo '          {'                                              | tee -a $no $en
      echo '            "boxCssSizeAndPlacement": "third-box-middle"'| tee -a $no $en
      echo '          },'                                             | tee -a $no $en
      ALIGN="right"
    fi
    if [ "$ALIGN" = "right" ];then
      echo '          {'                                              | tee -a $no $en
      echo '            "boxCssSizeAndPlacement": "third-box-right"' | tee -a $no $en
      echo '          }'                                              | tee -a $no $en
    fi
  fi
done 
echo '    ]'                                                 | tee -a $no $en
echo '   }'                                                  | tee -a $no $en
echo '  ]'                                                   | tee -a $no $en
echo ' }'                                                    | tee -a $no $en
echo '}'                                                     | tee -a $no $en
