#!/usr/bin/env ruby

# simple script to generate simple cmake modules for finding libraries (packages)
# Alexander Neundorf <neundorf@kde.org>, 2006
# Laurent Montel <montel@kde.org>, 2008 
# usage: generate_findpackage_file
# then you will be prompted to enter the required parameters

print("Name of package: ")
package=gets.chomp

print("pkgconfig package name [e.g. \"libxml-2.0\", we can add check as \"glib-2.0>=2.10 gtk+-2.0\" leave empty to skip pkgconfig]: ")
pkgconfig=gets.chomp

print("Look for header (e.g. \"jpeglib.h\" or \"libxml/xpath.h\"): ")
header=gets.chomp

print("Include subdir (e.g. \"libxml2\", empty to skip ): ")
incSubDir=gets.chomp

print("Look for library (e.g. \"jpeg\" or \"xml2\"): ")
lib=gets.chomp

cmakeIncDirName=package.upcase+"_INCLUDE_DIR"
cmakeLibName=package.upcase+"_LIBRARIES"
cmakeDefsName=package.upcase+"_DEFINITIONS"
cmakeFoundName=package.upcase+"_FOUND"
cmakeQuietName=package+"_FIND_QUIETLY"
cmakeRequiredName=package+"_FIND_REQUIRED"
cmakeFlagsName=package.upcase+"_CFLAGS"
cmakeLibDirName=package.upcase+"_LIBRARY_DIRS"
cmakeIncludeDirName=package.upcase+"_INCLUDE_DIRS"

file=File.new("Find#{package}.cmake", "w+")


file.printf("# - Try to find #{package}\n")
file.printf("# Once done this will define\n")
file.printf("#\n")
file.printf("#  #{cmakeFoundName} - system has #{package}\n")
file.printf("#  #{cmakeIncDirName} - the #{package} include directory\n")
file.printf("#  #{cmakeLibName} - Link these to use #{package}\n")
file.printf("#  #{cmakeDefsName} - Compiler switches required for using #{package}\n")
file.printf("# Redistribution and use is allowed according to the terms of the BSD license.\n")
file.printf("# For details see the accompanying COPYING-CMAKE-SCRIPTS file.\n")
file.printf("#\n\n\n")

file.printf("if ( #{cmakeIncDirName} AND #{cmakeLibName} )\n")
file.printf("   # in cache already\n")
file.printf("   SET(#{package}_FIND_QUIETLY TRUE)\n")
file.printf("endif ( #{cmakeIncDirName} AND #{cmakeLibName} )\n\n")

if not pkgconfig.empty?
   file.printf("# use pkg-config to get the directories and then use these values\n")
   file.printf("# in the FIND_PATH() and FIND_LIBRARY() calls\n")
   file.printf("if( NOT WIN32 )\n")
   file.printf("  find_package(PkgConfig)\n\n")
   file.printf("  pkg_check_modules(#{package.upcase} #{pkgconfig})\n\n")
   file.printf("  set(#{cmakeDefsName} ${#{cmakeFlagsName}})\n")
   file.printf("endif( NOT WIN32 )\n\n")
end

if not header.empty?
  file.printf("FIND_PATH(#{cmakeIncDirName} NAMES #{header}\n")
  if not pkgconfig.empty?
     file.printf("  PATHS\n")
    file.printf("  ${#{cmakeIncludeDirName}}\n")
  end

  if not incSubDir.empty?
     file.printf("  PATH_SUFFIXES #{incSubDir}\n")
  end
  file.printf(")\n\n")
end

if not lib.empty?
  file.printf("FIND_LIBRARY(#{cmakeLibName} NAMES #{lib}\n")
  if not pkgconfig.empty?
     file.printf("  PATHS\n")
    file.printf("  ${#{cmakeLibDirName}}\n")
  end
  file.printf(")\n\n")
end

file.printf("include(FindPackageHandleStandardArgs)\n")
file.printf("FIND_PACKAGE_HANDLE_STANDARD_ARGS(#{package} DEFAULT_MSG #{cmakeIncDirName} #{cmakeLibName} )\n\n")

file.printf("# show the #{cmakeIncDirName} and #{cmakeLibName} variables only in the advanced view\n")
file.printf("MARK_AS_ADVANCED(#{cmakeIncDirName} #{cmakeLibName} )\n\n")


printf("Done, generated Find#{package}.cmake\n")
