#? stdtmpl(subsChar = '?') | standard
#proc generateInstallScript(c: ConfigData): string = 
#  result = "#! /bin/sh\n# Generated by niminst\n"
#  var proj = c.name.toLower

set -e

if [ $# -eq 1 ] ; then
# if c.cat[fcUnixBin].len > 0:
  if test -f ?{c.cat[fcUnixBin][0].toUnix}
  then 
    echo "?c.displayName build detected"
  else
    echo "Please build ?c.displayName before installing it"
    exit 1
  fi
#  end if
  case $1 in
    "--help"|"-h"|"help"|"h")
      echo "?c.displayName installation script"
      echo "Usage: [sudo] sh install.sh DIR"
      echo "Where DIR may be:"
      echo "  /usr/bin"
      echo "  /usr/local/bin"
      echo "  /opt"
      echo "  <some other dir> (treated like '/opt')"
      echo "To deinstall, use the command:"
      echo "sh deinstall.sh DIR"
      exit 1
      ;;
    "/usr/bin")
      bindir=/usr/bin
      configdir=/etc
      libdir=/usr/lib/?proj
      docdir=/usr/share/?proj/doc
      datadir=/usr/share/?proj/data
      ;;
    "/usr/local/bin")
      bindir=/usr/local/bin
      configdir=/etc
      libdir=/usr/local/lib/?proj
      docdir=/usr/local/share/?proj/doc
      datadir=/usr/local/share/?proj/data
      ;;
    *)
      bindir="$1/?proj/bin"
      configdir="$1/?proj/config"
      libdir="$1/?proj/lib"
      docdir="$1/?proj/doc"
      datadir="$1/?proj/data"
      
      mkdir -p $1/?proj
      mkdir -p $bindir
      mkdir -p $configdir
      ;;
  esac
  mkdir -p $libdir
  mkdir -p $docdir
  echo "copying files..."
#var createdDirs = newStringTable()
#for cat in fcConfig..fcLib:
#  for f in items(c.cat[cat]):
#    var mk = splitFile(f.skipRoot).dir
#    if mk.len > 0:
#      mk = unixDirVars[cat] & "/" & mk
#      if not createdDirs.hasKey(mk):
#        createdDirs[mk] = "true"
  mkdir -p ?{mk.toUnix}
#      end if
#    end if
#  end for
#end for

#for f in items(c.cat[fcUnixBin]):
  cp ?f.toUnix $bindir/?f.skipRoot.toUnix
  chmod 755 $bindir/?f.skipRoot.toUnix
#end for
#for f in items(c.cat[fcConfig]): 
  cp ?f.toUnix $configdir/?f.skipRoot.toUnix
  chmod 644 $configdir/?f.skipRoot.toUnix
#end for
#for f in items(c.cat[fcData]): 
  if [ -f ?f.toUnix ]; then
    cp ?f.toUnix $datadir/?f.skipRoot.toUnix
    chmod 644 $datadir/?f.skipRoot.toUnix
  fi
#end for
#for f in items(c.cat[fcDoc]):
  if [ -f ?f.toUnix ]; then
    cp ?f.toUnix $docdir/?f.skipRoot.toUnix
    chmod 644 $docdir/?f.skipRoot.toUnix
  fi
#end for
#for f in items(c.cat[fcLib]):
  cp ?f.toUnix $libdir/?f.skipRoot.toUnix
  chmod 644 $libdir/?f.skipRoot.toUnix
#end for
  
  echo "installation successful"
else
  echo "?c.displayName installation script"
  echo "Usage: [sudo] sh install.sh DIR"
  echo "Where DIR may be:"
  echo "  /usr/bin"
  echo "  /usr/local/bin"
  echo "  /opt"
  echo "  <some other dir> (treated like '/opt')"
  echo "To deinstall, use the command:"
  echo "sh deinstall.sh DIR"
  exit 1
fi
